Skip to content

perf(ddsketch): recycle sketch state across windows via a pool - #418

Merged
zzylol merged 1 commit into
mainfrom
perf/ddsketch-cross-window-pool
May 22, 2026
Merged

zzylol merged 1 commit into
mainfrom
perf/ddsketch-cross-window-pool

Conversation

@zzylol

@zzylol zzylol commented May 22, 2026

Copy link
Copy Markdown
Contributor

What

In window mode the processor swaps its per-metric Precompute map every flush, so all ~30K per-series DDSketches are dropped and rebuilt each 60s window — the bucket store was allocated + grown from empty every window.

Adds a cross-window sketch pool owned by the long-lived processor (survives the per-window Precompute swap):

  • precompute: new optional SketchSink (SetSketchSink), invoked by finishRotate once a series' envelope is serialized — the Put side of a host pool. nil sink = prior drop/GC behavior.
  • DDSketchWrapper.Reset(): now an in-place Clear (keeps store capacity) instead of reallocating via NewDDSketch.
  • ddsketchprocessor: owns a sync.Pool; getOrCreate's factory Gets from it, the sink Resets + Puts. One relative_accuracy per processor ⇒ a single type+shape pool is correctness-safe. proc==nil (test batch path) falls back to fresh sketches.

Why

Issue #46 agent perf. Completes the "pool the sketch since its shape is config-fixed" idea — the storage primitive is in sketchlib-go #55, this wires it through.

Steady-state effect (cold-off 1-sketch agent, 30K series, 100 Hz)

Heap alloc_space diffed across two 60s windows (pprof -base): DDSketch store allocation (Buckets.ensure / FilledVector1D / GrowZeroed) drops to ~0 — the pool fully recycles backing arrays across windows. The cumulative-from-start profile only showed ~1% because window-1 cold dominates; measured across steady-state windows the per-window store alloc is gone. Remaining steady-state alloc is entirely framework OTLP decode + contrib hashkey.

CPU is neutral — the store churn was ~2% of alloc and never CPU-bound; the win is allocation / GC pressure, not cycles. (Part A — series-key pooling, #417 — is the CPU win.)

Correctness

Depends on

ProjectASAP/sketchlib-go#55 (DDSketch.Clear + capacity reuse). Builds locally via the `=> ../../sketchlib-go` replace; merge #55 first.

🤖 Generated with Claude Code

In window mode the processor swaps its per-metric Precompute map every
flush, so all ~30K per-series DDSketches are dropped and rebuilt each
60s window. The sketch store (bucket Vector1D) was therefore allocated +
grown from empty every window.

Add a cross-window sketch pool, owned by the (long-lived) processor so
it survives the per-window Precompute swap:
  - precompute: a new optional SketchSink (SetSketchSink) is invoked by
    finishRotate once a series' envelope is serialized — the symmetric
    Put side of a host pool. nil sink keeps the prior drop/GC behavior.
  - DDSketchWrapper.Reset() now does an in-place Clear (keeps the store's
    backing-array capacity) instead of reallocating via NewDDSketch.
  - ddsketchprocessor owns a sync.Pool; getOrCreate's factory draws from
    it (Get) and the sink Resets + returns sketches (Put). Every sketch
    in one processor shares the configured relative_accuracy, so a single
    type+shape pool is correctness-safe. proc==nil (test batch path)
    falls back to fresh sketches.

Steady-state effect (cold-off 1-sketch agent, 30K series, 100 Hz; heap
alloc diffed across two 60s windows): DDSketch store allocation
(Buckets.ensure / FilledVector1D / GrowZeroed) drops to ~0 — the pool
fully recycles backing arrays across windows. The one-time window-1 cold
allocation is the only store alloc; remaining steady-state alloc is all
framework OTLP decode + contrib hashkey, untouched by this change. CPU is
neutral (the store churn was ~2% of alloc, never CPU-bound) — the win is
GC pressure / allocation, not cycles.

Depends on ProjectASAP/sketchlib-go#55 (DDSketch.Clear + capacity reuse);
builds locally via the `=> ../../sketchlib-go` replace.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@zzylol
zzylol merged commit e608db3 into main May 22, 2026
@zzylol
zzylol deleted the perf/ddsketch-cross-window-pool branch July 17, 2026 20:08
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