Repository navigation
Absorb resultsets into one accumulator instead of folding pairwise - #1254
Merged
sferik merged 1 commit intoAug 7, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
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::CoverageAccumulatorand exposeResultMerger.absorb_resultsfor 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.
|
|
||
| ## 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. |
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
force-pushed
the
merge-resultsets-accumulator
branch
from
August 7, 2026 18:21
feb5a87 to
cbe13a9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Merging resultsets is most of what
collatespends 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::CoverageAccumulatorowns 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.rbfixture — 160 resultsets, 1,836 files, 147,875 lines, branch coverage on:Memory is the constraint this had to respect.
merge_resultsdeliberately 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:foldonly ever iterates its pairs, andabsorb_resultshands 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'snever_executed_filesnow 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 withResultMerger.Two fast paths are kept: a file only one resultset carried is returned as-is rather than copied, and
merge_coveragestill short-circuits on zero and one result.SimpleCov::Combine::FilesCombinerandSimpleCov::Combine.combineare 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.rbis replaced byspec/combine/coverage_accumulator_spec.rb, which covers the same merge semantics plus the in-place absorb behaviour.benchmarks/collate/runner.rbcallsResultMerger.absorb_resultsrather than reimplementing the loop, so what it times is what ships.Refs #1250.