Repository navigation
perf(agg): cov and scov by a wide key seed their pivots ahead of the scattered sums - #1893
singaraiona wants to merge 1 commit into
Conversation
singaraiona
left a comment
There was a problem hiding this comment.
Review of 9a37ff8: NEEDS CHANGES
[P1] The cache gate selects the slower path for cache-resident clustered/streaming access (src/ops/agg_stream.c:830–860). max(gid[0:256]) * stride > 1 MiB uses absolute group offsets as a proxy for the working set. With k = div(i, 56), most batches satisfy the gate, although each 256-row chunk touches only about five adjacent 48-byte states. The new seed pass adds substantial overhead there. Four interleaved runs per binary, each the minimum of 25 calls, at 10M rows, one worker pinned to CPU 12:
| Query | dev ms | PR ms | PR vs dev | PR with chunking disabled ms |
|---|---|---|---|---|
| cov | 31.161 | 44.741 | +43.6% | 31.125 |
| scov | 31.238 | 44.770 | +43.3% | 31.266 |
| wsum | 29.194 | 29.102 | -0.3% | 29.367 |
The last column changes only the new helper's gate to always take its row path; it restores dev's time. This is well beyond the control noise and violates the no-regression requirement. Keep clustered and streaming access on the row path, or use a deterministic admission rule that accounts for locality, then rerun the boundary and masked cases as well as the motivating random-key case.
Reproducer (run each aggregate separately):
(set i (til 10000000))
(set T (table [k x y] (list (div i 56)
(+ 1e8 (as 'F64 (% i 37))) (as 'F64 (% i 41)))))
(println (min (map (fn [_]
(timeit (select {c: (cov x y) from: T by: k}))) (til 25))))Other single-worker probes, also four pairs of 25-call minima, pinned to CPU 10, used the same x/y with cyclic k = i % G:
| Shape | cov dev / PR ms | cov change | wsum change |
|---|---|---|---|
| G=20000 | 19.984 / 20.410 | +2.1% | +2.6% |
| G=21845 | 20.031 / 20.690 | +3.3% | +1.9% |
| G=22000 | 20.095 / 20.737 | +3.2% | +2.0% |
| G=24000 | 20.692 / 21.381 | +3.3% | +1.1% |
| G=30000 | 22.084 / 24.026 | +8.8% | +3.2% |
| G=40000 | 23.514 / 27.375 | +16.4% | +2.8% |
| G=60000 | 24.491 / 29.284 | +19.6% | +1.2% |
| G=179200, where y > 20 | 52.293 / 56.801 | +8.6% | -0.3% |
| G=179200, first x null | 35.111 / 37.010 | +5.4% | -0.5% |
A separate four-triplet dev/PR/forced-row repeat on CPU 6 confirmed the cyclic and masked regressions: g30000_cov 22.064 / 23.706 / 22.030 ms; g60000_cov 24.967 / 29.179 / 24.870 ms; mask_cov 52.300 / 57.728 / 52.233 ms; mask_scov 52.274 / 57.833 / 52.435 ms. The nullable case retained a smaller slowdown even with chunking disabled; a parent-file replacement comparison was noisy, so I have not assigned it a separate causal finding.
Requested wide-key timings. gcc 11.4 release on i7-14700; before = 1ce035735, dev = b65eab939 (captured at review start), PR = 9a37ff843. The exact date × U8 key and F64/F64, F64/I32, I64/I64 values from #1889/this PR were used, at 10M rows. Four interleaved dev/PR/before triplets, minimum of 25 calls per run, then the lowest of the four minima. One worker was pinned to CPU 6; all-core runs used 28 workers. This was a shared workstation; controls are reported rather than treating every small percentage as a regression.
| Workers | Wide pair / aggregate | Before ms | Dev ms | PR ms | vs before | vs dev |
|---|---|---|---|---|---|---|
| 1 | F64×F64 cov | 75.56 | 89.45 | 84.05 | +11.2% | -6.0% |
| 1 | F64×F64 scov | 75.62 | 89.62 | 83.98 | +11.1% | -6.3% |
| 1 | F64×I32 cov | 75.19 | 76.84 | 75.97 | +1.0% | -1.1% |
| 1 | F64×I32 scov | 75.30 | 76.99 | 75.88 | +0.8% | -1.4% |
| 1 | I64×I64 cov | 76.06 | 84.93 | 84.02 | +10.5% | -1.1% |
| 1 | I64×I64 scov | 75.71 | 84.84 | 84.07 | +11.0% | -0.9% |
| 28 | F64×F64 cov | 20.09 | 19.64 | 19.61 | -2.4% | -0.1% |
| 28 | F64×F64 scov | 20.09 | 19.42 | 19.64 | -2.3% | +1.1% |
| 28 | F64×I32 cov | 18.02 | 17.46 | 18.00 | -0.1% | +3.1% |
| 28 | F64×I32 scov | 18.16 | 17.66 | 18.07 | -0.5% | +2.3% |
| 28 | I64×I64 cov | 20.01 | 19.68 | 20.01 | -0.0% | +1.7% |
| 28 | I64×I64 scov | 19.72 | 19.71 | 19.78 | +0.3% | +0.4% |
| Shape | 1-worker cov / scov / wsum change vs dev | 28-worker cov / scov / wsum change vs dev |
|---|---|---|
| wide_ff | -6.0% / -6.3% / -0.5% | -0.1% / +1.1% / +2.3% |
| wide_fi | -1.1% / -1.4% / -0.8% | +3.1% / +2.3% / +1.0% |
| wide_ii | -1.1% / -0.9% / +0.0% | +1.7% / +0.4% / +2.9% |
| by1k | -0.5% / +1.1% / -1.0% | +0.2% / -1.4% / -0.3% |
| keyless | -0.3% / -0.4% / -0.3% | +2.3% / -0.9% / -0.1% |
| direct | -0.0% / -0.0% / -0.1% | -0.0% / +0.1% / +0.5% |
| slice | -0.9% / -0.3% / -0.4% | +2.9% / -0.4% / -0.2% |
| parted | -0.0% / +0.8% / -1.2% | +0.2% / +0.1% / -0.2% |
The marginal 28-worker increases were rechecked in four further interleaved dev/PR pairs (25-call minima); cov / scov / wsum changes were: wide_fi -1.57% / -1.35% / +0.05%; keyless +0.45% / -0.19% / +0.90%; slice -0.95% / +0.10% / -1.36%. These increases did not reproduce.
Index slices used a grouped symbol key and membership filter; parted used two 5M-row splayed partitions, grouped by 1k. I also timed 120k-row singleton/two-row groups, 10M rows with four-row groups, and a 32-row input. The accepted remaining single-worker cost relative to pre-#1850 was reproduced. The verdict concerns the new regressions above.
Results and routing: make -j4 test CC=gcc TEST_ARGS="-j 4" passed 5938/5942, four skipped, zero failed. An independent C probe using strict binary128 reference arithmetic checked 8,601,600 result cells: eight 10M-row, 179,200-group cases, cov and scov separately, PR at one/four workers and dev's row path at one worker. Cases included outliers first/last/at powers of two/pseudorandom positions, reordered and clustered rows, large close values and scaled magnitudes. Maximum error was 7.90e-16 of group spread, below the 1e-9 bound; cross-path and worker-count differences consequently also fit that bound. Constant-side groups were exactly zero. Repeated four-worker outlier runs matched bit for bit after matching keys. This establishes agreement within the bound, not universal bitwise equality between one and four workers.
The new decision itself is deterministic: fixed first-256-row maximum, fixed stride, fixed 1 MiB threshold, no measured cache/load or random input. Null-bearing validity views and other type pairs take the original loop. Its defect is the locality estimate, not nondeterministic selection.
Commit message: the actual sole commit has the conventional perf(agg): ... subject matching the PR title, explanation, and final Fixes #1889 trailer. Revise the categorical “no shape slower” claim after fixing the regression; even the existing message's own all-core table contains +3–4% rows while its summary says every shape is within 2%.
Additional boundary, nullable, and small-group timing probes used one worker only. The clustered repro was also timed at 28 workers: cov -1.4%, scov +2.0%, wsum +5.3% versus dev, without a clear regression beyond control noise.
Not covered: other CPU/cache architectures, macOS/Windows/clang/TSan, exhaustive floating-point inputs or a formal error proof, cold-storage performance, and unrelated repository behavior. No dev merge/rebase or repository changes were made. Temporary build trees and generated data were deleted after collecting the results.
…scattered sums ## Defect Since #1850, `cov` and `scov` grouped by a wide random key were 18% slower at one worker when both sides are null-free F64 (#1889). On the 179,200-group date × U8 key, at 10,000,000 rows, they took 89.6 ms against 75.5 ms before #1850. The cost is in the v2 engine's per-row path, not in the arithmetic. On a wide key every row's state misses the cache. The per-row test "is this state new?" (seed its pivot from this pair) is a branch on that missed count. When it is mispredicted, once per group, it waits out the miss and drops the loads in flight behind it. Throwaway builds on that shape: | row path | ms | |---|---:| | the pivot subtraction only, no seed and no due test | 70.3 | | plus the seed branch | 82.5 | | plus the count bookkeeping and a due test that fires only at 64 | 86.4 | | the full design, a due test and a repivot at every power of two | 89.6 | ## Fix Null-free F64 × F64 pairs (`cov_update2_ff` in `src/ops/agg_stream.c`) now run in chunks of 256 rows, in two passes: - **The seed pass:** for each row, a state not yet touched takes the row's pair as its pivot. This is a bit select on the count, not a branch, so it takes the cache misses with little work per row. A group with several rows in a chunk is seeded by its last one there. That is still a pair of the group, which is all the pivot needs to be until its first move to the running mean at count 2, so the accuracy argument of #1850 is unchanged. - **The accumulation:** the same sums and due test as before, now on lines that are in the cache, where a mispredicted branch costs a few cycles instead of a memory latency. - **Which batches** (`cov_states_spill`): the chunked path is taken only when a batch's rows scatter over states that spill out of the cache. The decision is deterministic, from the batch's own group ids: - of its first 64 rows, at least three quarters must jump more than 4 KiB of states from the row before; - the largest id seen, times the state stride, must pass 1 MiB. A sorted, clustered or cyclic key walks neighbouring states that the cache and its prefetch serve, and there the extra pass only costs: the first review measured +43% on a clustered key and up to +20% on cyclic ones with a table-size gate alone. A key of few groups keeps its states in the cache. Both take the row-by-row path as before. Masked (`where:`) and nullable inputs keep it too. Every other pair keeps today's path. - **I64 × I64:** the same two-pass form measured slower (89–101 ms against today's 86). I spent a bounded pass on it; its instruction count is already below dev's, the cost is the same missed-load dependency, and none of the variants I tried beat the row-by-row path. - **F64 × I32:** within 3% of the commit before #1850 already. ## What changes against dev - **Answers:** within the tolerance of #1850, the same accuracy contract. A group whose first chunk holds several of its rows takes a different pair as its first pivot, so the last bits can differ from today's. The seeded search against the double-double reference has 0 failures: 400 cases from each of seeds 0x1847, 0x5eed and 0xc0ffee, 56,919,200 cells each, in the release-flags build; in the suite, 24 cases and 3,415,152 cells. - **Cost:** see below. ## Cost Wall time in ms, gcc release, 10,000,000 rows. Interleaved runs of each build, each the minimum of 25 calls; the tables compare the lowest minima. One worker is pinned to an idle performance core (`taskset -c 0`); all-core runs use all 28. The load average was about 4 to 8. The builds: - *before*: 1ce0357, the commit before #1850; - *today*: dev with #1850; - *this*: this commit. The `wsum` rows are controls: their code is unchanged. The wide key is #1889's 179,200-group date × U8 key, with `x = 1e8 + i%37` (F64) and `y = i%41` as F64 or I32; I64 × I64 is `1e16 + i%37` against `1e16 + i%41`. Three interleaved rounds: | query | 1 worker before | today | this | vs before | vs today | all cores before | today | this | vs before | vs today | |---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:| | wide F64xF64 cov | 76.24 | 89.98 | 84.22 | +10.5% | -6.4% | 19.64 | 19.43 | 19.70 | +0.3% | +1.4% | | wide F64xF64 scov | 76.18 | 89.63 | 84.01 | +10.3% | -6.3% | 19.44 | 19.17 | 19.72 | +1.4% | +2.9% | | wide F64xI32 cov | 75.78 | 77.29 | 77.31 | +2.0% | +0.0% | 17.34 | 17.44 | 17.18 | -0.9% | -1.5% | | wide F64xI32 scov | 75.68 | 77.35 | 77.33 | +2.2% | -0.0% | 17.55 | 17.40 | 17.56 | +0.1% | +1.0% | | wide I64xI64 cov | 76.32 | 85.46 | 84.85 | +11.2% | -0.7% | 19.43 | 19.44 | 19.52 | +0.4% | +0.4% | | wide I64xI64 scov | 76.60 | 85.49 | 84.88 | +10.8% | -0.7% | 19.31 | 19.13 | 19.66 | +1.8% | +2.8% | | wide F64xF64 wsum ctl | 76.10 | 76.31 | 76.39 | +0.4% | +0.1% | 19.61 | 19.73 | 19.74 | +0.7% | +0.0% | Other key shapes, at one worker, two interleaved rounds: - *clustered* `div i 56`; - *nearly sorted*, the same plus a small row offset; - *cyclic* `i % G`; - masks on the wide key; - *tiny*: `div i 2`, and `(i·7919) % 5,000,000`. | query | before | today | this | vs before | vs today | |---|---:|---:|---:|---:|---:| | random wide cov | 76.21 | 89.42 | 84.17 | +10.4% | -5.9% | | random wide wsum ctl | 76.20 | 76.35 | 76.62 | +0.5% | +0.4% | | clustered cov | 28.72 | 30.61 | 30.29 | +5.5% | -1.1% | | clustered wsum ctl | 28.74 | 28.70 | 28.67 | -0.3% | -0.1% | | nearly sorted cov | 26.64 | 29.56 | 27.93 | +4.9% | -5.5% | | cyclic 21845 cov | 23.83 | 20.12 | 20.10 | -15.6% | -0.1% | | cyclic 60000 cov | 25.05 | 25.01 | 25.01 | -0.2% | -0.0% | | cyclic 60000 wsum ctl | 24.97 | 25.08 | 25.09 | +0.5% | +0.0% | | cyclic 179200 cov | 26.76 | 28.70 | 28.73 | +7.4% | +0.1% | | mask 10pct wide cov | 28.36 | 29.67 | 30.05 | +5.9% | +1.3% | | mask 50pct wide cov | 58.52 | 63.54 | 62.36 | +6.6% | -1.9% | | mask 90pct wide cov | 86.68 | 96.67 | 95.22 | +9.9% | -1.5% | | mask 50pct wide wsum ctl | 58.64 | 58.55 | 58.25 | -0.7% | -0.5% | | tiny groups cov | 926.92 | 953.21 | 952.14 | +2.7% | -0.1% | | tiny random groups cov | 959.40 | 993.00 | 995.41 | +3.8% | +0.2% | | tiny random groups wsum ctl | 966.73 | 971.41 | 973.99 | +0.8% | +0.3% | | by3 cov | 22.15 | 18.32 | 18.78 | -15.2% | +2.5% | | cyclic 179200 where y>20 cov | 51.82 | 52.49 | 52.48 | +1.3% | -0.0% | | cyclic 179200 where y>20 wsum ctl | 51.68 | 51.27 | 51.45 | -0.5% | +0.3% | | cyclic 179200 first x null cov | 29.71 | 35.47 | 36.55 | +23.0% | +3.0% | | random wide where y>20 cov | 59.12 | 64.59 | 63.21 | +6.9% | -2.1% | Against dev today: - **Wide random F64 × F64:** 6% faster at one worker: +10% to +11% against the commit before #1850, down from +18%. - **Every other shape:** within ±2% at one worker, except two: - **The nullable shape** (cyclic 179,200, first x null) reads **+3%**. It never takes the new path; a build with the path compiled in but never called shows the same +3–4%. So it is the code layout of the added function, and moving the definitions, an out-of-line row loop and forbidding the clone did not remove it. - **`by3 cov`** read **+2.5%** in this table; an earlier pair read it level. - **All cores:** within 3% of both builds. Two `scov` rows read +2.8–2.9% in three rounds; earlier rounds read them level. What remains against the commit before #1850, at one worker, is in #1889, which stays open: - wide random F64 × F64, about +11%; - wide I64 × I64, +11–12%; - nullable cyclic, +19%; - masked wide keys, +7% to +12%. These come from the per-group pivot of #1850's design on paths this commit leaves alone. ## Tests - **`test/rfl/agg/comoment_large_magnitude.rfl`:** a 30,000-group F64 × F64 key with four rows per group, several groups to a chunk, and an outlier every 28th row, checked against each group's direct `cov`. It passes on dev too: this change keeps answers within the tolerance and only changes time. A timing change has no test that fails on dev. - **`agg_contract/comoment_random_search`** and **`agg_contract/comoment_large_magnitude`** cover the path, with null-free F64 × F64 pairs by high-cardinality keys at 1 and 4 workers. Runs: - gcc release and debug builds are clean; clang 23 `-fsyntax-only -Werror` on `src/ops/agg_stream.c`, with and without `DEBUG`, is clean. - `make -j4 test CC=gcc TEST_ARGS="-j 4"`: 5938 of 5942 passed, 4 skipped, 0 failed. - The co-moment tests pass in the release-flags build, with the 400-case search on three seeds as above. Part of #1889
9a37ff8 to
df2b2b6
Compare
|
Review addressed in df2b2b6 (one commit; the description matches its message).
|
singaraiona
left a comment
There was a problem hiding this comment.
Review of df2b2b6: NEEDS CHANGES
[P1] The new gate still admits cache-local input and can apply an unrepresentative prefix to an entire batch (src/ops/agg_stream.c:834–854). Two reproducible cases:
- In each 8192-row batch, alternate group IDs 0 and 179199 for the first 64 rows, then use
div i 56for the remaining rows. All 63 sampled transitions pass the 4 KiB test, and the maximum ID passes the 1 MiB test, so the whole batch takes the chunked path. The sampled prefix itself touches only two states, and the rest is clustered. Changing less than 0.8% of the rows in the original clustered repro reintroduces its slowdown. - First introduce keys 0 through 179199 once, then cycle through keys 0, 50000, 100000, 150000 for the rest of 10M rows. Here the sample represents the remaining input, but the four hot states occupy only 192 bytes. Large jumps between their addresses still admit the slower chunked path.
gcc 11.4 release, i7-14700, 10M rows, one worker pinned to CPU 12. Four interleaved dev/PR/forced-row rounds, each the minimum of 25 calls; table entries are the lowest minima. The forced-row build retains the chunked code and uses a volatile flag to force the helper's original row loop.
| Shape / query | dev ms | PR ms | PR vs dev | forced-row ms |
|---|---|---|---|---|
| jump prefix → clustered tail / cov | 32.013 | 47.736 | +49.1% | 32.748 |
| jump prefix → clustered tail / scov | 30.848 | 47.998 | +55.6% | 31.247 |
| jump prefix → clustered tail / wsum | 29.644 | 30.063 | +1.4% | 30.150 |
| jump prefix → cyclic tail / cov | 27.656 | 34.133 | +23.4% | 28.798 |
| jump prefix → cyclic tail / scov | 28.406 | 33.760 | +18.8% | 28.137 |
| jump prefix → cyclic tail / wsum | 28.409 | 29.089 | +2.4% | 28.747 |
| four hot states / cov | 21.362 | 26.292 | +23.1% | 22.080 |
| four hot states / scov | 21.237 | 26.101 | +22.9% | 22.013 |
| four hot states / wsum | 25.983 | 25.433 | -2.1% | 25.267 |
The wsum controls and forced-row builds separate this from the accepted pre-#1850 accuracy cost. Admission needs to account for state reuse and locality changes within the batch while remaining deterministic. The reverse case, a clustered prefix followed by jumping IDs, was also tested: cov/scov were +1.6%/+1.4%, with wsum +1.3%, so that case showed no clear regression.
Reproducer for the first case (cov/scov/wsum queried separately):
(set i (til 10000000))
(set p (as 'I64 (< (% i 8192) 64)))
(set k (+ (* p (* (% i 2) 179199)) (* (- 1 p) (div i 56))))
(set T (table [k x y] (list k
(+ 1e8 (as 'F64 (% i 37))) (as 'F64 (% i 41)))))
(println (min (map (fn [_]
(timeit (select {c:(cov x y) from:T by:k}))) (til 25))))Previous cases: against dev 1691a5748, the original clustered, cyclic and masked regressions are fixed in these measurements. The motivating date × U8 wide key retains its improvement. Four interleaved dev/PR pairs, minima of 25, 10M rows, pinned CPU 6 or 28 workers:
| Shape | 1 worker: cov / scov / wsum vs dev | 28 workers: cov / scov / wsum vs dev |
|---|---|---|
| wide | -6.8% / -7.1% / -0.3% | +2.9% / +0.0% / -0.8% |
| cluster | -2.7% / -3.1% / +0.5% | +0.9% / -5.4% / -3.4% |
| cyclic60 | -0.6% / +0.1% / -0.8% | +9.4% / -7.2% / +10.7% |
| cyclic179 | +1.4% / +0.7% / +0.5% | -2.8% / +0.1% / -1.1% |
| maskcyclic | -3.2% / +0.0% / +1.2% | -0.1% / -4.5% / -1.4% |
| maskwide | -3.7% / -1.7% / +1.1% | -1.9% / -3.7% / +0.4% |
| by3 | +0.4% / -0.4% / +1.0% | -1.7% / -2.8% / +0.1% |
| null | +1.1% / +3.4% / +0.2% | -0.9% / -4.4% / -2.5% |
maskcyclic and maskwide use where: (> y 20.0); cyclic domains contain 60,000 or 179,200 keys. Separate exact 10% and 90% masks on the wide key (four pairs, one worker) gave cov / scov / wsum changes of 10%: +1.9% / +1.7% / +1.4%; 90%: +4.0% / +4.5% / +0.8%. By3 did not reproduce a persistent +2.5% regression. All-core controls varied by as much as 10.7% on this shared host, so these runs do not establish a universal ±3% bound; the large pinned regressions above are the blocking evidence.
The 90% mask also survived a separate four-triplet dev/PR/forced-row repeat: cov 85.528 / 89.134 / 86.204 ms; scov 84.750 / 89.325 / 86.133 ms; wsum 82.720 / 82.671 / 83.935 ms. This mask is p = i % 10, where: (< p 9) on the date × U8 key. It should be retained in the performance checks for the next admission change.
Nullable layout check: four further interleaved rounds on CPU 6, the cyclic 179,200-key input with only the first x null:
| Query | dev ms | PR ms | helper retained, call disabled ms | helper retained, original caller restored ms |
|---|---|---|---|---|
| cov | 35.516 | 37.222 | 36.696 | 35.544 |
| scov | 35.311 | 36.962 | 37.078 | 35.568 |
| wsum | 29.762 | 29.713 | 29.718 | 29.428 |
The disabled-call build keeps the dispatch guard and adds a volatile disable flag. The last build removes the call-site block and retains the helper with used; nm confirms its 4326-byte body remains. The slowdown persists without executing the helper, consistent with a compiler/code-layout effect rather than chunked arithmetic. Restoring the original caller removes most of it, so the stronger claim that the added helper's mere footprint alone explains the effect is not established. Disassembly also shows the dispatch guard changes the caller's generated code, including its stack frame (0x1d8 → 0x1e8). I have kept this separate from the blocking admission finding and the lead's accepted cost decisions.
Answers and checks: independent strict binary128 comparisons checked 4,654,956 result cells over five 10M-row cases (misleading prefixes, their reverse, a cyclic tail, constant groups, and four hot groups), with outliers at rows 63 and 255 of each batch, PR at one/four workers and forced-row at one worker. No failures: maximum error was 9.874e-13 of group spread versus the documented 1e-9 bound. Cross-path/worker differences consequently also fit that bound; constant groups were exactly zero and singleton scov groups were null. The gate remains deterministic for the same batch IDs and stride. The four targeted co-moment tests passed, including the seeded 24-case search.
make -j4 test CC=gcc TEST_ARGS="-j 4": 5938 of 5942 passed, 4 skipped, 0 failed.
The single commit has the conventional subject and now ends in Part of #1889, preserving the open issue as requested. I did not reopen the accepted pre-#1850 cost decision.
Not covered: other compilers/CPU architectures/operating systems, TSan, exhaustive floating-point inputs or a formal error proof. I did not repeat the pre-#1850 build or the earlier review's direct/index-slice/parted matrix in this focused follow-up. Temporary build trees and generated data were deleted; the repository was unchanged.
|
Closing without merging, by the lead's decision. The chunked path gains about 6% on wide random F64 × F64 keys at one worker. Its selector judges a batch from a prefix of its group ids, and the second review built inputs where that prefix misleads it: a jumping 64-row prefix ahead of a clustered tail runs 49–56% slower than dev, and four hot states among jumps run 23% slower. Any prefix heuristic leaves such an input. That small gain on one shape does not justify a path selector that can be 50% slower on other key orders. The remaining single-worker cost stays documented on #1889. |
Defect
Since #1850,
covandscovgrouped by a wide random key were 18% slower at one worker when both sides are null-free F64 (#1889). On the 179,200-group date × U8 key, at 10,000,000 rows, they took 89.6 ms against 75.5 ms before #1850.The cost is in the v2 engine's per-row path, not in the arithmetic. On a wide key every row's state misses the cache. The per-row test "is this state new?" (seed its pivot from this pair) is a branch on that missed count. When it is mispredicted, once per group, it waits out the miss and drops the loads in flight behind it.
Throwaway builds on that shape:
Fix
Null-free F64 × F64 pairs (
cov_update2_ffinsrc/ops/agg_stream.c) now run in chunks of 256 rows, in two passes:The seed pass: for each row, a state not yet touched takes the row's pair as its pivot. This is a bit select on the count, not a branch, so it takes the cache misses with little work per row. A group with several rows in a chunk is seeded by its last one there. That is still a pair of the group, which is all the pivot needs to be until its first move to the running mean at count 2, so the accuracy argument of fix(agg): cov and scov sum about the running mean #1850 is unchanged.
The accumulation: the same sums and due test as before, now on lines that are in the cache, where a mispredicted branch costs a few cycles instead of a memory latency.
Which batches (
cov_states_spill): the chunked path is taken only when a batch's rows scatter over states that spill out of the cache. The decision is deterministic, from the batch's own group ids:A sorted, clustered or cyclic key walks neighbouring states that the cache and its prefetch serve, and there the extra pass only costs: the first review measured +43% on a clustered key and up to +20% on cyclic ones with a table-size gate alone. A key of few groups keeps its states in the cache. Both take the row-by-row path as before. Masked (
where:) and nullable inputs keep it too.Every other pair keeps today's path.
What changes against dev
Cost
Wall time in ms, gcc release, 10,000,000 rows. Interleaved runs of each build, each the minimum of 25 calls; the tables compare the lowest minima. One worker is pinned to an idle performance core (
taskset -c 0); all-core runs use all 28. The load average was about 4 to 8. The builds:The
wsumrows are controls: their code is unchanged.The wide key is #1889's 179,200-group date × U8 key, with
x = 1e8 + i%37(F64) andy = i%41as F64 or I32; I64 × I64 is1e16 + i%37against1e16 + i%41. Three interleaved rounds:Other key shapes, at one worker, two interleaved rounds:
div i 56;i % G;div i 2, and(i·7919) % 5,000,000.Against dev today:
by3 covread +2.5% in this table; an earlier pair read it level.scovrows read +2.8–2.9% in three rounds; earlier rounds read them level.What remains against the commit before #1850, at one worker, is in #1889, which stays open:
These come from the per-group pivot of #1850's design on paths this commit leaves alone.
Tests
test/rfl/agg/comoment_large_magnitude.rfl: a 30,000-group F64 × F64 key with four rows per group, several groups to a chunk, and an outlier every 28th row, checked against each group's directcov. It passes on dev too: this change keeps answers within the tolerance and only changes time. A timing change has no test that fails on dev.agg_contract/comoment_random_searchandagg_contract/comoment_large_magnitudecover the path, with null-free F64 × F64 pairs by high-cardinality keys at 1 and 4 workers.Runs:
-fsyntax-only -Werroronsrc/ops/agg_stream.c, with and withoutDEBUG, is clean.make -j4 test CC=gcc TEST_ARGS="-j 4": 5938 of 5942 passed, 4 skipped, 0 failed.Part of #1889