⚡ Bolt: optimize _escape_latex regex compilation#448
Conversation
Co-authored-by: aafre <8656674+aafre@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Code Review
This pull request optimizes LaTeX character escaping by hoisting static dictionaries and regex patterns to module-level constants in app.py and resume_generator_latex.py. Feedback suggests consolidating this duplicated logic into a shared utility module to improve maintainability. Further performance improvements are recommended, including hoisting the replacement callback function to avoid repeated lambda allocations and removing redundant .keys() calls during regex compilation.
| return "LinkedIn Profile" | ||
|
|
||
|
|
||
| LATEX_SPECIAL_CHARS = { |
There was a problem hiding this comment.
This dictionary and the associated LaTeX escaping logic are duplicated in resume_generator_latex.py. Consider consolidating these into a shared utility module to improve maintainability and ensure consistency across the application. Centralizing configuration mappings helps prevent logic from becoming out of sync.
References
- Avoid duplicating configuration data or mappings across different files to prevent them from becoming out of sync and to improve maintainability.
| "-": r"{-}", | ||
| } | ||
| LATEX_ESCAPE_PATTERN = re.compile("|".join(re.escape(key) for key in LATEX_SPECIAL_CHARS.keys())) |
There was a problem hiding this comment.
To further optimize performance, hoist the replacement callback to a module-level function. This avoids the overhead of creating a new lambda object on every call to _escape_latex. Additionally, .keys() is redundant when iterating over a dictionary, and adding a descriptive comment for the hyphen escape improves clarity.
"-": r"{-", # Protect hyphens that might be misinterpreted as math operators
}
LATEX_ESCAPE_PATTERN = re.compile("|".join(re.escape(key) for key in LATEX_SPECIAL_CHARS))
def _latex_char_replacer(match):
return LATEX_SPECIAL_CHARS[match.group(0)]| # Bolt Optimization: Regex compilation and mapping dictionary are hoisted to module-level constants | ||
| # to avoid O(N) redundant processing on every function call. | ||
| escaped_text = LATEX_ESCAPE_PATTERN.sub(lambda match: LATEX_SPECIAL_CHARS[match.group(0)], text) |
There was a problem hiding this comment.
Use the hoisted _latex_char_replacer function to avoid repeated lambda allocations.
| # Bolt Optimization: Regex compilation and mapping dictionary are hoisted to module-level constants | |
| # to avoid O(N) redundant processing on every function call. | |
| escaped_text = LATEX_ESCAPE_PATTERN.sub(lambda match: LATEX_SPECIAL_CHARS[match.group(0)], text) | |
| # Bolt Optimization: Regex compilation and mapping dictionary are hoisted to module-level constants | |
| # to avoid redundant processing on every function call. | |
| escaped_text = LATEX_ESCAPE_PATTERN.sub(_latex_char_replacer, text) |
| # Use a regular expression to find and replace all special characters | ||
| # This approach ensures each character is handled once | ||
| LATEX_ESCAPE_PATTERN = re.compile("|".join(re.escape(key) for key in LATEX_SPECIAL_CHARS.keys())) |
There was a problem hiding this comment.
Hoist the replacement callback to a module-level function to avoid repeated lambda allocations, and remove the redundant .keys() call.
# Use a regular expression to find and replace all special characters
# This approach ensures each character is handled once
LATEX_ESCAPE_PATTERN = re.compile("|".join(re.escape(key) for key in LATEX_SPECIAL_CHARS))
def _latex_char_replacer(match):
return LATEX_SPECIAL_CHARS[match.group(0)]| # Bolt Optimization: Regex compilation and mapping dictionary are hoisted to module-level constants | ||
| # to avoid O(N) redundant processing on every function call. | ||
| escaped_text = LATEX_ESCAPE_PATTERN.sub(lambda match: LATEX_SPECIAL_CHARS[match.group(0)], text) |
There was a problem hiding this comment.
Use the hoisted _latex_char_replacer function here.
| # Bolt Optimization: Regex compilation and mapping dictionary are hoisted to module-level constants | |
| # to avoid O(N) redundant processing on every function call. | |
| escaped_text = LATEX_ESCAPE_PATTERN.sub(lambda match: LATEX_SPECIAL_CHARS[match.group(0)], text) | |
| # Bolt Optimization: Regex compilation and mapping dictionary are hoisted to module-level constants | |
| # to avoid redundant processing on every function call. | |
| escaped_text = LATEX_ESCAPE_PATTERN.sub(_latex_char_replacer, text) |
💡 What
Optimized the
_escape_latexfunction in bothapp.pyandresume_generator_latex.pyby hoisting thelatex_special_charsstatic dictionary and there.compilepattern out of the function scope and into module-level constants (LATEX_SPECIAL_CHARSandLATEX_ESCAPE_PATTERN).🎯 Why
In Python, defining a large dictionary and calling$O(N)$ operation per call, degrading rendering performance.
re.compileinside a function means those objects are recreated and recompiled on every single function invocation. Since_escape_latexis a core utility function that gets called recursively (e.g., viaapply_escaping_recursive) or in loops for every piece of string data in the AST/JSON during LaTeX generation, this redundant compilation and memory allocation acts as a hidden📊 Impact
🔬 Measurement
Run the backend tests (
python3 -m pytest tests/) to ensure the escaping logic remains completely accurate while benefiting from the module-level caching. The optimization requires zero logic changes to the actual regex matching.PR created automatically by Jules for task 9610003798119027084 started by @aafre