Skip to content

Stop matching the :nocov: regex twice on every classified line - #1253

Merged
sferik merged 1 commit into
simplecov-ruby:mainfrom
andriytyurnikov:lines-classifier-single-pass
Aug 7, 2026
Merged

sferik merged 1 commit into
simplecov-ruby:mainfrom
andriytyurnikov:lines-classifier-single-pass

Conversation

@andriytyurnikov

Copy link
Copy Markdown
Contributor

Follow-on to #1252, on the same per-process path. Skipping the branch and method synthesis took the bulk of the cost out of a line-coverage-only run; classification is what remains, and unlike the synthesis it is paid under every configuration.

LinesClassifier#classify matched the :nocov: regex twice for every line — once to toggle skipping, once inside not_relevant_line?:

lines.map.with_index(1) do |line, line_number|
  skipping = !skipping if self.class.no_cov_line?(line)          # match 1
  not_relevant_line?(line, line_number, skipping, directive_disabled)  # match 2
end

A :nocov: marker is always a comment, so the cheaper whitespace-or-comment test now gates the token match, and a line of real code — most of a source file — no longer pays for it at all.

Measured over SimpleCov's own lib/ (104 files, 8,845 lines), median of three runs:

classify alone                48.31 -> 43.36 ms   -10%
simulate, line coverage only  67.58 -> 60.09 ms   -11%
simulate, synthesis enabled   85.30 -> 79.41 ms    -7%

Classification output is unchanged; the existing specs pin it.

benchmarks/simulate_coverage.rb is new. Nothing measured this path before — only collate and Result had benchmarks, so the per-process work at exit was invisible.

Two smaller things in here:

The rescue in LinesClassifier.no_cov_line? is no longer reachable from classify, since an invalid byte sequence now fails the whitespace test first. SourceFile::SkipChunks still calls it directly on raw source, so it keeps its rescue and gains a direct spec.

One spec added to simulate_coverage_spec.rb: synthesize: false skips the Prism parse outright rather than parsing and discarding. The existing specs assert the tuples come back empty, which parsing-then-discarding would also satisfy — the point of the flag is that the parse never happens.

Refs #1250.

SimpleCov.result simulates coverage at exit for every file a track_files
or cover glob matched that the process never loaded. It is per-process
work, so a parallel run pays it once per worker, and since each worker
loads only a fraction of the code, nearly every worker simulates nearly
the whole project. Skipping the branch and method synthesis takes the
bulk of that out for a line-coverage-only run; classification is what
remains, and it is paid under every configuration.

LinesClassifier#classify ran the :nocov: regex twice per line: once to
toggle skipping and once inside not_relevant_line?. A marker is always a
comment, so the cheaper whitespace-or-comment test now gates the token
match, and a line of real code -- most of a source file -- no longer
pays for it at all.

Measured over SimpleCov's own lib/ (104 files, 8,845 lines), median of
three runs each:

  classify alone                    48.31 -> 43.36 ms   -10%
  simulate, line coverage only      67.58 -> 60.09 ms   -11%
  simulate, synthesis enabled       85.30 -> 79.41 ms    -7%

benchmarks/simulate_coverage.rb covers this path, which had no benchmark
before -- only collate and Result did, so nothing measured the
per-process work at exit.

The rescue in LinesClassifier.no_cov_line? is no longer reachable from
classify, since an invalid byte sequence now fails the whitespace test
first. SourceFile::SkipChunks still calls it directly on raw source, so
it keeps its rescue and gains a direct spec.

The spec added alongside pins that `synthesize: false` skips the Prism
parse outright rather than parsing and discarding it, which is the claim
behind that flag.

This comment was marked as resolved.

@sferik
sferik merged commit 7c1e6ba into simplecov-ruby:main Aug 7, 2026
10 checks passed
sferik added a commit to andriytyurnikov/simplecov that referenced this pull request Aug 7, 2026
Only CHANGELOG.md conflicted, where this branch and
simplecov-ruby#1253 each appended a
bullet to the Unreleased Performance section.

Kept both. The classification bullet goes first because it reads "once
the synthesis above is skipped" and has to stay next to the synthesis
bullet it refers to. The merge-side bullet follows, which also leaves the
section running per-process synthesis, then per-process classification,
then merging.

The code merged clean: main's two new commits touch only
lib/simplecov/lines_classifier.rb and its spec, which this branch does
not. Full rake is green on the result.
@andriytyurnikov
andriytyurnikov deleted the lines-classifier-single-pass branch August 7, 2026 20:50
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.

3 participants