Skip to content

Inject unloaded files once at the merge point, not once per process - #1255

Merged
sferik merged 3 commits into
mainfrom
prototype-merge-point-injection
Aug 7, 2026
Merged

sferik merged 3 commits into
mainfrom
prototype-merge-point-injection

Conversation

@sferik

@sferik sferik commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

#1252 made each simulation cheaper. This stops them happening N times.

inject_unloaded_files skips only the files the current process loaded, so every worker in a parallel run simulates nearly the whole project and the merge discards all but one copy of each. Only the union of what every process loaded says what was really never loaded, so the injection now happens once, in ResultMerger.create_result.

Over 400 tracked files with 40 of them loaded by no worker, under line plus branch plus method coverage:

workers per-process (before) merge-point (after) simulations
1 0.026s 0.024s once 40 → 40
2 0.208s over 2 workers 0.024s once 440 → 40
4 0.600s over 4 workers 0.025s once 1240 → 40
8 1.368s over 8 workers 0.025s once 2840 → 40
16 2.904s over 16 workers 0.025s once 6040 → 40

Flat in worker count rather than linear. A single process, and any run with merging false, pays what it paid before. Per-worker resultsets also shrink from 13.4MB to 800KB at 16 workers, since they no longer each carry a simulated copy of the whole project.

Deferring the injection alone is not enough, and this is the part worth reviewing closely. The merge needs to know which files were tracked, and the cover / track_files configuration lives in whichever process ran SimpleCov.start. A standalone SimpleCov.collate never ran it, so it has no glob to expand. Collating the same resultsets in a process with no glob configured:

without tracked paths in resultset   files= 90  not_loaded=  0  line=36.88%
with tracked paths in resultset      files=400  not_loaded=310  line= 8.30%

Forty untested files silently vanish and coverage inflates. So each process now records the paths it was told to track into its resultset, and the merge unions them. Discovery costs one Dir.glob per process, 1.26ms over 400 files, against the simulation it replaces.

The key is omitted when empty, and absent from resultsets written by earlier versions. Those already carry the files their process injected, and injection skips paths already present, so they merge unchanged.

sferik added 2 commits August 7, 2026 14:39
Discovering tracked files and simulating the ones that were never loaded
lived as private helpers on the SimpleCov singleton, reading root, globs
and criteria straight off it. The next commit moves the injection to the
merge step, which runs on behalf of processes whose configuration it does
not share, so the logic has to take what it needs as arguments.

SimpleCov::UnloadedFileInjector now owns discovery and injection.
SimpleCov.inject_unloaded_files is the seam the merger will call, public
for that reason and marked @api private. No behaviour changes: every
process still injects its own unloaded files exactly as before.
inject_unloaded_files skips only what the current process loaded, so
every worker in a parallel run simulated nearly the whole project and the
merge discarded all but one copy of each file. Only the union of what
every process loaded says what was really never loaded, so the injection
now happens in ResultMerger.create_result. Over 400 tracked files with 40
loaded by no worker, a 16-worker run goes from 6040 simulations taking
3.15s across the workers to 40 taking 0.024s once, flat in worker count.
A single process, and any run with merging false, pays what it did
before, and resultsets shrink from 13.4MB to 800KB at 16 workers.

The merge needs to know which files were tracked, and that configuration
lives in whichever process ran SimpleCov.start. A standalone collate
never ran it, so deferring alone would have left it with no glob to
expand and silently dropped every untested file: 90 files at an inflated
36.88% where there should be 400 at 8.30%. Each process therefore records
the paths it was told to track into its resultset, and the merge unions
those. Discovery costs a Dir.glob per process, 1.26ms over 400 files,
against the simulation it replaces. The key is omitted when empty and
absent from resultsets written by earlier versions, which already carry
the files their process injected and so merge unchanged.

Merged coverage and the not-loaded set are identical at every worker
count. Knowing exactly which files were injected also supersedes the
line-count heuristic for them, so a never-loaded branchless file is now
flagged under a branch-only run, which that heuristic cannot do because
it has no line data to judge by.

ResultMerger::UnloadedFiles holds the merge half. ResultMerger.valid_results
went unused and is removed.

Refs #1250.
@sferik
sferik requested a balanced review from Copilot August 7, 2026 18:58

This comment was marked as resolved.

Four places where deferring injection left the merge reading state that
belonged to whichever process produced the resultset. Same shape as the
glob problem the tracked-path list already solved, so they are fixed the
same way: what the producer knew goes in the resultset, and the merge
derives the rest from the data it is merging.

A process records what it was told to track, did not load, and would have
reported. Two things were leaking. Result#to_hash serializes coverage
after filtering, so a file the process loaded and then filtered out was
absent from the stored coverage while still listed as tracked, and the
merge simulated it back in as never loaded. And a tracked file the
process never loaded but its own filters exclude was recorded too, so a
merge that does not share those filters reported a file the producer
deliberately left out. Filters that decide from the path alone are now
applied when recording. A block filter is handed the source file and may
consult coverage that does not exist yet, so those still fall to the
merging process, which is why Filter#path_only? defaults to false.

Injection read branch_coverage? and friends off the merging process,
which for `simplecov merge` never ran SimpleCov.start at all. An injected
file given fewer tables than the files beside it is what inflates the
percentage #1059 fixed, so the criteria now come from the merged data.
The configuration is consulted only when the merge carried no files and
there is nothing to be consistent with.

Tracked paths were collected before drop_expired_results, so a resultset
dropped for being past merge_timeout still contributed paths and a file
only a stale run ever tracked was simulated in at 0%. merge_valid_results
now yields the surviving entries, and both merge entry points collect
from those.

Each fix has a spec that fails against the behaviour it replaces.
@sferik
sferik force-pushed the prototype-merge-point-injection branch from f5078ae to b7bfde4 Compare August 7, 2026 19:20
@sferik
sferik requested a balanced review from Copilot August 7, 2026 19:20

This comment was marked as resolved.

@sferik
sferik merged commit c231814 into main Aug 7, 2026
21 checks passed
@sferik
sferik deleted the prototype-merge-point-injection branch August 12, 2026 19:51
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