Skip to content

⚡ Bolt: [performance improvement] Hoist regex and dictionary allocations#450

Closed
aafre wants to merge 1 commit into
mainfrom
bolt-hoist-regex-compilation-17262486954979246865
Closed

⚡ Bolt: [performance improvement] Hoist regex and dictionary allocations#450
aafre wants to merge 1 commit into
mainfrom
bolt-hoist-regex-compilation-17262486954979246865

Conversation

@aafre
Copy link
Copy Markdown
Owner

@aafre aafre commented Apr 7, 2026

💡 What: Hoisted the LATEX_SPECIAL_CHARS static dictionary and its corresponding re.compile() call (LATEX_ESCAPE_PATTERN) to the module level in both app.py and resume_generator_latex.py.

🎯 Why: The _escape_latex utility 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 (like test_latex_escaping.py) will continue to pass without regression.


PR created automatically by Jules for task 17262486954979246865 started by @aafre

…_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>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread app.py
Comment on lines +476 to +493
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()))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
  1. Avoid duplicating configuration data (e.g., mappings) across files to prevent inconsistencies; define as module-level constants and import where needed.

@aafre
Copy link
Copy Markdown
Owner Author

aafre commented Apr 11, 2026

Closing: superseded by #440 (cleanest hoisting). Cherry-picked into #457.

@aafre aafre closed this Apr 11, 2026
@google-labs-jules
Copy link
Copy Markdown
Contributor

Closing: superseded by #440 (cleanest hoisting). Cherry-picked into #457.

Understood. Acknowledging that this work is superseded and stopping work on this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant