Repository navigation
fix(cost): charge a counter-matrix sketch's state allocation, heap re-estimates, readout and merge, measured on the executor - #1382
Merged
Conversation
zzylol
force-pushed
the
fix/query-time-sketch-size-cost
branch
from
November 16, 2026 23:42
1a9cd5f to
e717824
Compare
zzylol
force-pushed
the
fix/query-time-sketch-size-cost
branch
from
November 17, 2026 18:56
e717824 to
8188722
Compare
…-estimates, readout and merge, measured on the executor Count-Min and Count Sketch (with and without a top-k heap) and UnivMon get constants fitted on the executor's builds, readouts and merges: a counter update per row of depth, the heap kernel's re-estimate of every resident per row, each state's counters allocated and zeroed when it is built, a merge per counter plus the heap residents re-estimated, and a heap top-k read per resident counter. Each is a CostConstant. A heap holds the distinct items its state folded, at most its size: the build reads StateInputRows::distinct_values from the summary's item column (a keyed summary), else its weight column. StateLastWrittenBy carries the state's input rows so the readout counts the residents.
…op-k now costs less than a query-time Count-Min heap The Count-Min Sketch with heap the snapshot selected is charged its measured heap re-estimates per row (the executor's kernel re-estimates every resident after each update), so the exact hash aggregate over the same 10,000 rows is now the cheaper plan.
zzylol
force-pushed
the
fix/query-time-sketch-size-cost
branch
from
November 17, 2026 18:58
8188722 to
6004805
Compare
…ct items; review fixes StateLastWrittenBy::Merge carries the merged state's input rows, so a top-k heap read after a merge counts the items it holds instead of a full heap. The KMV merge reuses distinct_items_seen; counters_updated_per_row no longer has an arm for the heap families, whose work is charged apart. The new CostConstant variants follow the constants' declaration order. A SummaryAgg's detail names the allocation only when there is one. Docs: held-out ingestion range, the hand-off in the heap example, the input state's residents in the merge, the allocation example's charged difference. The snapshot manifest says the top-k rate snapshot now plans exact.
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.
Summary
Review guide
main(5ebca6c11, after the KMV refit). Rebased and ported onto main'sNodeCostTerms: every new constant is aCostConstant. The pre-port head is onbackup/fix/query-time-sketch-size-cost-e71782461.3832afa7bthe charges and their tests (ported);6004805b6regenerates one sketch snapshot (see Verification);34dea5d69applies the final review.update_terms_per_row,merge_terms_per_stateandestimate_terms_per_groupincrates/cost/src/node/operations_per_unit.rs, then the new constants above them and theirCostConstantvariants incrates/cost/src/node/cost_terms.rs.crates/cost/src/node/usage/mod.rs, theSummaryAggandSummaryTumblingWindowChainarms add one allocation per state and record the distinct values their state folds inStateInputRows, which theSummaryEstimateandSummaryMergearms pass on.Behavior: before → after
topk(10, sum by (url) (rate(http_requests_total[1h]))), with these settings:Interfaces
No new public type.
CostConstant(public,crates/cost/src/node/cost_terms.rs) gains 12 variants, in the constants' declaration order; their JSON names are the snake_case of each constant (e.g."cms_heap_operations_per_estimate"). Everything else is crate-private toasap-cost.StateInputRows::distinct_valuesalready exists on main (merging keeps the largest); this PR fills it from a keyed summary's item column.crates/cost/src/node/operations_per_unit.rscrates/cost/src/node/cost_terms.rspub enum CostConstant { ... SketchUpdateOperationsPerCounter, + CounterMatrixUpdateOperationsPerCounter, + UnivmonUpdateOperationsPerCounter, + CmsHeapOperationsPerEstimate, + CmsHeapOperationsPerEstimatedCounter, + CountSketchHeapOperationsPerEstimate, + CountSketchHeapOperationsPerEstimatedCounter, + SketchAllocationOperationsPerCounter, ... SketchMergeOperationsPerByte, + CounterMatrixMergeOperationsPerState, + CounterMatrixMergeOperationsPerCounter, + HeapMergeOperationsPerResidentCounter, ... SketchEstimateOperationsPerGroup, + HeapReadOperationsPerGroup, + HeapReadOperationsPerResidentCounter, ... }crates/cost/src/node/usage/mod.rs:folded_distinct_valuesreads a keyed summary's item column (theurla top-k heap counts), and its weight column otherwise.SummaryAggaddsgroup_count × state_allocation_terms;SummaryTumblingWindowChainadds the same per emitted state.SummaryEstimatepasses the producer'sstate_input_rowsin bothStateLastWrittenByvariants, so a heap read after a merge counts the merged state's distinct items.Usage, end to end, for the workload above:
Algorithm changes
Cost model: counter-matrix sketch state size, heap and merge
Problem.
update_terms_per_rowcharged29 + 18 × (depth + 1)per row for every Count-Min, Count Sketch (± heap) and UnivMon, plus nothing per state. Three costs were missing:width × depthcounters per state.crates/executor/src/summary_kernels/weighted_frequency.rs,update_key) callsrescore_heap()after every update, which re-estimates every heap resident.asap_sketchlib's own heap update estimates only the touched item; the per-row rescore is the kernel's choice, made to keep an older kernel's exact membership. A Count Sketch row at depth 81 with 10 residents measures 33–40 µs; it was charged 1.5 µs. This PR charges what the executor does today. Once the kernel stops rescoring, the four*_HEAP_OPERATIONS_PER_ESTIMATE*constants must be measured again (see Follow-ups).Across the grid, the old charges were 1–800× under (RMS log error 3.1).
Before → after.
update_terms_per_row):29 + 19 × depth.29 + (residents + 1) × (a + b × depth), with Count-Min a = 150, b = 33 and Count Sketch a = 58, b = 36. Residents come fromheap_residents_per_update: the distinct items seen so far, at most the heap size, averaged over the state's rows.29 + 95 × counters_updated_per_row.SummaryAggper group; a chain per emitted state):2.1 × sketch_state_counters.estimate_terms_per_group):4,000 + 55 × residents × depth, plus the output row. Residents come fromheap_residents: the distinct items the state folded (distinct_items_seen), at most the heap size. UnivMon and the plain sketches keep 1,000 per group plus the 2,500 output row. UnivMon's L2 read measured 0.2–0.3 µs per group with its row at 1,000 states, whatever its width, so it stays over-charged; that is small next to its build.merge_terms_per_state):2,300 + 2.9 × counters, plus53 × residents × depthfor a heap variant.Fit.
Heap residents at read and merge count distinct items.
heap_residentswasmin(heap, rows); it is nowmin(heap, distinct_items_seen(rows, distinct)), the same countheap_residents_per_updateuses. A state of 1,000 rows of 5 items was charged 5 residents per update but 10 at read and merge; now 5 at all three. The constants are unchanged. This changes the charges at 26 of the 158 grid points, all of them states of 10 rows of 10 keys:igets keyi % 10), so every state holds all 10. The uniform-draw count expects 6.32. On the other points the two counts agree.Correctness. Three tests check this:
a_wider_query_time_count_sketch_costs_more_as_the_executor_measures: at width 30,000 against 300, the charge ratio is within the fit's leave-one-setting-out factor of 1.37 of the measured ratio (2.06).counter_matrix_sketches_are_charged_their_measured_time: 15 measured shapes across build, readout, merge and ingestion, each within the fit's range (readout 0.65–2.66, the whole grid's range with distinct-item residents).a_heap_over_few_distinct_items_is_read_and_merged_for_the_items_it_holds: a heap of 10 over 1,000 rows of 5 items is read and merged for 5 residents.The ranges come from the fit; no constant was tuned to the tests.
Complexity. Each term is O(1) per node.
heap_residents_per_updatesums 16 slices.Assumptions and limits.
measured_operation_costs) replaces.unchanged_columnresolves only a singleColumn. A PromQL top-k item is always aTuple(even of one column, from a closed child), the series-identity tuple (series_identity_itemappends__series_identity), or anEntityIdentity. So it falls back tomax_distinct_per_evaluation, and toNonewhen the workload does not declare that. Only a SQL single-column item resolves. I chose to state this gap and file [Bug] A top-k heap over a PromQL item (tuple or series identity) is costed as if every row's item differed #1387 rather than resolve aTuplehere: a correct resolution also needs the series identity andEntityIdentitymapped to the series count, which is more than a product of declared values. The surveyed heavy-hitter workloads are charged the same either way (9.53 against 9.50 residents).Decisions and open questions
StateInputRows::mergedkeeps the largest of its inputs' distinct values (as main defines it), not their sum: the inputs fold the same column.None([Bug] A top-k heap over a PromQL item (tuple or series identity) is costed as if every row's item differed #1387).Verification
Run on a 40-core build host, PR
34dea5d69against main5ebca6c11:asap-costcargo nextest run -p asap-costasap-cost+asap-integration-tests+asap-devtoolscargo nextest run -p asap-cost -p asap-integration-tests -p asap-devtools --no-fail-fastcargo test --release -p asap-devtools --test corpus_snapshots -- --ignoredcargo fmt --all --check;cargo clippy -p asap-cost -p asap-integration-tests -p asap-devtools --all-targets -- -D warningsThe 9 failing on both:
joint_search_equivalence×2,materialized_views::materialized_views_equal_the_stored_outputs_main_listed,plan_sharing×2,promql_to_post_asap::promql_quantile_of_rate_binds_kll_over_rate_accumulator,planner_fuzz×3.New tests:
a_wider_query_time_count_sketch_costs_more_as_the_executor_measures: a wider Count Sketch costs more, in proportion to the executor's time.counter_matrix_sketches_are_charged_their_measured_time: measured CMS+heap, CS+heap and UnivMon builds (at query and ingestion time), readouts and merges are charged within the fit's range.a_heap_over_few_distinct_items_is_read_and_merged_for_the_items_it_holds: a heap's read and merge count the distinct items it holds.Snapshot
topk-rate-guarantees-ignored-mvpregenerated (by construction). The workload istopk(5, sum by (job) (rate(...)))with accuracy guarantees ignored. The query-time top-k now reads an exact aggregate instead of a Count-Min Sketch with heap (width 28, depth 5, heap 10). Hand-derived per execution, 10,000 finalized rates, 0.1 executions per second.jobdeclares no distinct values, so every heap is full:job: 10,000 × 20 + 10,000 × 170 + 10,000 × 2,500 = 26.9 M;coverstext now says so.Plan survey, main
5ebca6c11against this PR. Every workload was planned on both with the executor's sketch families, cost model 1, and an exact-only run for the sketch-heavy set. All 357 runs plan. The PR side was run again after the final review fixes and is identical:rollupheavy-t11. Every plan and every cost is unchanged.mixed__l2-and-distinct__*. Main's KMV refit already builds their KMV in ingestion windows, so they now differ only in UnivMon moving to ingestion.Top-k:
edge-dashboardkeeps its UnivMon, now built at ingestion; its CMS+heap goes exact (2.84× → 1.92×).UnivMon L2:
frequency-moments__univmon-l2__r1-10hz-6h-every10s-releps0.2-d0.05frequency-moments__univmon-l2__r1-1hz-1h-every60s-releps0.1-d0.05frequency-moments__univmon-l2__r2-10hz-24h-every300s-releps0.1-d0.05frequency-moments__univmon-l2__r2-1hz-5m-every60s-releps0.1-d0.05frequency-moments__univmon-l2__r4-10hz-24h-every300s-releps0.1-d0.1frequency-moments__univmon-l2__r4-1hz-1h-every10s-releps0.1-d0.05frequency-moments__univmon-l2__r8-10hz-1h-every60s-releps0.05-d0.1frequency-moments__univmon-l2__r8-10hz-24h-every10s-releps0.2-d0.1frequency-moments__univmon-l2__r8-10hz-5m-every10s-releps0.1-d0.1frequency-moments__univmon-l2__r8-1hz-24h-every60s-releps0.1-d0.1heavy-hitters__cmsheap-topk10__u1000-i1-10hz-1h-every60s-l1eps0.01-d0.05heavy-hitters__cmsheap-topk10__u1000-i1-10hz-24h-every300s-l1eps0.002-d0.01heavy-hitters__cmsheap-topk10__u1000-i1-1hz-5m-every10s-l1eps0.01-d0.05heavy-hitters__cmsheap-topk10__u1000-i1-1hz-5m-every60s-l1eps0.01-d0.05heavy-hitters__cmsheap-topk10__u1000-i2-1hz-1h-every60s-l1eps0.01-d0.05heavy-hitters__cmsheap-topk10__u2500-i1-10hz-5m-every10s-l1eps0.002-d0.05heavy-hitters__cmsheap-topk10__u2500-i1-1hz-1h-every60s-l1eps0.01-d0.05heavy-hitters__cmsheap-topk10__u2500-i2-10hz-5m-every10s-l1eps0.01-d0.05heavy-hitters__cmsheap-topk10__u2500-i2-1hz-24h-every60s-l1eps0.002-d0.05heavy-hitters__cmsheap-topk10__u5000-i1-10hz-1h-every10s-l1eps0.01-d0.05heavy-hitters__cmsheap-topk10__u5000-i1-1hz-24h-every300s-l1eps0.002-d0.05heavy-hitters__cmsheap-topk10__u5000-i1-1hz-5m-every60s-l1eps0.01-d0.05heavy-hitters__csheap-topk10__u100-i1-10hz-5m-every10s-l2eps0.05-d0.1heavy-hitters__csheap-topk10__u100-i1-1hz-1h-every60s-l2eps0.02-d0.1heavy-hitters__csheap-topk10__u100-i1-1hz-5m-every60s-l2eps0.1-d0.1heavy-hitters__csheap-topk10__u1000-i1-1hz-5m-every60s-l2eps0.05-d0.1heavy-hitters__csheap-topk10__u300-i1-10hz-1h-every60s-l2eps0.1-d0.1heavy-hitters__csheap-topk10__u300-i1-1hz-5m-every60s-l2eps0.05-d0.1mixed__edge-dashboard__u1000-i1-1hz-r4-10hzmixed__l2-24h-and-15m__r1-10hz-every60smixed__l2-24h-and-15m__r4-10hz-every60smixed__l2-24h-and-15m__r8-10hz-every60smixed__l2-and-count__r1-10hz-24h-every60smixed__l2-and-count__r4-10hz-24h-every60smixed__l2-and-count__r8-10hz-24h-every60smixed__l2-and-distinct__r1-10hz-24h-every60smixed__l2-and-distinct__r4-10hz-24h-every60smixed__l2-and-distinct__r8-10hz-24h-every60smixed__repeat-rate-and-l2__r1-10hz-24h-every60smixed__repeat-rate-and-l2__r4-10hz-24h-every60smixed__topk-and-total__u1000-i1-1hz-1h-every60smixed__topk-and-total__u5000-i1-1hz-1h-every60smixed__topk-by-url-and-by-instance__u100-i10-1hz-1h-every60smixed__topk-by-url-and-by-instance__u500-i10-1hz-1h-every60smixed__topk-dashboard-and-alert__u1000-i1-1hz-1h-every10s-and-60smixed__topk-dashboard-and-alert__u5000-i1-1hz-1h-every10s-and-60sFollow-ups
Executor heap fix, then re-measure the heap constants. The weighted-frequency kernel's per-row
rescore_heap()makes each row cost (heap size + 1) estimates. Offering only the touched item (asap_sketchlib's owntry_update_weightedon the heap sketch) keeps every certified guarantee and would cut Count-Min with heap from 3,608 to 596 ns per row at depth 10, and Count Sketch with heap at depth 81 from 29.0 to 6.6–9.1 µs. After it,CMS_HEAP_OPERATIONS_PER_ESTIMATE,CMS_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER,COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATEandCOUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATED_COUNTERmust be measured again.Exact top-k is over-charged about 2.2–3.8× ([Bug] Exact top-k is charged 2.2–3.8× its measured time; with the executor heap fix the planner would pick a slower sketch #1386). It must be recalibrated together with the executor heap fix. Otherwise the cheaper heap would make the model pick the sketch where exact is still 1.9–15× faster.
PromQL top-k items get no distinct count ([Bug] A top-k heap over a PromQL item (tuple or series identity) is costed as if every row's item differed #1387).
The 19 µs per-execution readout constant, and an allocation model that follows the allocator's regimes, if large states matter.
Hydra and HyperLogLog are still charged unmeasured constants; plain Count Sketch has no executor build path to measure.
The UnivMon benefits now rest on its measured 95 per counter, a cost the hashing in
asap_sketchlibsets: hashing every row separately for each of its sketch rows. Sharing one hash across the rows would lower it; that belongs in a separate sketchlib PR.Human review — do not complete with an agent