Skip to content

Commit 7c13a60

Browse files
authored
perf: zstd implement Pooling + DecodeAll with no new reader. (#1331)
### Rationale for this change Performance improvement on the zstd path. ### What changes are included in this PR? - zstd: decoders come from a `sync.Pool` instead of being created for every record batch, and each compressed buffer is decoded with one `DecodeAll` call straight into the destination buffer (decoder built with `WithDecoderConcurrency(1)` and `WithDecodeAllCapLimit(true))` instead of `Reset(bytes.NewReader)` + `io.ReadFull` on a streaming decoder. - lz4 : no functional change. It keeps its original per-batch `lz4.Reader`; the library already pools its large block buffers internally. (Only a Decompress method wrapper was added.) ### Are these changes tested? Added tests + benchmark for zstd (the changed path) - every case improves ``` SMALL buffers (4 KiB per column) time : -70.5% to -78.3% in all 8 cases 1 col, 1 batch (stream) 9.89 us -> 2.92 us 1 col, 64 batches (stream) 592.9 us -> 128.7 us 16 cols, 64 batches (file) 3.67 ms -> 894 us B/op : -60% (1 col, 1 batch), -64% (1 col, 64 batches), -22% (16 cols) allocs/op : -45% (1 col, 1 batch) to -70% (16 cols, 64 batches) LARGE buffers per column time B/op allocs/op 16 KiB 77.6 us -> 24.0 us (-69%) 306.9 KiB -> 157.1 KiB (-49%) 184 -> 78 (-58%) 256 KiB 796.7 us -> 191.7 us (-76%) 20.6 MiB -> 2.05 MiB (-90%) 185 -> 81 (-56%) 4 MiB 4.63 ms -> 3.05 ms (-34%) 51.1 MiB -> 32.1 MiB (-37%) 189 -> 79 (-58%) ``` ### Are there any user-facing changes? Strictness change for zstd. A buffer whose decompressed size differs from the uncompressed-length prefix now returns an error. This matches other arrow libraries (e.g.: [arrow-cpp](https://github.com/apache/arrow/blob/f3b46fb7d4b8f5fb3fc459860038f0ed8b02bba5/cpp/src/arrow/ipc/reader.cc#L571-L575))
1 parent b02cc01 commit 7c13a60

4 files changed

Lines changed: 515 additions & 14 deletions

File tree

‎arrow/ipc/compression.go‎

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package ipc
1818

1919
import (
20+
"bytes"
21+
"fmt"
2022
"io"
23+
"sync"
2124

2225
"github.com/apache/arrow-go/v18/arrow/internal/debug"
2326
"github.com/apache/arrow-go/v18/arrow/internal/flatbuf"
@@ -81,27 +84,87 @@ func getCompressor(codec flatbuf.CompressionType) compressor {
8184
}
8285

8386
type decompressor interface {
84-
io.Reader
85-
Reset(io.Reader)
87+
// Decompress decodes src into dst, which must be sized to the exact
88+
// uncompressed length.
89+
Decompress(dst, src []byte) error
90+
// Close releases the decompressor; it must not be used afterwards.
91+
// Implementations may return it to a pool, so calling Close twice hands
92+
// the same decompressor to two owners.
8693
Close()
8794
}
8895

96+
var zstdDecompressorPool = sync.Pool{
97+
New: func() any {
98+
// WithDecoderConcurrency(1): Each pooled decoder is used by one goroutine at a time, so a single
99+
// block decoder is enough. The default would create up to four that
100+
// could never run in parallel;
101+
//
102+
// WithDecodeAllCapLimit(true): The cap limit bounds DecodeAll to cap(dst), so a frame claiming a
103+
// larger content size can't allocate beyond the buffer the caller
104+
// sized from the uncompressed length prefix.
105+
dec, err := zstd.NewReader(nil, zstd.WithDecoderConcurrency(1), zstd.WithDecodeAllCapLimit(true))
106+
if err != nil {
107+
panic(err)
108+
}
109+
return &zstdDecompressor{Decoder: dec}
110+
},
111+
}
112+
89113
type zstdDecompressor struct {
90114
*zstd.Decoder
91115
}
92116

93-
func (z *zstdDecompressor) Reset(r io.Reader) {
94-
if err := z.Decoder.Reset(r); err != nil {
95-
panic(err)
117+
func (z *zstdDecompressor) Decompress(dst, src []byte) error {
118+
if len(dst) == 0 {
119+
return nil
96120
}
121+
122+
// The decoder was created with WithDecodeAllCapLimit, so it decodes into
123+
// dst's own capacity and fails rather than allocating a larger slice.
124+
out, err := z.DecodeAll(src, dst[:0])
125+
if err != nil {
126+
return err
127+
}
128+
// Catch cases where the prefix says fewer bytes than the content, but the content fits in the dst's spare capacity
129+
if len(out) != len(dst) {
130+
return fmt.Errorf("arrow/ipc: zstd decompressed to %d bytes, expected %d", len(out), len(dst))
131+
}
132+
return nil
97133
}
98134

135+
// Close returns the decoder to the pool. z.Decoder.Close() is deliberately
136+
// not called: zstd.NewReader(nil) starts no goroutines, so a pooled decoder
137+
// only holds GC-reclaimable state and there is nothing to tear down. Closing
138+
// it would make the decoder unusable for the next borrower.
99139
func (z *zstdDecompressor) Close() {
100-
z.Decoder.Close()
140+
zstdDecompressorPool.Put(z)
101141
}
102142

103143
type lz4Decompressor struct {
104144
*lz4.Reader
145+
extra [1]byte // probe buffer for trailing data
146+
}
147+
148+
func (z *lz4Decompressor) Decompress(dst, src []byte) error {
149+
if len(dst) == 0 {
150+
return nil
151+
}
152+
153+
z.Reset(bytes.NewReader(src))
154+
if _, err := io.ReadFull(z.Reader, dst); err != nil {
155+
return err
156+
}
157+
// ReadFull stops once dst is full, so probe for one more byte: if there is
158+
// one, the frame decompresses to more than the length prefix claims, and we
159+
// reject it rather than silently truncate (mirrors the zstd path).
160+
n, err := z.Read(z.extra[:])
161+
if n > 0 {
162+
return fmt.Errorf("arrow/ipc: lz4 decompressed to more than the expected %d bytes", len(dst))
163+
}
164+
if err != io.EOF {
165+
return err
166+
}
167+
return nil
105168
}
106169

107170
func (z *lz4Decompressor) Close() {
@@ -111,13 +174,9 @@ func (z *lz4Decompressor) Close() {
111174
func getDecompressor(codec flatbuf.CompressionType) decompressor {
112175
switch codec {
113176
case flatbuf.CompressionTypeLZ4_FRAME:
114-
return &lz4Decompressor{lz4.NewReader(nil)}
177+
return &lz4Decompressor{Reader: lz4.NewReader(nil)}
115178
case flatbuf.CompressionTypeZSTD:
116-
dec, err := zstd.NewReader(nil)
117-
if err != nil {
118-
panic(err)
119-
}
120-
return &zstdDecompressor{dec}
179+
return zstdDecompressorPool.Get().(*zstdDecompressor)
121180
}
122181
return nil
123182
}

0 commit comments

Comments
 (0)