Skip to content

perf: tdigest grows its buffers as they fill - #561

Draft
rustyconover wants to merge 1 commit into
apache:masterfrom
rustyconover:perf/tdigest-lazy-buffers
Draft

rustyconover wants to merge 1 commit into
apache:masterfrom
rustyconover:perf/tdigest-lazy-buffers

Conversation

@rustyconover

Copy link
Copy Markdown
Contributor

tdigest's constructor reserves room for 2k + fudge centroids and BUFFER_MULTIPLIER times as many buffered values. For k = 100 with double that's about 10 KB per digest before anything is added, and it happens again on every deserialize().

This is the same kind of change as #558 for kll_sketch. Instead of reserving everything up front:

  • update() grows the buffer geometrically, starting at 8 values and doubling up to its existing limit.
  • compress() and merge() grow the centroids before they need them, also up to the existing limit.

Results and the serialized format are unchanged.

Test

Added tdigest custom allocator: buffers grow as they fill to tdigest_custom_allocator_test.cpp, using test_allocator:

  • An empty digest allocates nothing. Before this change it fails at REQUIRE( test_allocator_total_bytes == 0 ).
  • Three values take at most 8 values of buffer.
  • A digest of 100,000 updates gives the same quantiles as one built with std::allocator without the change.

All 17 test suites pass under CTest (GCC 14, aarch64).

We found this in the DuckDB DataSketches extension. For GROUP BY queries with many groups, DuckDB creates a state for each input row and merges them later, so the up-front reservation multiplied by millions of states. DuckDB made the same change to its own t-digest.

🤖 Generated with Claude Code

The constructor reserved room for 2k + fudge centroids and four times as many
buffered values (about 10 KB for k = 100 with doubles), also when
deserializing. Code that holds many small t-digests paid for all of it.
Grow the buffer geometrically up to its limit on update, and the centroids
before a compress or merge needs them. Results are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rustyconover
rustyconover force-pushed the perf/tdigest-lazy-buffers branch from 3dee23d to 15024ac Compare October 29, 2026 16:05
@leerho

leerho commented Oct 30, 2026

Copy link
Copy Markdown
Member

Thanks, Rusty. Same situation as #558: we'd like one allocation convention across the sketches and languages before merging either. The dev@ proposal, a lazy consensus ending Nov 7, 01:00 UTC, is here: https://lists.apache.org/thread/rv730qo61qqkd99dcy4wnglf3hz1s0fz

I'm marking this PR as a draft until that resolves. If it passes, this PR fits it well. The geometric growth up to the existing limits is the behavior the proposal describes, and we'd only add a reserve().

@leerho
leerho marked this pull request as draft October 30, 2026 23:26

This branch has not been deployed

No deployments
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.

2 participants