diff options
| author | 2025-11-22 08:00:00 +0800 | |
|---|---|---|
| committer | 2025-11-22 08:00:00 +0800 | |
| commit | a504524ca695f04692f5a2cf49952ed039a6a348 (patch) | |
| tree | 4d0ab8463f1bd9be22aab2af7b7d38f88ef24c77 /internal | |
| parent | bufpool: Improve perf by using buckets of different size classes (diff) | |
| signature | No signature | |
bufpool: Return *Buffer
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/bufpool/buffers.go | 22 | ||||
| -rw-r--r-- | internal/flatex/decompress_bytes.go | 10 | ||||
| -rw-r--r-- | internal/zlibx/decompress.go | 26 |
3 files changed, 26 insertions, 32 deletions
diff --git a/internal/bufpool/buffers.go b/internal/bufpool/buffers.go index 24b29fb5..27c017bd 100644 --- a/internal/bufpool/buffers.go +++ b/internal/bufpool/buffers.go @@ -23,11 +23,10 @@ const ( // pooled buffer, the caller should invoke Release() to return it to the pool. // // Buffers must not be copied after first use; doing so can cause double-returns -// to the pool and data races. A zero-value Buffer is not valid for use. -// -//go:nocopy +// to the pool and data races. A zero-value Buffer is not valid for use. Use +// the pointer type (*Buffer) returned by Borrow/FromOwned to avoid accidental +// copies. type Buffer struct { - _ noCopy buf []byte pool poolIndex } @@ -70,28 +69,28 @@ var bufferPools = func() []sync.Pool { // unpooled buffer is allocated. // // The caller must call Release() when finished using the returned Buffer. -func Borrow(capHint int) Buffer { +func Borrow(capHint int) *Buffer { if capHint < DefaultBufferCap { capHint = DefaultBufferCap } classIdx, classCap, pooled := classFor(capHint) if !pooled { newBuf := make([]byte, 0, capHint) - return Buffer{buf: newBuf, pool: unpooled} + return &Buffer{buf: newBuf, pool: unpooled} } buf := bufferPools[classIdx].Get().(*[]byte) if cap(*buf) < classCap { *buf = make([]byte, 0, classCap) } slice := (*buf)[:0] - return Buffer{buf: slice, pool: poolIndex(classIdx)} + return &Buffer{buf: slice, pool: poolIndex(classIdx)} } // FromOwned constructs a Buffer from a caller-owned byte slice. The resulting // Buffer does not participate in pooling and will never be returned to the // internal pool when released. -func FromOwned(buf []byte) Buffer { - return Buffer{buf: buf, pool: unpooled} +func FromOwned(buf []byte) *Buffer { + return &Buffer{buf: buf, pool: unpooled} } // Resize adjusts the length of the buffer to n bytes. If n exceeds the current @@ -187,8 +186,3 @@ func (buf *Buffer) returnToPool() { tmp := buf.buf[:0] bufferPools[int(buf.pool)].Put(&tmp) } - -type noCopy struct{} - -func (*noCopy) Lock() {} -func (*noCopy) Unlock() {} diff --git a/internal/flatex/decompress_bytes.go b/internal/flatex/decompress_bytes.go index 5e29f82d..ce6d0558 100644 --- a/internal/flatex/decompress_bytes.go +++ b/internal/flatex/decompress_bytes.go @@ -25,26 +25,26 @@ var bufferDecompressorPool = sync.Pool{ // Decompress inflates the provided DEFLATE stream and returns the full output // in a pooled bufpool.Buffer along with the number of consumed bytes from src. -func Decompress(src []byte) (bufpool.Buffer, int, error) { +func Decompress(src []byte) (*bufpool.Buffer, int, error) { return DecompressDictSized(src, nil, 0) } // DecompressDict inflates the provided DEFLATE stream using dict as the preset // dictionary and returns the full output in a pooled bufpool.Buffer. The second // returned value reports how many bytes of src were consumed. -func DecompressDict(src []byte, dict []byte) (bufpool.Buffer, int, error) { +func DecompressDict(src []byte, dict []byte) (*bufpool.Buffer, int, error) { return DecompressDictSized(src, dict, 0) } // DecompressDictSized is like DecompressDict but allows providing an expected // output size to pre-size the destination buffer and avoid repeated growth. // A non-positive sizeHint falls back to the default buffer capacity. -func DecompressDictSized(src []byte, dict []byte, sizeHint int) (bufpool.Buffer, int, error) { +func DecompressDictSized(src []byte, dict []byte, sizeHint int) (*bufpool.Buffer, int, error) { d := bufferDecompressorPool.Get().(*bufferDecompressor) defer bufferDecompressorPool.Put(d) if err := d.inflater.reset(src, dict); err != nil { - return bufpool.Buffer{}, 0, err + return nil, 0, err } out := bufpool.Borrow(sizeHint) @@ -61,7 +61,7 @@ func DecompressDictSized(src []byte, dict []byte, sizeHint int) (bufpool.Buffer, return out, d.inflater.pos, nil } out.Release() - return bufpool.Buffer{}, 0, d.inflater.err + return nil, 0, d.inflater.err } d.inflater.step(&d.inflater) if d.inflater.err != nil && len(d.inflater.toRead) == 0 { diff --git a/internal/zlibx/decompress.go b/internal/zlibx/decompress.go index c6eb65e5..68a92587 100644 --- a/internal/zlibx/decompress.go +++ b/internal/zlibx/decompress.go @@ -11,71 +11,71 @@ import ( // Decompress inflates the provided zlib wrapped stream and returns the // uncompressed data inside a pooled bufpool.Buffer. -func Decompress(src []byte) (bufpool.Buffer, error) { +func Decompress(src []byte) (*bufpool.Buffer, error) { return DecompressSized(src, 0) } // DecompressSized inflates the provided zlib stream, using sizeHint to // preallocate the output buffer when known (e.g. packfile entries). -func DecompressSized(src []byte, sizeHint int) (bufpool.Buffer, error) { +func DecompressSized(src []byte, sizeHint int) (*bufpool.Buffer, error) { return DecompressDictSized(src, nil, sizeHint) } // DecompressDict is like Decompress but accepts a preset dictionary. The // dictionary must match the checksum embedded in the stream if the dictionary // flag is present. -func DecompressDict(src []byte, dict []byte) (bufpool.Buffer, error) { +func DecompressDict(src []byte, dict []byte) (*bufpool.Buffer, error) { return DecompressDictSized(src, dict, 0) } // DecompressDictSized is like DecompressDict but allows providing an expected // uncompressed size to avoid buffer growth copies. -func DecompressDictSized(src []byte, dict []byte, sizeHint int) (bufpool.Buffer, error) { +func DecompressDictSized(src []byte, dict []byte, sizeHint int) (*bufpool.Buffer, error) { if len(src) < 6 { - return bufpool.Buffer{}, io.ErrUnexpectedEOF + return nil, io.ErrUnexpectedEOF } cmf := src[0] flg := src[1] if (cmf&0x0f != zlibDeflate) || (cmf>>4 > zlibMaxWindow) || (binary.BigEndian.Uint16(src[:2])%31 != 0) { - return bufpool.Buffer{}, ErrHeader + return nil, ErrHeader } offset := 2 haveDict := flg&0x20 != 0 if haveDict { if len(src) < offset+4 { - return bufpool.Buffer{}, io.ErrUnexpectedEOF + return nil, io.ErrUnexpectedEOF } if dict == nil { - return bufpool.Buffer{}, ErrDictionary + return nil, ErrDictionary } checksum := binary.BigEndian.Uint32(src[offset : offset+4]) if checksum != adler32.Checksum(dict) { - return bufpool.Buffer{}, ErrDictionary + return nil, ErrDictionary } offset += 4 } if len(src[offset:]) < 4 { - return bufpool.Buffer{}, io.ErrUnexpectedEOF + return nil, io.ErrUnexpectedEOF } deflateData := src[offset:] out, consumed, err := flatex.DecompressDictSized(deflateData, dict, sizeHint) if err != nil { - return bufpool.Buffer{}, err + return nil, err } checksumPos := offset + consumed if checksumPos+4 > len(src) { out.Release() - return bufpool.Buffer{}, io.ErrUnexpectedEOF + return nil, io.ErrUnexpectedEOF } expected := binary.BigEndian.Uint32(src[checksumPos : checksumPos+4]) if expected != adler32.Checksum(out.Bytes()) { out.Release() - return bufpool.Buffer{}, ErrChecksum + return nil, ErrChecksum } return out, nil } |
