⚡ Bolt: [performance improvement] Hoist regex and dictionary allocations#450
⚡ Bolt: [performance improvement] Hoist regex and dictionary allocations#450aafre wants to merge 1 commit into
Conversation
…_latex.py Moves `LATEX_SPECIAL_CHARS` and `LATEX_ESCAPE_PATTERN` to the module level in `app.py` and `resume_generator_latex.py`. This prevents redundant allocations and regex compilations on every call to the `_escape_latex` function, improving performance during template rendering. 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 performance by hoisting LaTeX special character mappings and compiled regex patterns to the module level in both app.py and resume_generator_latex.py, which prevents redundant allocations during recursive calls to _escape_latex. Feedback indicates that these constants and the escaping logic are now duplicated across files, suggesting they should be extracted into a shared utility module to adhere to DRY principles and ensure consistency.
| LATEX_SPECIAL_CHARS = { | ||
| "\\": r"\textbackslash{}", | ||
| "&": r"\&", | ||
| "%": r"\%", | ||
| "$": r"\$", | ||
| "#": r"\#", | ||
| # "_": r"\_", # Don't escape: used for markdown italic/bold (_text_ and __text__) | ||
| "{": r"\{", | ||
| "}": r"\}", | ||
| # "~": r"\textasciitilde{}", # Don't escape: used for markdown strikethrough (~~text~~) | ||
| "^": r"\textasciicircum{}", | ||
| "<": r"\textless{}", | ||
| ">": r"\textgreater{}", | ||
| "|": r"\textbar{}", | ||
| "-": r"{-}", | ||
| } | ||
|
|
||
| LATEX_ESCAPE_PATTERN = re.compile("|".join(re.escape(key) for key in LATEX_SPECIAL_CHARS.keys())) |
There was a problem hiding this comment.
The LATEX_SPECIAL_CHARS dictionary and the logic for LATEX_ESCAPE_PATTERN are now duplicated across app.py and resume_generator_latex.py. While hoisting these to the module level correctly addresses the performance issue, maintaining two identical copies of this logic increases the risk of inconsistencies in the future. Consider extracting these constants and the _escape_latex function into a shared utility module to adhere to the DRY (Don't Repeat Yourself) principle.
References
- Avoid duplicating configuration data (e.g., mappings) across files to prevent inconsistencies; define as module-level constants and import where needed.
💡 What: Hoisted the
LATEX_SPECIAL_CHARSstatic dictionary and its correspondingre.compile()call (LATEX_ESCAPE_PATTERN) to the module level in bothapp.pyandresume_generator_latex.py.🎯 Why: The
_escape_latexutility function is called frequently (and sometimes recursively) when generating PDFs. Defining the dictionary and compiling the regular expression inside the function body caused these objects to be re-created and re-compiled on every single call. Moving them to the module scope ensures they are instantiated and compiled exactly once at load time, reducing CPU overhead and memory churn.📊 Impact: Reduces redundant dictionary allocations and regex compilations inside hot-path rendering operations, leading to faster execution times for PDF generation, especially for large resumes with many text fields that need escaping.
🔬 Measurement: Verify by running the Python test suite (
python3 -m pytest tests/). Test execution time will be slightly faster, and all text escaping tests (liketest_latex_escaping.py) will continue to pass without regression.PR created automatically by Jules for task 17262486954979246865 started by @aafre