Skip to content

Cut unloaded-file injection cost, fix 100% coverage in merged reports - #1252

Merged
sferik merged 2 commits into
mainfrom
unloaded-file-injection-cost-and-merge-flags
Aug 7, 2026
Merged

sferik merged 2 commits into
mainfrom
unloaded-file-injection-cost-and-merge-flags

Conversation

@sferik

@sferik sferik commented Aug 7, 2026 •

Copy link
Copy Markdown
Collaborator

Two changes from #1250, one per commit:

  1. Skip synthesizing branch and method tuples nothing will read

Simulating a tracked-but-unloaded file statically enumerates its branch and method tuples so unloaded files contribute to those denominators symmetrically with their lines (#1059). The Prism parse behind that is over half the cost of simulating a file, and it ran unconditionally, including for the default line-coverage-only configuration where Combine::FilesCombiner and SourceFile::Statistics both discard the tuples unread. It is paid once per tracked file in every process, which is why it shows up on large parallel runs.

SimulateCoverage.call now takes a synthesize: keyword (defaulting to true, so it stays a pure function of the file), and inject_unloaded_files passes branch_coverage? || method_coverage?. The criterion decision lives at the call site.

Measured end to end through add_not_loaded_files on a 400 file, 26,000 line synthetic project:

configuration injection cost
line coverage only (default), before 0.526 ms/file
line coverage only (default), after 0.222 ms/file
branch or method coverage enabled unchanged

Line classification is identical either way.

  1. Flag never-loaded files in merged results

SourceFile::Statistics reports 0% rather than a misleading 100% when a never-loaded file has no branch or method data (#902), but the rule keys off a loaded: flag that only the single-process path ever set. ResultMerger.create_result built its Result without not_loaded_files, which defaults to an empty Set, so every file in a merged report claimed to have been loaded and the rule could never fire there.

The flag cannot simply be carried across, because Result#to_hash writes only coverage and a timestamp, so it never reaches .resultset.json. The merged result now re-derives it from the merged line counts using the same "did any line execute" signal Combine::FilesCombiner already reconciles on.

A tracked file that no process loaded and that contains no conditionals, say a constants file caught by a cover glob:

before:  constants.rb  not_loaded?=false  branch=100.0%  method=100.0%
after:   constants.rb  not_loaded?=true   branch=0.0%    method=0.0%
         loaded.rb     not_loaded?=false  branch=50.0%   method=100.0%   (unchanged)

Only branchless files exposed this, since #1059's synthesized tuples already give anything containing a conditional a real 0%, which is likely why it went unnoticed.

Line counts only carry that signal when the run produces them. A branch-only or method-only run gets none from Coverage for the files it loaded, so judging on their absence would mark every file in the report not loaded and report 0% for the branchless ones. SimulateCoverage now omits line data under those criteria too, mirroring Coverage, so a simulated file cannot be mistaken for one a sibling process actually loaded once the two merge. Nothing is flagged under those configurations rather than misflagged, which leaves them exactly as they are on main. This adds a SimpleCov.line_coverage? predicate alongside branch_coverage? and method_coverage?, counting :oneshot_line because ResultAdapter turns its executed-line list back into a line array.

The second commit closes a hole the first would otherwise open. Gating the synthesis keeps synthesized tuples out of resultsets written by line-coverage-only runs, and a later collate with branch coverage enabled would then find those files empty. On the old merge path, where the #902 rule is dead, they would report 100%. With the flag re-derived they correctly report 0%.

Each worker still simulates every tracked file it did not personally load, so the work stays O(N) in worker count. That is the larger architectural change discussed in the issue, and it is worth re-measuring first: after this PR a default run pays roughly 0.22 ms/file instead of 0.47, so the remaining headroom is smaller than the issue's figures suggest.

@sferik
sferik requested a balanced review from Copilot August 7, 2026 12:12

This comment was marked as resolved.

sferik added 2 commits August 7, 2026 08:41
Simulating a tracked-but-unloaded file statically enumerates its branch
and method tuples (#1059), and the Prism parse behind that is over half
the cost of simulating a file. It ran even for the default
line-coverage-only configuration, where Combine::FilesCombiner and the
statistics both drop the tuples unread, and it is paid once per tracked
file in every process, which is why it shows up on large parallel runs.

SimulateCoverage.call now takes a synthesize: keyword and the injection
site passes branch_coverage? || method_coverage?. On a 400 file
synthetic project the default configuration's injection pass goes from
0.526 to 0.222 ms per file. Line classification is identical either way,
and suites with either criterion enabled are unaffected.

Reported with measurements by @andriytyurnikov.

Refs #1250.
SourceFile::Statistics reports 0% rather than a misleading 100% when a
never-loaded file has no branch or method data (#902), but the rule keys
off a loaded: flag that only the single-process path set.
ResultMerger.create_result omitted not_loaded_files, so every file in a
merged report claimed to have been loaded and the rule never fired
there. A tracked file no process loaded and that contains no
conditionals, say a constants file caught by a cover glob, reported 100%
branch and method coverage. Only branchless files exposed it, since
#1059's synthesized tuples already give anything with a conditional a
real 0%.

The flag cannot be carried across, since Result#to_hash writes only
coverage and a timestamp, so the merged result re-derives it from the
merged line counts using the same "did any line execute" signal
Combine::FilesCombiner reconciles on. This also matters for the previous
commit, which keeps synthesized tuples out of resultsets written by
line-coverage-only runs: a later collate with branch coverage enabled
now reports those files as 0% rather than 100%.

Line counts only carry that signal when the run produces them. A
branch-only or method-only run gets none from Coverage for the files it
loaded, so judging on their absence would mark every file in the report
not loaded and report 0% for the branchless ones. SimulateCoverage now
omits line data under those criteria too, mirroring Coverage, which
keeps a simulated file from looking like one a sibling process loaded
once the two merge. Nothing is flagged there rather than misflagged.

Reported with an exemplary diagnosis by @andriytyurnikov.

Refs #1250.
@sferik
sferik force-pushed the unloaded-file-injection-cost-and-merge-flags branch from e48b443 to 813cecf Compare August 7, 2026 12:45
@sferik
sferik requested a balanced review from Copilot August 7, 2026 12:47

This comment was marked as resolved.

@sferik
sferik merged commit b05951c into main Aug 7, 2026
21 checks passed
@sferik
sferik deleted the unloaded-file-injection-cost-and-merge-flags branch August 12, 2026 19:37
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.

2 participants