Repository navigation
perf(arrow/ipc): reuse zstd encoders across writers - #1429
Conversation
zeroshade
left a comment
There was a problem hiding this comment.
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.
3ea03b9 to
c7ecdb0
Compare
|
Thanks for the review. Added a subtest that pins the bleep bloop, a clanker wrote this 🤖 |
zeroshade
left a comment
There was a problem hiding this comment.
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.
Rationale for this change
getCompressorbuilds a newzstd.Encoderfor 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/compressalready pools its zstd encoders (#717). This does the same for the IPC writer.What changes are included in this PR?
sync.Pool, one per encoder level: the library default, plus the fourzstd.EncoderLevelpresets thatWithZstdLevelmaps to. An encoder is only reused for the level it was built with. The encoder options are unchanged.Writer.Close,FileWriter.CloseandGetRecordBatchPayloadreturn the encoders. A writer keeps its encoder for its whole lifetime, so a long-lived stream sees no change.Reset(nil). That waits for any in-flight block goroutines (also after a failedWrite) and drops the reference to the last output buffer.Are these changes tested?
New
TestZstdEncoderPool:Closereturns every encoder, forWriterandFileWriterwithWithCompressConcurrency(4). This fails without the change.-race.go test -race ./arrow/ipc/... ./arrow/flight/...passes, andgolangci-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: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.