From 2a82e5ab2a475484fb63da24ef8b96e1fc384f14 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 8 Jun 2026 05:39:15 +0000 Subject: internal/compress/zlib: Use lgo's safe sync.Pool --- internal/compress/zlib/reader.go | 19 +++++-------------- internal/compress/zlib/writer.go | 17 +++++------------ 2 files changed, 10 insertions(+), 26 deletions(-) (limited to 'internal/compress/zlib') diff --git a/internal/compress/zlib/reader.go b/internal/compress/zlib/reader.go index 25f422a6..f58a904a 100644 --- a/internal/compress/zlib/reader.go +++ b/internal/compress/zlib/reader.go @@ -38,10 +38,10 @@ import ( "errors" "hash" "io" - "sync" "lindenii.org/go/furgit/internal/compress/flate" "lindenii.org/go/lgo/intconv" + "lindenii.org/go/lgo/sync" ) const ( @@ -59,13 +59,9 @@ var ( ) //nolint:gochecknoglobals -var readerPool = sync.Pool{ - New: func() any { - r := new(Reader) - - return r - }, -} +var readerPool = sync.NewPool(func() *Reader { + return new(Reader) +}) // Reader reads and verifies one zlib stream. // @@ -93,12 +89,7 @@ func NewReader(r io.Reader) (*Reader, error) { // NewReaderDict ignores the dictionary if the compressed data does not refer to it. // If the compressed data refers to a different dictionary, NewReaderDict returns [ErrDictionary]. func NewReaderDict(r io.Reader, dict []byte) (*Reader, error) { - v := readerPool.Get() - - z, ok := v.(*Reader) - if !ok { - panic("zlib: pool returned unexpected type") - } + z := readerPool.Get() err := z.reset(r, dict) if err != nil { diff --git a/internal/compress/zlib/writer.go b/internal/compress/zlib/writer.go index ee3ce329..ae327e6d 100644 --- a/internal/compress/zlib/writer.go +++ b/internal/compress/zlib/writer.go @@ -9,9 +9,9 @@ import ( "fmt" "hash" "io" - "sync" "lindenii.org/go/furgit/internal/compress/flate" + "lindenii.org/go/lgo/sync" ) // These constants are copied from the [flate] package, so that code that imports @@ -38,11 +38,9 @@ type Writer struct { } //nolint:gochecknoglobals -var writerPool = sync.Pool{ - New: func() any { - return new(Writer) - }, -} +var writerPool = sync.NewPool(func() *Writer { + return new(Writer) +}) // NewWriter creates a new [Writer]. // Writes to the returned Writer are compressed and written to w. @@ -75,12 +73,7 @@ func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error) { return nil, fmt.Errorf("zlib: invalid compression level: %d", level) } - v := writerPool.Get() - - z, ok := v.(*Writer) - if !ok { - panic("zlib: pool returned unexpected type") - } + z := writerPool.Get() // flate.Writer can only be Reset with the same level/dictionary mode. // Reuse it only when the configuration is unchanged and dictionary-free. -- cgit v1.3.1-10-gc9f91