Repository navigation
stream: reuse pooled output buffer in Writer to eliminate O(N^2) write allocations - #176
AliDatadog wants to merge 3 commits into
Conversation
…ng in Writer Stream compressed output directly to the underlying writer through a pooled ZSTD_CStreamOutSize() buffer instead of growing dstBuffer in an O(N^2) loop during small writes or allocating CompressBound(len(p)) on large writes. Co-Authored-By: Claude <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Co-Authored-By: Claude <noreply@anthropic.com>
…ound-trip Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8c43f3828a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if err := w.SetNbWorkers(2); err != nil { | ||
| t.Fatalf("SetNbWorkers failed: %v", err) |
There was a problem hiding this comment.
Skip the worker test when parallel compression is unsupported
When testing with the supported external_libzstd build tag against a libzstd compiled without multithreading, SetNbWorkers(2) returns ErrNoParallelSupport, so this new test fails even though compression remains functional. The existing testCompressionDecompression helper explicitly skips this configuration. Handle ErrNoParallelSupport with t.Skip here as well, while retaining the failure for other errors, so the test suite continues to support single-threaded external libraries.
Useful? React with 👍 / 👎.
|
Closing for now; after measuring the org2 fleet impact (~0.3–0.6% CPU across the Agent fleet), not worth chasing right now. |
Problem
In production Go allocation profiling of the internal Datadog Agent fleet (
service:datadog-agent, 4-houralloc-sizeprofile, 267 MiB/min total), streaming zstd compression accounts for 15.32% of all Agent Go heap allocations:Across internal Go backend services (
language:go env:prod),google.golang.org/grpczstd compression ((*Writer).Write:167) also accounts for 9.28% of allDataDog/zstdallocations (~19 MiB/min).Today, a streaming writer starts with a ~1.5 KiB output buffer (
CompressBound(1024)). When a caller streams small writes until libzstd flushes a full compressed block (typically 20–50 KiB), the output buffer fills mid-write and grows repeatedly by onlyCompressBound(remaining_input)(~400 bytes at a time), copying the entire buffer on each step — O(N²) reallocation and copying (~7.7 MB of garbage to compress 200 KB of logs). On large writes, every writer also allocates a freshCompressBound(len(p))output buffer instead of streaming through a bounded buffer.Solution
ZSTD_CStreamOutSize()(~128 KiB, libzstd's recommended streaming output size, matching the existing reader pool pattern in this file) when creating a writer, and return it to the pool on close.Write,Flush, andClose, flush compressed chunks directly to the underlyingio.Writerwheneverbytes_written > 0instead of accumulating and reallocating the output buffer.WriteandFlushafterClosewith an explicit closed-writer error, and skip zero-byte writes to the underlyingio.Writer.Proven Results
End-to-End Datadog Agent Single Machine Performance (SMP)
Tested end-to-end on
datadog-agentvia spike PR DataDog/datadog-agent#61387 (SMP Regression Detector report, Job ID493e0946-2b62-4991-83cf-4c93f529e0de, baseline07dd2ae9de):quality_gate_logs(% CPU utilization):−5.05%(90% CI[−6.10%, −3.99%]).dsd_uds_10mb_3k_timestamped_contexts_cpu(% CPU utilization):−0.87%(90% CI[−1.15%, −0.59%]).quality_gate_metrics_logs(memory utilization):−0.46%(90% CI[−0.68%, −0.24%]).dsd_uds_10mb_3k_timestamped_contexts_memory(memory utilization):−0.33%(90% CI[−0.52%, −0.13%]).Local Microbenchmarks
Measured locally on Apple M4 Max (arm64) with
go test -bench=BenchmarkStreamWriter -benchmem -count=5(medians):1.x)All existing tests plus new tests for
Write/FlushafterCloseandNewWriterLevelDict+SetNbWorkers(2)multi-chunk round-trip pass undergo test -race ./...and all 11 CI jobs (golang-1.24/1.25/1.26,external-libzstd,efence,i386).Why This Approach
ZSTD_CStreamOutSize()buffer. This matches libzstd's streaming contract and the existing reader pool pattern inzstd_stream.go, eliminates O(N²) buffer growth on small writes and per-writer buffer allocations on large writes, and adds no API changes.Remaining Limits
Close()will not return the pooled buffer (it is garbage-collected normally with no leak).io.Writercontract requires the underlying writer not to retain the slice passed toWrite(already relied on byFlushandClose).Internal production baseline notebook (Datadog employees): https://app.datadoghq.com/notebook/16529155
🤖 Generated with Claude Code