Skip to content

Evaluate all segment pairs when selecting next best merge. - #284

Merged
garretrieger merged 13 commits into
w3c:mainfrom
garretrieger:all_pairs_merging
Nov 13, 2026
Merged

garretrieger merged 13 commits into
w3c:mainfrom
garretrieger:all_pairs_merging

Conversation

@garretrieger

Copy link
Copy Markdown
Contributor

Use all-pairs best-first selection for cost-based segment merging.

Replaces single-base greedy cost-reducing segment merging with global all-pairs best-first selection (AllPairsMerger). Previously, evaluating and applying merges for one base segment until exhaustion could cause a high-probability segment to absorb a lower-probability segment for a marginal cost reduction, preventing a much stronger merge between that lower-probability segment and another partner.

Key changes:

  • Two-stage byte-normalized priority queue: Evaluates candidate exclusive pairs (pruned in descending probability order via the best-case inert probability threshold) and composite conditions across all active segments above the optimization cutoff. Candidates with a negative best-case cost delta are queued without running Brotli and lazily refined with exact Brotli patch sizes only when popped from the top of the min-heap. Heap priority normalizes cost delta by the total pre-merge byte size (including network overhead) of affected patches.
  • Lazy invalidation & incremental dirty refresh: Tracks per-segment epoch counters so stale heap entries are discarded in O(1) on pop without rebuilding the queue, and records pre-combination representative glyphs and affected composite conditions during merge application so only newly impacted candidates are re-queued after condition reprocessing.
  • Two-phase minimum group size enforcement: Once all strictly negative cost-delta merges are exhausted, remaining undersized segments are merged per-base in a separate pass using the probability ordering maintained by AllPairsMerger.
  • Cost delta fast paths & caching: Adds a 2-segment inert fast path and unchanged-condition/unchanged-patch-size short-circuits in CandidateMerge::ComputeCostDelta() (ActivationCondition::IsUnchangedByMerge()), along with per-segment aggregate probability and exclusive patch size caching on Merger.

Benchmark Results (Google Fonts corpus):

  • Total Segmentation Time Across Corpus: 57.94h -> 54.36h (-6.18% / -3.58h total, median -2.62%, mean -7.05%)
  • Total IFT Cost: 208.40M -> 206.16M (-1.07% / -2.24M total, median -0.33%, mean -1.31%)
  • Total number of timeouts decreased by 11 (252 -> 241).

- Adds new AllPairsMerger which tracks all pairs merging specific state.
- Does not yet include invalidation logic.
…in group sizes.

- Updates per base merging to utilize all pair's segment ordering if present (since all pairs merging can invalidate the input segment ordering).
Some patches have their condition changed without changing the corresponding patch, thus the new patch has the same size. The delta contribution for these is then (Pnew - Pold) * (patch_size + k) where Pnew >= Pold. As a result we can estimate a best case contribution of 0 without having to calculate Pnew or Pold. Furthermore there are some cases where a condition change, but is effectively the same condition. Which means Pnew = Pold. In this case we can immediately calculate the delta as 0 in non best case analysis without evaluating sizes or probabilities.

This updates ComputeCostDelta() to detect these cases and skip probability/size calcs when possible.
- Uses bytes as the normalization divisor.
- All pairs merging is not kept separate from min group enforcement merging.

This particular configuration demonstrated significant segmenter time and cost reductions in batch testing.
…non-dep graph modes.

These are deprecated and will be removed in the future.
@garretrieger
garretrieger merged commit 64e4cab into w3c:main Nov 13, 2026
3 checks passed
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.

1 participant