Skip to content

cov and scov by a wide random key are 18% slower since the running-mean pivot #1889

In progress

Description

@singaraiona

Since #1850 (e7178a2), cov and scov grouped by a wide random key are about 18% slower at one worker when both sides are F64.

Repro

10,000,000 rows, gcc release. One worker, pinned to one CPU. Interleaved dev/head runs; each figure is the minimum of 25 calls.

(set n 10000000)
(set i (til n))
(set TM (table [d u x y] (list (as 'DATE (+ 20000 (% (div i 256) 700))) (as 'U8 (% i 256)) (+ 1e8 (as 'F64 (% i 37))) (as 'I32 (% i 41)))))
(set TF (table [d u x y] (list (at TM 'd) (at TM 'u) (at TM 'x) (as 'F64 (at TM 'y)))))
(select {c: (cov x y) from: TF by: {d: d u: u}})   ; 179,200 groups of about 56 rows
build F64×F64 cov, ms
before #1850 (1ce0357) 75.4–76.1
dev with #1850 89.4–90.0 (+18%)

The F64×I32 shape on the same key, the one #1850's reviews timed, is only +3%: the loop before #1850 was generic for it.

Where the time goes

Throwaway builds, same shape and method:

row path ms
the pivot subtraction only: no seed branch, no due test 70.3
plus the per-group seed branch if (st->n == 0) 82.5
plus the count bookkeeping and a due test that fires only at 64 86.4
the full design: the due test and a repivot at every power of two 89.6
  • About 12 ms is the seed branch. It tests a count that misses the cache on every scattered row.
  • About 4 ms is the due test and the count bookkeeping (mode bits, mask).
  • About 3 ms is the repivot work: about 5 repivots per 56-row group.
  • A branch-free seed (a select on the count) does not help, because the pivot load then depends on the count load.

Direction

  • Seed the pivots outside the scattered row loop: a first-touch pass per batch, or states seeded from each group's first row in the batch.
  • Test for a due count less often, for example only at batch boundaries, if the accuracy contract still holds.
  • Target: every wide-key shape (F64×F64, F64×I32, I64×I64; cov and scov; one worker and all cores) within 10% of the commit before fix(agg): cov and scov sum about the running mean #1850, and no other shape slower.

Activity

  1. singaraiona commented on Nov 28, 2026

    @singaraiona
    CollaboratorAuthor

    What remains after #1893, at one worker, against the commit before #1850 (1ce0357). Interleaved runs, pinned, lowest minima:

    shape vs before #1850
    wide random key, F64 × F64 cov / scov about +11% (dev today +18%; #1893 −6% against dev)
    wide random key, I64 × I64 +11–12% (unchanged by #1893)
    cyclic 179,200 groups, a nullable x (first value null) +19% (29.7 → 35.5 ms on dev; #1893 adds +3% layout)
    wide key with a where: mask (10% / 50% / 90%) +7% to +12%
    wide random key, F64 × I32 about +2%

    At all cores every one of these is within 3% of the commit before #1850 (mostly within 2%): the cost shows only at one worker.

    The cause is the per-group pivot of #1850's design. Each group's pivot is taken from its own first pair, and a state that misses the cache costs about a memory latency per group on the paths that seed it per row. #1893 removes most of it for null-free F64 × F64 pairs on scattered keys. The nullable, masked and integer paths still seed per row; extending the seed pass there is about a day of work with no guarantee of reaching 10%. Left open for a decision on whether that is worth doing.

  2. singaraiona commented on Nov 28, 2026

    @singaraiona
    CollaboratorAuthor

    #1893 is closed without merging. Its chunked seed path for null-free F64 × F64 pairs on scattered keys gained about 6% at one worker on the wide random key, from +18% to about +11% against the commit before #1850. Its path selector, which judges a batch from a prefix of its group ids, can be misled. The second review measured, at one worker:

    • a batch whose first 64 rows jump between two far groups, with a clustered tail: +49% to +56% slower than dev;
    • four hot states visited with large jumps between them: +23% slower than dev.

    A prefix heuristic always leaves such an input, so the path was dropped. Dev keeps the #1850 row path. What remains against the commit before #1850, at one worker, is therefore:

    shape vs before #1850
    wide random key, F64 × F64 cov / scov +18%
    wide random key, I64 × I64 +11–12%
    cyclic 179,200 groups, a nullable x +19%
    wide key with a where: mask (10–90%) +7% to +12%
    wide random key, F64 × I32 about +2%

    At all cores every one of these is within 3% of the commit before #1850. Left open for the owner to decide whether more work is worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions