Skip to content

perf(parquet/compress): set zstd pool encoder concurrency to 1 - #717

Merged
zeroshade merged 1 commit into
apache:mainfrom
dimakuz:perf/zstd-pool-concurrency
Mar 17, 2026
Merged

zeroshade merged 1 commit into
apache:mainfrom
dimakuz:perf/zstd-pool-concurrency

Conversation

@dimakuz

@dimakuz dimakuz commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

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).

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

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).
@dimakuz
dimakuz requested a review from zeroshade as a code owner March 16, 2026 19:52
@zeroshade

Copy link
Copy Markdown
Member

Should this be a configurable setting that we just default to 1?

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just the one question

@dimakuz

dimakuz commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the prompt review!
I think that in the current implementation of zstd codec and zstd libs there's no real reason to make it configurable. We encode as one-shot (EncodeAll) and internally it uses a single encoder, never benefitting from parallel encoders being available.
If you prefer to plumb it out in some way so its more controllable from outside I can try.

@zeroshade

Copy link
Copy Markdown
Member

I think this is fine for now, we can look into making it more controllable in a follow-up. Thanks!

@zeroshade
zeroshade merged commit 5a94422 into apache:main Mar 17, 2026
39 of 41 checks passed
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.
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