Skip to content

perf(arrow/ipc): reuse zstd encoders across writers - #1429

Merged
zeroshade merged 1 commit into
apache:mainfrom
vdpoora:perf/ipc-zstd-encoder-pool
Oct 29, 2026
Merged

zeroshade merged 1 commit into
apache:mainfrom
vdpoora:perf/ipc-zstd-encoder-pool

Conversation

@vdpoora

@vdpoora vdpoora commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

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.

@vdpoora
vdpoora marked this pull request as ready for review October 28, 2026 14:58
@vdpoora
vdpoora requested a review from zeroshade as a code owner October 28, 2026 14:58

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

Thanks — this is a nice win for the writer-per-batch pattern and mirrors the decoder pooling from #1331. Ownership looks right: each writer owns its compressor slice, encoders are only released at Close/end of GetRecordBatchPayload, slots are cleared so a double Close is safe, and Reset(nil) before Put waits for in-flight blocks. On amd64 (pinned, GOMAXPROCS=1, 8 runs) BenchmarkWriterZstd matches your M2 numbers: writer_per_record is 92–98% faster with B/op down from ~1.7 MiB to 5–22 KiB, and one_writer is unchanged. go test -race ./arrow/ipc/... ./arrow/flight/... passes.

Non-blocking: the "Close returns the encoders" subtest only checks that the slots are cleared — dropping the pool.Put still passes it. If you want to pin the reuse itself, an allocation check (e.g. testing.AllocsPerRun/bytes allocated for a second writer, skipped under -race since sync.Pool drops items randomly there) would catch that.


This review was drafted by an AI-assisted tool and
confirmed by an Apache Arrow maintainer. The findings
below are observations, not blockers; an Apache Arrow
maintainer — a real person — will take the next look at the
PR. If you think a finding is mis-applied, please reply on
the PR and a maintainer will weigh in.

More on how Apache Arrow handles maintainer review:
CONTRIBUTING.md.

Each writer built a new zstd encoder, which allocates about 2 MB of
window and match tables. A writer per record batch paid that for every
batch. Encoders now come from a pool per encoder level and go back on
Close, so a long-lived writer behaves as before.
@vdpoora
vdpoora force-pushed the perf/ipc-zstd-encoder-pool branch from 3ea03b9 to c7ecdb0 Compare October 29, 2026 08:14
@vdpoora

vdpoora commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Added a subtest that pins the Put itself: it swaps a recording fake into the encoder's pool field (now typed interface{ Put(any) }) and checks that Close puts that exact encoder back. It fails with the pool.Put removed. I went with a fake over an allocation check because sync.Pool may drop any item, so an alloc-based test would be flaky in principle and would need a skip under -race. This one is deterministic and runs under -race too.

bleep bloop, a clanker wrote this 🤖

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

Thanks — the recording fake is a better fix than the allocation check I suggested: it's deterministic, runs under -race, and it does pin the Put (removing z.pool.Put(z) now fails "Close puts the encoder back in its pool"). Everything else is as reviewed: encoders are owned per writer, released at Close/end of GetRecordBatchPayload, reset before reuse, and BenchmarkWriterZstd showed writer_per_record 92–98% faster on amd64 with the one_writer path unchanged.


This review was drafted by an AI-assisted tool and
confirmed by an Apache Arrow maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.

More on how Apache Arrow handles maintainer review:
CONTRIBUTING.md.

@zeroshade
zeroshade merged commit 56815b7 into apache:main Oct 29, 2026
40 of 41 checks passed
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