Ray's Knowledge Base
Unlock

A rare-path bookkeeping step inside a hot per-row loop stops the loop from inlining

PitfallVerified 29 Sep 2026Holds project: dftracer-utils
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

After adding duql aggregation, plain View aggregations were 1.4% to 4.4% slower than baseline 05a7f86b in all 5 old/new benchmark pairs ("agg name" +2.9%, "time bucket" +2.8%, "session tree + agg" +4.4%), although the change did not touch them.

Cause#

The integer sum overflow bookkeeping sat inside the per-row loop and kept the compiler from inlining that loop. Two smaller costs added to it: a copy of every event name in a call-tree fix, and idle workers waking on timed waits. Guesses at the null bitmap and row numbers were wrong; a profile of both binaries showed the real causes.

Fix#

Move the rare step into an out-of-line function marked __attribute__((noinline, cold)), the form the repo already uses in agg/state.cpp. Keep pointers into the name table instead of copying strings, and write rows in output order in one pass (containment_fold.cpp).

Evidence#

After the fix, alternating old/new runs: agg name 705-717 ms vs 704-709 ms, time bucket 694-710 ms vs 698-702 ms, session 1347-1404 ms vs 1366-1384 ms. To time one case, view_api_bench 2000000 DIR session runs in about 1 minute instead of 12. See also A/B timings on a busy machine.

Also found by searching

  • view_api_bench: agg name +2.9%, time bucket +2.8%, session tree + agg +4.4% slower vs baseline
  • my change made unrelated aggregation benchmarks a few percent slower although I did not touch them
  • how do I keep a rarely used overflow check from slowing down a hot per-row aggregation loop?
  • integer sum overflow handling in inner loop prevents inlining, move to noinline cold function
  • benchmark shows View aggregation 3% slower after adding duql sum overflow check, change did not touch them
  • small regression in hot loop after adding bookkeeping branch, compiler no longer inlines
  • why did adding a rare overflow check in the per-row loop slow things down?
  • perf regression of 1.4 to 4.4 percent against baseline across old/new benchmark pairs, move cold path out of loop