Skip to content

⚡ Bolt: Pre-compile regex patterns in keyword density tool#209

Open
anchapin wants to merge 1 commit intomainfrom
bolt-keyword-density-regex-optimization-284781109145590586
Open

⚡ Bolt: Pre-compile regex patterns in keyword density tool#209
anchapin wants to merge 1 commit intomainfrom
bolt-keyword-density-regex-optimization-284781109145590586

Conversation

@anchapin
Copy link
Copy Markdown
Owner

@anchapin anchapin commented Mar 25, 2026

💡 What: Extracted the literal regex patterns for finding job titles and companies in cli/utils/keyword_density.py and pre-compiled them as module-level constants (_TITLE_PATTERNS and _COMPANY_PATTERNS).
🎯 Why: When analyzing job descriptions for keyword density, the _extract_job_details function is called and previously it compiled these regex objects every single time. Compiling them once when the module is imported avoids redundant processing overhead.
📊 Impact: Reduces regex recompilation overhead to near-zero for job detail extraction, making keyword density reports slightly faster to generate.
🔬 Measurement: Run the keyword density generation on large or multiple job descriptions. Local tests verify that the exact same matching logic is preserved while eliminating unnecessary re.compile() steps.


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

Summary by Sourcery

Enhancements:

  • Introduce module-level pre-compiled regex pattern lists for job title and company extraction in the keyword density tool and update extraction logic to use them for improved performance.

Moves title and company extraction patterns to module-level constants in `cli/utils/keyword_density.py` to prevent repeated regex compilation during analysis.

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 Mar 25, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Pre-compiles the regex patterns used for extracting job titles and company names in the keyword density tool so they are compiled once at import instead of on every call to _extract_job_details, preserving behavior while improving performance.

Class diagram for pre-compiled regex usage in keyword density module

classDiagram
    class keyword_density_py {
        +list _TITLE_PATTERNS
        +list _COMPANY_PATTERNS
        +tuple _extract_job_details(job_description: str)
    }

    class _TITLE_PATTERNS {
        +Pattern pattern_1
        +Pattern pattern_2
        +Pattern pattern_3
    }

    class _COMPANY_PATTERNS {
        +Pattern pattern_1
        +Pattern pattern_2
    }

    keyword_density_py "1" o-- "1" _TITLE_PATTERNS : contains
    keyword_density_py "1" o-- "1" _COMPANY_PATTERNS : contains

    keyword_density_py ..> _TITLE_PATTERNS : uses_in__extract_job_details
    keyword_density_py ..> _COMPANY_PATTERNS : uses_in__extract_job_details
Loading

File-Level Changes

Change Details Files
Pre-compile job title extraction regexes as module-level constants and update usage to call search on compiled patterns.
  • Introduce a _TITLE_PATTERNS list containing compiled regex objects for the previously inline title-matching patterns.
  • Move the regex flags (IGNORECASE and MULTILINE) into the compilation step instead of passing them to re.search.
  • Update _extract_job_details to iterate over _TITLE_PATTERNS and call pattern.search(job_description) instead of re.search with a string pattern.
cli/utils/keyword_density.py
Pre-compile company extraction regexes as module-level constants and update usage to call search on compiled patterns.
  • Introduce a _COMPANY_PATTERNS list containing compiled regex objects for the previously inline company-matching patterns.
  • Move the IGNORECASE flag into the compilation step instead of passing it to re.search.
  • Update _extract_job_details to iterate over _COMPANY_PATTERNS and call pattern.search(job_description) instead of re.search with a string pattern.
cli/utils/keyword_density.py

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 left some high level feedback:

  • The first company regex pattern was previously case-sensitive and is now compiled with re.IGNORECASE, which subtly changes matching behavior; confirm this is intentional or adjust the flags to preserve existing semantics.
  • Since _TITLE_PATTERNS and _COMPANY_PATTERNS are intended as constants, consider using tuples instead of lists to better convey their immutability and avoid accidental modification.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The first company regex pattern was previously case-sensitive and is now compiled with `re.IGNORECASE`, which subtly changes matching behavior; confirm this is intentional or adjust the flags to preserve existing semantics.
- Since `_TITLE_PATTERNS` and `_COMPANY_PATTERNS` are intended as constants, consider using tuples instead of lists to better convey their immutability and avoid accidental modification.

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.

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