⚡ Bolt: Optimize regex and caching in ATSGenerator#281
⚡ Bolt: Optimize regex and caching in ATSGenerator#281
Conversation
Hoist string operations and regular expressions out of iterative structures inside `cli/generators/ats_generator.py` to prevent repeated resource allocations. What: Pre-compile multiple regular expressions and a static list of action verbs as module-level constants, and cache lowercased string text before generator comprehensions. Why: Previously, regular expressions were compiled on every invocation, and string lowercasing (`all_text.lower()`) was triggered inside a generator expression for every single item in the action verbs list, causing O(N) operations. Impact: Significant reduction in CPU cycles and memory allocations when parsing resumes for ATS scores, avoiding repeated processing of large text strings. Measurement: Compare parsing speeds or CPU profile metrics on `ats_generator.py` for large resumes; tests confirm output remains deterministic and unchanged. Co-authored-by: anchapin <[email protected]>
|
👋 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. |
Reviewer's GuidePrecompiles commonly used regular expressions and the action verb list in ats_generator as module-level constants, and reuses a single lowercased resume text string to reduce redundant allocations and regex compilation while preserving behavior. Sequence diagram for optimized _check_readability flow in ATSGeneratorsequenceDiagram
participant ATSGenerator
participant ResumeData
participant RegexPatterns
ATSGenerator->>ResumeData: _get_all_text(resume_data)
ResumeData-->>ATSGenerator: all_text
ATSGenerator->>ATSGenerator: all_text_lower = all_text.lower()
ATSGenerator->>RegexPatterns: access _ACTION_VERBS
RegexPatterns-->>ATSGenerator: action_verbs
ATSGenerator->>ATSGenerator: action_verb_count = sum(verb in all_text_lower)
ATSGenerator->>RegexPatterns: _QUANTIFIABLE_PATTERN.search(all_text)
RegexPatterns-->>ATSGenerator: has_numbers
ATSGenerator->>RegexPatterns: _ACRONYM_PATTERN.findall(all_text)
RegexPatterns-->>ATSGenerator: acronyms
ATSGenerator-->>ATSGenerator: update details and suggestions based on checks
Flow diagram for module-level regex caching in ATSGeneratorflowchart TD
A["ats_generator module import"] --> B["Compile _TABLE_PATTERN, _SPECIAL_CHARS_PATTERN, _EMAIL_PATTERN, _PHONE_PATTERN, _QUANTIFIABLE_PATTERN, _ACRONYM_PATTERN, _JSON_ARRAY_PATTERN, _TECH_TERM_PATTERN, _SUMMARY_TERM_PATTERN once"]
B --> C["Define _ACTION_VERBS list once"]
C --> D["Instantiate ATSGenerator"]
D --> E["_check_format_parsing"]
E --> E1["Use _TABLE_PATTERN.search(all_text)"]
E --> E2["Use _SPECIAL_CHARS_PATTERN.findall(all_text)"]
D --> F["_check_contact_info"]
F --> F1["Use _EMAIL_PATTERN.search(contact_email)"]
F --> F2["Use _PHONE_PATTERN.search(contact_phone)"]
D --> G["_check_readability"]
G --> G1["all_text = _get_all_text(resume_data)"]
G1 --> G2["all_text_lower = all_text.lower()"]
G2 --> G3["count verbs from _ACTION_VERBS in all_text_lower"]
G1 --> G4["Use _QUANTIFIABLE_PATTERN.search(all_text)"]
G1 --> G5["Use _ACRONYM_PATTERN.findall(all_text)"]
D --> H["_extract_job_keywords"]
H --> H1["Use _JSON_ARRAY_PATTERN.search(response)"]
D --> I["_extract_resume_keywords"]
I --> I1["Use _TECH_TERM_PATTERN.findall(text)"]
I --> I2["Use _SUMMARY_TERM_PATTERN.findall(summary.lower())"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="cli/generators/ats_generator.py" line_range="47" />
<code_context>
+_PHONE_PATTERN = re.compile(r"\d")
+_QUANTIFIABLE_PATTERN = re.compile(r"\d+%|\$\d+|\d+\s*(?:users|customers|projects)", flags=re.IGNORECASE)
+_ACRONYM_PATTERN = re.compile(r"\b[A-Z]{2,4}\b")
+_JSON_ARRAY_PATTERN = re.compile(r"\[.*\]", flags=re.DOTALL)
+_TECH_TERM_PATTERN = re.compile(r"\b[a-z]+(?:\s+[a-z]+)?\b")
+_SUMMARY_TERM_PATTERN = re.compile(r"\b[a-z]{2,}\b")
</code_context>
<issue_to_address>
**suggestion:** Make JSON array extraction regex non-greedy to avoid overmatching
With `DOTALL`, `r"\[.*\]"` will match from the first `[` to the last `]` in the entire response. If the model returns multiple bracketed sections or other text containing `[`/`]`, `json_match.group(0)` may be much larger than intended and break `json.loads` or parse the wrong data. A non-greedy pattern like `r"\[.*?\]"` limits the match to the smallest bracketed JSON array, which is likely what we want.
```suggestion
_JSON_ARRAY_PATTERN = re.compile(r"\[.*?\]", flags=re.DOTALL)
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| _PHONE_PATTERN = re.compile(r"\d") | ||
| _QUANTIFIABLE_PATTERN = re.compile(r"\d+%|\$\d+|\d+\s*(?:users|customers|projects)", flags=re.IGNORECASE) | ||
| _ACRONYM_PATTERN = re.compile(r"\b[A-Z]{2,4}\b") | ||
| _JSON_ARRAY_PATTERN = re.compile(r"\[.*\]", flags=re.DOTALL) |
There was a problem hiding this comment.
suggestion: Make JSON array extraction regex non-greedy to avoid overmatching
With DOTALL, r"\[.*\]" will match from the first [ to the last ] in the entire response. If the model returns multiple bracketed sections or other text containing [/], json_match.group(0) may be much larger than intended and break json.loads or parse the wrong data. A non-greedy pattern like r"\[.*?\]" limits the match to the smallest bracketed JSON array, which is likely what we want.
| _JSON_ARRAY_PATTERN = re.compile(r"\[.*\]", flags=re.DOTALL) | |
| _JSON_ARRAY_PATTERN = re.compile(r"\[.*?\]", flags=re.DOTALL) |
Hoist string operations and regular expressions out of iterative structures inside `cli/generators/ats_generator.py` to prevent repeated resource allocations. Added a fix for formatting to pass CI (black checks). What: Pre-compile multiple regular expressions and a static list of action verbs as module-level constants, and cache lowercased string text before generator comprehensions. Additionally, ran `black` on the modified file to pass CI pipeline code formatting requirements. Why: Previously, regular expressions were compiled on every invocation, and string lowercasing (`all_text.lower()`) was triggered inside a generator expression for every single item in the action verbs list, causing O(N) operations. The CI lint check failed because `black` wasn't run on the modified file to enforce formatting. Impact: Significant reduction in CPU cycles and memory allocations when parsing resumes for ATS scores, avoiding repeated processing of large text strings. Fixes a CI failure related to code formatting. Measurement: Compare parsing speeds or CPU profile metrics on `ats_generator.py` for large resumes; tests confirm output remains deterministic and unchanged. The CI should pass now. Co-authored-by: anchapin <[email protected]>
💡 What: Pre-compile multiple regular expressions and a static list of action verbs as module-level constants in$O(1)$ regarding the loops instead of $O(N)$ with the collection size.
cli/generators/ats_generator.py. Furthermore, cache the lowercased version of the entire resume text before invoking the generator expression to parse action verbs.🎯 Why: Previously, regular expressions were compiled on every invocation, and string lowercasing (
all_text.lower()) was triggered inside a generator expression for every single item in the action verbs list, resulting in redundant O(N) string allocations and compilation steps inside the execution paths.📊 Impact: This significantly reduces CPU cycles and string allocations during ATS score generation, especially on longer resumes. It ensures string manipulation is linear
🔬 Measurement: Verified through the existing test suite (using
python -m pytest tests/test_ats_generator.py), confirming that no deterministic output logic changed. Running standard profiling againstATSGeneratoron a large text blob should display a measurable performance boost.PR created automatically by Jules for task 10442857480046678864 started by @anchapin
Summary by Sourcery
Optimize ATS generator performance by reusing compiled regular expressions and shared constants for common text and keyword checks.
Enhancements: