Skip to content

⚡ Bolt: Pre-compile regex constants for job parsing#324

Open
anchapin wants to merge 1 commit into
mainfrom
bolt-precompile-regex-job-parser-11625549520871511255
Open

⚡ Bolt: Pre-compile regex constants for job parsing#324
anchapin wants to merge 1 commit into
mainfrom
bolt-precompile-regex-job-parser-11625549520871511255

Conversation

@anchapin
Copy link
Copy Markdown
Owner

@anchapin anchapin commented May 28, 2026

💡 What: Extracted 20+ dynamically compiled regular expressions inside JobParser methods (like _extract_salary_from_text, _parse_generic, _extract_job_type) into module-level re.Pattern constants. Refactored helper methods like _extract_text_by_pattern to support Union[str, re.Pattern].
🎯 Why: In bulk parsing scenarios, creating and destroying regex engine states via re.search and re.compile within loops and multiple function calls creates measurable and unnecessary overhead. Pre-compiling them guarantees the parser pays the regex initialization cost only once at module load.
📊 Impact: Reduces CPU allocation and string parsing time inside hot loops for every job parsing request.
🔬 Measurement: Verify tests (python -m pytest tests/test_job_parser_integration.py) pass and use cProfile on a bulk list of job postings to observe the reduction in re.compile calls.


PR created automatically by Jules for task 11625549520871511255 started by @anchapin

Summary by Sourcery

Pre-compile frequently used regular expressions in the job parser to reduce repeated compilation overhead and improve performance when parsing job postings.

Enhancements:

  • Refactor job parsing helpers to accept both raw regex strings and pre-compiled re.Pattern objects for reuse across parsing routines.
  • Replace inline regex usage in salary extraction, section parsing, bullet extraction, job type, and experience level detection with shared module-level compiled patterns.

Documentation:

  • Document the performance learning and best practice of pre-compiling regexes in parser loops in the Bolt knowledge file.

Co-authored-by: anchapin <6326294+anchapin@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.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 28, 2026

Reviewer's Guide

Pre-compiles all frequently used regexes in JobParser into module-level patterns and refactors parsing helpers to accept and use these compiled patterns, reducing repeated compilation in hot parsing paths while documenting the performance guideline in the Bolt playbook.

File-Level Changes

Change Details Files
Pre-compile all job-parsing regexes as module-level constants and update parsing logic to use them instead of recompiling per call.
  • Introduce named module-level re.Pattern constants for Indeed header, position cleanup, salary extraction, requirements/responsibilities headings and sections, bullet/comma list parsing, job type, and experience level detection.
  • Replace inline re.compile/re.search usage in _parse_indeed, _parse_generic, _extract_salary_from_text, _extract_sections_from_description, _extract_items_from_text, _extract_job_type, and _extract_experience_level with the new compiled pattern constants and their .search/.sub/.findall/.split methods.
  • Add small type: ignore annotations where BeautifulSoup APIs expect strings to satisfy type checkers when passing compiled patterns.
cli/integrations/job_parser.py
Generalize helper methods to support both raw pattern strings and pre-compiled regex objects for reuse.
  • Change _extract_text_by_pattern signature to accept Union[str, re.Pattern] and branch between pattern.search for compiled patterns and re.search with re.IGNORECASE for string patterns.
  • Change _extract_list_by_keyword to accept Union[str, re.Pattern], compiling strings to re.Pattern internally, and then use BeautifulSoup.find_all with the resulting compiled regex.
cli/integrations/job_parser.py
Document the regex pre-compilation guideline for parser integrations in the Bolt performance notes.
  • Add a new dated entry to .jules/bolt.md describing the cost of regex compilation in parser loops and recommending module-level pre-compilation and helper refactors as a standard practice.
.jules/bolt.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • Instead of using re.Pattern with multiple # type: ignore[call-overload]s, consider annotating the compiled constants as Pattern[str] (from typing or re in 3.11+) and adjusting the BeautifulSoup helper signatures so that soup.find(..., string=...) accepts the correctly typed pattern without suppressing type checking.
  • For helpers like _extract_text_by_pattern and _extract_list_by_keyword, it may be clearer to expose two overloads (one for str, one for Pattern[str]) rather than Union[str, Pattern], which would make call sites and type checking more precise and could eliminate the need for runtime isinstance branching.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Instead of using `re.Pattern` with multiple `# type: ignore[call-overload]`s, consider annotating the compiled constants as `Pattern[str]` (from `typing` or `re` in 3.11+) and adjusting the BeautifulSoup helper signatures so that `soup.find(..., string=...)` accepts the correctly typed pattern without suppressing type checking.
- For helpers like `_extract_text_by_pattern` and `_extract_list_by_keyword`, it may be clearer to expose two overloads (one for `str`, one for `Pattern[str]`) rather than `Union[str, Pattern]`, which would make call sites and type checking more precise and could eliminate the need for runtime `isinstance` branching.

## Individual Comments

### Comment 1
<location path="cli/integrations/job_parser.py" line_range="844-847" />
<code_context>
-
-        for pattern in patterns:
-            match = re.search(pattern, html, re.IGNORECASE)
+        for pattern in _JOB_TYPE_PATTERNS:
+            match = pattern.search(html)
             if match:
                 return match.group(1).lower().replace("-", "-")

</code_context>
<issue_to_address>
**issue (bug_risk):** The `replace("-", "-")` call is a no-op and likely not doing what was intended.

This makes the line equivalent to `match.group(1).lower()`. If you meant to normalize separators (e.g., turn spaces into hyphens or collapse multiple hyphens), please update the `replace` to the intended mapping, such as `.replace(" ", "-")` or another appropriate transformation.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +844 to 847
for pattern in _JOB_TYPE_PATTERNS:
match = pattern.search(html)
if match:
return match.group(1).lower().replace("-", "-")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): The replace("-", "-") call is a no-op and likely not doing what was intended.

This makes the line equivalent to match.group(1).lower(). If you meant to normalize separators (e.g., turn spaces into hyphens or collapse multiple hyphens), please update the replace to the intended mapping, such as .replace(" ", "-") or another appropriate transformation.

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