Reduce Datadog allocation for small batches - #184
Merged
Merged
Conversation
Bound initial raw and offset capacity by available candidates and series and cursor capacity by remaining source values. Preserve exact payload limits, bounded conversion windows, and checkpoint delivery semantics.
Sample bounded name and tag lengths to reduce repeated buffer growth for long series. Use a median to avoid extrapolating isolated large values, and retain exact payload checks and the small-series capacity floor.
Areson
marked this pull request as ready for review
September 17, 2026 17:59
jayjanssen
approved these changes
Sep 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The Datadog sink reserves space for 10,000 series even when a batch contains only a few dozen metrics. These short-lived buffers create avoidable allocation and garbage collection work.
What
Size initial series, cursor, offset, and raw JSON buffers from available input. Keep the 10,000-series conversion ceiling and exact payload limits unchanged.
How
Use remaining source slice lengths to bound series and cursor capacity. Estimate raw capacity from at most eight evenly spaced series and sixteen tags per sampled series. The estimate uses string lengths and an upper median, with a 512-byte floor per series and overflow-safe caps. It adds no full conversion pass or retained buffers. Serialization, gzip settings, chunking, checkpoints, and retry behavior stay unchanged.
Risk
This affects every Datadog send. A bounded estimate can miss large fields or JSON escaping: allocation still exceeds baseline for some inputs. The measured minority-large case regresses 42.85%; the escape-heavy case regresses 18.03%. This is not a universal memory or latency improvement.
Testing
Compared baseline
a5c9f4b, initial sizingdbcc2b7, and final6a99d5c2using identical inputs and a test-only consumer dependency graph. Measurements used Go 1.26.4, Datadog SDK 2.37.1, CGO disabled, GOMAXPROCS=2, GOGC=100, and GOMEMLIMIT=off. This PR does not change dependencies.Six balanced-order runs used ten fixed sends per case through
SendWithCheckpointand the SDK'sCallAPI. Native Linux arm64 median allocated bytes per send:The local comparison harness ran
-test.run '^$' -test.bench '^BenchmarkRefinement$' -test.benchtime=10x -test.count=1for each variant and order. It covered 26 distributions, including mixed sizes, early/late outliers, Unicode, many tags, and large names. Linux amd64 runs used emulation; their CPU and memory results do not qualify native amd64 performance.Committed benchmarks can be run with
go test ./sink -run '^$' -bench '^BenchmarkDatadogCapacity' -benchmem -benchtime=10x -count=6. They use the repository's dependencies by default, so their absolute results differ from the matched consumer comparison above.go test -race ./sink -count=1passed on native macOS arm64 with both upstream and matched consumer dependencies. The upstream race run was refreshed before PR creation.Generated with Codex