Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
## 2025-02-18 - Regex Pre-compilation in Hot Paths
**Learning:** Re-compiling regexes inside a frequently called function (like `latex_escape` which runs for every string) creates significant overhead. Pre-compiling them at module level yielded a ~3.2x speedup.
**Action:** Always look for regex compilations inside loops or frequently called functions and move them to module level constants.

## 2024-05-15 - [Regex replacement for LaTeX escaping in resume_pdf_lib]
**Learning:** The `resume_pdf_lib/generator.py` implementation of `latex_escape` was using a character-by-character `while` loop which is very slow in Python. Replacing it with a module-level compiled regex (`_LATEX_ESCAPE_PATTERN`) and a lookup map (`_LATEX_ESCAPE_MAP`) provides significantly faster performance. This implementation intentionally omits Markdown bold processing, distinguishing it from `cli/utils/template_filters.py`.
**Action:** Always favor pre-compiled module-level regex `.sub()` with a mapping function over manual string loops in Python when performing character translations. Look out for differences in expected behavior (like the intentional omission of Markdown parsing) when porting optimizations between similar functions.
52 changes: 22 additions & 30 deletions resume_pdf_lib/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@
from jinja2 import Environment, FileSystemLoader, select_autoescape
from markupsafe import Markup

_LATEX_ESCAPE_MAP = {
"\\": r"\textbackslash{}",
"&": r"\&",
"%": r"\%",
"$": r"\$",
"#": r"\#",
"_": r"\_",
"{": r"\{",
"}": r"\}",
"~": r"\textasciitilde{}",
"^": r"\^{}",
"<": r"\textless{}",
">": r"\textgreater{}",
"[": r"{[}",
"]": r"{]}",
}

_LATEX_ESCAPE_PATTERN = re.compile("|".join(map(re.escape, _LATEX_ESCAPE_MAP.keys())))

from .exceptions import (
InvalidVariantError,
LaTeXCompilationError,
Expand Down Expand Up @@ -483,37 +502,10 @@ def latex_escape(text: Any) -> Markup:

text_str = str(text)

# Process the string character by character
result = []
i = 0
while i < len(text_str):
char = text_str[i]

if char == "\\":
result.append(r"\textbackslash{}")
elif char in "&%$#_{}~^<>[]":
escaped_map = {
"&": r"\&",
"%": r"\%",
"$": r"\$",
"#": r"\#",
"_": r"\_",
"{": r"\{",
"}": r"\}",
"~": r"\textasciitilde{}",
"^": r"\^{}",
"<": r"\textless{}",
">": r"\textgreater{}",
"[": r"{[}",
"]": r"{]}",
}
result.append(escaped_map[char])
else:
result.append(char)

i += 1
def replace(match: re.Match) -> str:
return _LATEX_ESCAPE_MAP[match.group(0)]

return Markup("".join(result))
return Markup(_LATEX_ESCAPE_PATTERN.sub(replace, text_str))


def proper_title(text: str) -> str:
Expand Down
Loading