Skip to content

Absorb resultsets into one accumulator instead of folding pairwise - #1254

Merged
sferik merged 1 commit into
simplecov-ruby:mainfrom
andriytyurnikov:merge-resultsets-accumulator
Aug 7, 2026
Merged

sferik merged 1 commit into
simplecov-ruby:mainfrom
andriytyurnikov:merge-resultsets-accumulator

Conversation

@andriytyurnikov

Copy link
Copy Markdown
Contributor

Merging resultsets is most of what collate spends its time on for a large parallel CI run. This is the merge side of #1250, independent of the per-process injection discussed there.

Results were folded together pairwise, so every one of the N-1 steps rebuilt the whole accumulated structure: a fresh outer file hash, a fresh lines array for every file, and a fresh branch/method table for every file whose keys were re-interned from their tuples each time. A 160-worker run over ~1,800 files does ~290,000 whole-file rebuilds to produce ~1,800 files of output.

The accumulated side is private to the fold, so it can be updated in place instead. Combine::CoverageAccumulator owns its state, absorbs one resultset at a time, and turns the interned tables back into tuple-keyed hashes once, at the end.

On the repository's benchmarks/collate.rb fixture — 160 resultsets, 1,836 files, 147,875 lines, branch coverage on:

phase           seconds  vs baseline
merge             3.38s      -42.3%
build_result      0.01s      -82.6%
format            0.46s      -35.5%
total             3.87s      -41.8%

peak RSS:  0.35 GB -> 0.34 GB
files reported: 1836 (unchanged)

Memory is the constraint this had to respect. merge_results deliberately reads and parses one file at a time — 100s of CI jobs each producing megabytes is gigabytes if read together — and the accumulator keeps that property: fold only ever iterates its pairs, and absorb_results hands it a lazy enumerable. Peak RSS on the benchmark is unchanged, which is the number I'd most want checked.

reconcile_synthesized (the 1.0.2 defensive layer, and the signal #1252's never_executed_files now re-derives on) is preserved exactly, including the detail that the accumulated side's executed-ness is re-read on every absorb, because folding in an executed resultset can flip it — which is what it did between two steps of the pairwise fold. CoverageAccumulator.executed? is the single definition of that signal, shared with ResultMerger.

Two fast paths are kept: a file only one resultset carried is returned as-is rather than copied, and merge_coverage still short-circuits on zero and one result.

SimpleCov::Combine::FilesCombiner and SimpleCov::Combine.combine are gone, their roles taken by the accumulator. Both were internal API — flagging it explicitly since it is the part of this that isn't purely internal-facing. spec/combine/files_combiner_spec.rb is replaced by spec/combine/coverage_accumulator_spec.rb, which covers the same merge semantics plus the in-place absorb behaviour.

benchmarks/collate/runner.rb calls ResultMerger.absorb_results rather than reimplementing the loop, so what it times is what ships.

Refs #1250.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors SimpleCov’s resultset merge path to avoid pairwise folding overhead by introducing an in-place SimpleCov::Combine::CoverageAccumulator that absorbs resultsets one at a time, preserving the “read/parse one resultset at a time” memory ceiling while significantly reducing rebuild work during large parallel collate merges.

Changes:

  • Replace pairwise folding with Combine::CoverageAccumulator and expose ResultMerger.absorb_results for streaming/lazy merge iteration.
  • Update combiners (LinesCombiner, BranchesCombiner, MethodsCombiner) to support in-place absorption/materialization and reduce allocation/churn.
  • Remove internal Combine.combine, Combine::FilesCombiner, and associated specs; add new accumulator specs and update benchmarks/signatures/changelog.

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
spec/combine/files_combiner_spec.rb Removes specs tied to deleted FilesCombiner.
spec/combine/coverage_accumulator_spec.rb Adds coverage-merge semantics + in-place absorb behavior tests for the new accumulator.
sig/internal/simplecov/result_merger.rbs Types/docs for new absorb_results and updated executed-ness signal reference.
sig/internal/simplecov/combine/results_combiner.rbs Updates typed API/docs to reflect accumulator-driven combining.
sig/internal/simplecov/combine/methods_combiner.rbs Adds absorb/materialize signatures for method folding.
sig/internal/simplecov/combine/lines_combiner.rbs Adds merge_into/sum_into signatures for in-place line folding.
sig/internal/simplecov/combine/files_combiner.rbs Removes typed surface for deleted FilesCombiner.
sig/internal/simplecov/combine/coverage_accumulator.rbs Adds typed surface for CoverageAccumulator and MergedFile.
sig/internal/simplecov/combine/branches_combiner.rbs Adds absorb/materialize signatures for branch folding.
sig/internal/simplecov/combine.rbs Removes typed surface for deleted Combine.combine helper.
lib/simplecov/result_processing.rb Updates documentation comment to reference the new accumulator.
lib/simplecov/result_merger.rb Implements absorb_results; switches merge paths to use accumulator folding.
lib/simplecov/combine/results_combiner.rb Re-implements multi-result combining via CoverageAccumulator.
lib/simplecov/combine/methods_combiner.rb Adds interned-table absorb/materialize path for folding.
lib/simplecov/combine/lines_combiner.rb Adds merge_into/sum_into for in-place line merging.
lib/simplecov/combine/files_combiner.rb Removes deleted FilesCombiner implementation.
lib/simplecov/combine/coverage_accumulator.rb Adds the new accumulator and per-file merge state (MergedFile).
lib/simplecov/combine/branches_combiner.rb Adds interned-table absorb/materialize path for folding.
lib/simplecov/combine.rb Removes deleted Combine.combine helper; documents new entry point.
lib/simplecov.rb Updates requires to drop files_combiner and load coverage_accumulator.
CHANGELOG.md Documents performance improvement; (one remaining reference needs updating per comment).
benchmarks/collate/runner.rb Benchmarks now time ResultMerger.absorb_results (shipping code path).
benchmarks/collate/breakdown.rb Updates breakdown targets for new accumulator + in-place combiner methods.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CHANGELOG.md

## Performance
* Simulating a tracked-but-unloaded file no longer parses it to synthesize branch and method tuples when neither branch nor method coverage is enabled. Nothing reads those tuples in that case, and the Prism parse that produces them is over half the cost of simulating a file, paid once per tracked file in every process. On a 400 file synthetic project the injection pass drops from 0.526 to 0.222 ms per file for the default line-coverage-only configuration. Suites with branch or method coverage on are unaffected, and the line classification is identical either way. Reported with measurements by @andriytyurnikov. See #1250.
* Merging resultsets is roughly 40% faster, which on a large parallel CI run is most of what `collate` spends its time on. Results were folded together pairwise, so every one of the N-1 steps rebuilt the whole accumulated structure — a fresh outer file hash, a fresh lines array for every file, and a fresh branch/method table for every file whose keys were re-interned from their tuples each time. A 160-worker run over ~1,800 files did ~290,000 whole-file rebuilds to produce ~1,800 files of output. Results are now absorbed into an accumulator that owns its state and updates it in place, and the interned branch/method tables become tuple-keyed hashes once, at the end. Resultsets are still read and absorbed one at a time, so the memory ceiling `merge_results` is careful about is unchanged (peak RSS on the benchmark is identical). On the repository's `benchmarks/collate.rb` fixture — 160 resultsets, 1,836 files, 147,875 lines, branch coverage on — the merge phase drops from 5.85s to 3.38s and the whole collate from 6.65s to 3.87s. `SimpleCov::Combine::FilesCombiner` and `SimpleCov::Combine.combine` are gone, their roles taken by the new `SimpleCov::Combine::CoverageAccumulator`; both were internal API.
@sferik

sferik commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

@copilot resolve the merge conflicts in this pull request

Merging results folded them together pairwise, so every one of the N-1
steps rebuilt the whole accumulated structure: a fresh outer file hash,
a fresh lines array for every file, and a fresh branch/method table for
every file whose keys were re-interned from their tuples each time. A
160-worker run over ~1,800 files did ~290,000 whole-file rebuilds to
produce ~1,800 files of output, which made merging the dominant cost of
collate by a wide margin.

Combine::CoverageAccumulator now absorbs results one at a time into
state it owns outright and updates in place, and the interned branch
and method tables become tuple-keyed hashes once, at the end. Results
are still read and absorbed one at a time, so the memory ceiling
ResultMerger.merge_results is careful about is unchanged: peak RSS on
the benchmark is identical.

On benchmarks/collate.rb -- 160 resultsets, 1,836 files, 147,875 lines,
branch coverage on -- the merge phase drops from 5.85s to 3.38s (-42%)
and the whole collate from 6.65s to 3.87s. Peak RSS is unchanged.

The line merge itself was left alone beyond folding in place. At around
60ns per element it is already at CRuby's floor; a tight index loop
that avoids the block and method call per element measures the same.
Only the redundant re-walking was recoverable.

Combine::FilesCombiner and Combine.combine are gone. Both became
unreachable once the fold stopped being pairwise, and both were
internal API (sig/internal, never sig/simplecov.rbs). FilesCombiner's
spec cases, including the simplecov-ruby#1233 executed-vs-simulated reconciliation
that MergedFile now performs, move to the accumulator's spec.

The read-and-fold loop is extracted as ResultMerger.absorb_results so
benchmarks/collate can time what ships rather than its own copy of it.
It had one, which silently kept benchmarking the pairwise fold and
reported this change as no improvement at all. The loop only iterates
its paths, so the benchmark's progress reporting rides along on an
Enumerator rather than needing a hook inside the merge.
@sferik
sferik force-pushed the merge-resultsets-accumulator branch from feb5a87 to cbe13a9 Compare August 7, 2026 18:21
@sferik
sferik merged commit 736fbd7 into simplecov-ruby:main Aug 7, 2026
10 checks passed
@andriytyurnikov
andriytyurnikov deleted the merge-resultsets-accumulator 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