Skip to content

Micro optimization: avoid recompiling regex in incremental_coverage.py#7953

Closed
mhucka wants to merge 2 commits intoquantumlib:mainfrom
mhucka:precompile-regex
Closed

Micro optimization: avoid recompiling regex in incremental_coverage.py#7953
mhucka wants to merge 2 commits intoquantumlib:mainfrom
mhucka:precompile-regex

Conversation

@mhucka
Copy link
Contributor

@mhucka mhucka commented Mar 12, 2026

The function determine_ignored_lines() in dev_tools/incremental_coverage.py compiled a regex. Compiling regex inside a function that is potentially called multiple times adds unnecessary overhead. This PR pre-compiles some regex patterns at the module level in dev_tools/incremental_coverage.py to avoid redundant compilation.

In simple wall-clock timings on a linux VM, running dev_tools/pytest-and-incremental-coverage, I get an average of about 10% time improvement over the unmodified version of the file.

(Full disclosure: this was largely the work of Jules, which I've experimenting with.)

Compiling regex inside a function that is potentially called multiple
times adds unnecessary overhead.
@mhucka mhucka requested review from a team and vtomole as code owners March 12, 2026 05:45
@mhucka mhucka requested a review from senecameeks March 12, 2026 05:45
@github-actions github-actions bot added the size: S 10< lines changed <50 label Mar 12, 2026
@mhucka mhucka requested a review from pavoljuhas March 12, 2026 05:46
@codecov
Copy link

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.62%. Comparing base (6689231) to head (cbe4c83).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
dev_tools/incremental_coverage.py 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7953      +/-   ##
==========================================
- Coverage   99.63%   99.62%   -0.01%     
==========================================
  Files        1108     1108              
  Lines       99571    99574       +3     
==========================================
- Hits        99205    99204       -1     
- Misses        366      370       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

After adding a blank line to every Python source in this repo, the wall time before and with this PR for

python dev_tools/check_incremental_coverage_annotations.py origin/main

changed from 9.1 to 8.9 s. This should be considered in context of a preceding pytest coverage run needed to create .cover files which takes about 5 minutes. The improvement in time is at this scale negligible; indeed re.search caches its compiled patterns so any difference is likely from cache lookup overhead.

The other possible benefit of this PR could be in making the code more readable, but it goes the opposite way by adding more module constants. The level of improvement here does not justify more work.

@mhucka mhucka closed this Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: S 10< lines changed <50

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants