Repository navigation
perf(parquet/compress): set zstd pool encoder concurrency to 1 - #717
Merged
Merged
Conversation
The zstdEncoderPool is used exclusively by EncodeAll(), which is a
single-shot synchronous call that uses exactly one inner block encoder.
However, zstd.NewWriter defaults concurrent to runtime.GOMAXPROCS,
pre-allocating that many inner block encoders — each with its own ~1 MiB
history buffer (ensureHist). On a 10-core machine, each pooled Encoder
allocates 10 inner encoders when only 1 is ever used by EncodeAll.
With WithEncoderConcurrency(1), each pooled encoder creates a single
inner encoder, matching actual usage. The streaming Write/Close path
is unaffected — it does not use the pool.
Benchmark results (Apple M4 Pro, arm64, 256 KiB semi-random data):
BenchmarkZstdPooledEncodeAll/Default-14 11000 B/op 5250 MB/s
BenchmarkZstdPooledEncodeAll/Concurrency1-14 810 B/op 5500 MB/s
14x less memory per operation, ~5% higher throughput from reduced GC
pressure.
In a parquet write workload (1 GiB Arrow data, ZSTD level 3), this
reduced ensureHist allocations from 22 GiB to 7 GiB and madvise
kernel CPU from 4.6s to 2.3s (10% wall-time improvement).
Member
|
Should this be a configurable setting that we just default to 1? |
Contributor
Author
|
Thanks for the prompt review! |
Member
|
I think this is fine for now, we can look into making it more controllable in a follow-up. Thanks! |
zeroshade
pushed a commit
that referenced
this pull request
Oct 29, 2026
### Rationale for this change `getCompressor` builds a new `zstd.Encoder` for every writer. A new encoder allocates its window and match tables, about 2.3 MB at the default level, before it compresses anything. A service that writes each table or batch with its own writer pays that on every call. In our case, one writer per series in each Grafana query, this was about 63% of all allocations on that path, and the GC load that followed was a third of the CPU. #1331 pooled the zstd decoders for the same reason, and `parquet/compress` already pools its zstd encoders (#717). This does the same for the IPC writer. ### What changes are included in this PR? - zstd encoders come from a `sync.Pool`, one per encoder level: the library default, plus the four `zstd.EncoderLevel` presets that `WithZstdLevel` maps to. An encoder is only reused for the level it was built with. The encoder options are unchanged. - `Writer.Close`, `FileWriter.Close` and `GetRecordBatchPayload` return the encoders. A writer keeps its encoder for its whole lifetime, so a long-lived stream sees no change. - Before going back to the pool, the encoder is `Reset(nil)`. That waits for any in-flight block goroutines (also after a failed `Write`) and drops the reference to the last output buffer. - lz4 is unchanged. ### Are these changes tested? New `TestZstdEncoderPool`: - `Close` returns every encoder, for `Writer` and `FileWriter` with `WithCompressConcurrency(4)`. This fails without the change. - The output of a reused encoder is byte-identical to its first use, after a larger stream went through it in between. - 16 concurrent writers with mixed sizes and compression concurrency round-trip correctly under `-race`. `go test -race ./arrow/ipc/... ./arrow/flight/...` passes, and `golangci-lint run ./arrow/ipc/` is clean. Run on macOS only. New `BenchmarkWriterZstd`. Old and new test binaries were run alternately, 8 runs each, on an Apple M2 Pro: ``` sec/op B/op allocs/op writer_per_record/256rows 246.4µ → 4.5µ (-98%) 2279Ki → 7.1Ki (-99.7%) 72 → 44 writer_per_record/4096rows 280.7µ → 21.8µ (-92%) 2296Ki → 24.7Ki (-98.9%) 72 → 44 one_writer/256rows 2.98µ → 2.98µ (~) 2.755Ki → 2.755Ki (~) 23 → 23 one_writer/4096rows 19.4µ → 19.6µ (~) 19.54Ki → 19.54Ki (~) 23 → 23 ``` ### Are there any user-facing changes? No API change. Writers that use zstd allocate less. Idle encoders can stay in the pool until GC clears it, at most one per level and per P.
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.
The zstdEncoderPool is used exclusively by EncodeAll(), which is a single-shot synchronous call that uses exactly one inner block encoder. However, zstd.NewWriter defaults concurrent to runtime.GOMAXPROCS, pre-allocating that many inner block encoders — each with its own ~1 MiB history buffer (ensureHist). On a 10-core machine, each pooled Encoder allocates 10 inner encoders when only 1 is ever used by EncodeAll.
With WithEncoderConcurrency(1), each pooled encoder creates a single inner encoder, matching actual usage. The streaming Write/Close path is unaffected — it does not use the pool.
Benchmark results (Apple M4 Pro, arm64, 256 KiB semi-random data):
14x less memory per operation, ~5% higher throughput from reduced GC pressure.
In a parquet write workload (1 GiB Arrow data, ZSTD level 3), this reduced ensureHist allocations from 22 GiB to 7 GiB and madvise kernel CPU from 4.6s to 2.3s (10% wall-time improvement).
Rationale for this change
High memory churn during parquet encoding
What changes are included in this PR?
Change to zstd encoder concurrency, a benchmark to reproduce results.
Are these changes tested?
Yes
Are there any user-facing changes?
No