aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/objfile/writer.go
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2022-11-04 12:44:40 +0000
committerPaulo Gomes <pjbgf@linux.com>2022-11-07 14:49:56 +0000
commit9490da0f86a12269abb2099e2ead1f20eec166d2 (patch)
tree1b6d079443203393f9e321aa91bb247ea4984cbb /plumbing/format/objfile/writer.go
parentffa7e69efb8c4ba8d4e08ec4c65e49e2228fd88b (diff)
downloadgo-git-9490da0f86a12269abb2099e2ead1f20eec166d2.tar.gz
Optimize zlib reader and consolidate sync.pools
Expands on the optimisations from https://github.com/fluxcd/go-git/pull/5 and ensures that zlib reader does not need to recreate a deflate dictionary at every use. The use of sync pools was consolidated into a new sync utils package. name old time/op new time/op delta Parser-16 7.51ms ± 3% 7.71ms ± 6% ~ (p=0.222 n=5+5) name old alloc/op new alloc/op delta Parser-16 4.65MB ± 3% 1.90MB ± 3% -59.06% (p=0.008 n=5+5) name old allocs/op new allocs/op delta Parser-16 3.48k ± 0% 3.32k ± 0% -4.57% (p=0.016 n=5+4) Signed-off-by: Paulo Gomes <pjbgf@linux.com>
Diffstat (limited to 'plumbing/format/objfile/writer.go')
-rw-r--r--plumbing/format/objfile/writer.go16
1 files changed, 4 insertions, 12 deletions
diff --git a/plumbing/format/objfile/writer.go b/plumbing/format/objfile/writer.go
index 248f81b..0d0f154 100644
--- a/plumbing/format/objfile/writer.go
+++ b/plumbing/format/objfile/writer.go
@@ -5,9 +5,9 @@ import (
"errors"
"io"
"strconv"
- "sync"
"github.com/go-git/go-git/v5/plumbing"
+ "github.com/go-git/go-git/v5/utils/sync"
)
var (
@@ -21,7 +21,7 @@ type Writer struct {
raw io.Writer
hasher plumbing.Hasher
multi io.Writer
- zlib io.WriteCloser
+ zlib *zlib.Writer
closed bool
pending int64 // number of unwritten bytes
@@ -32,21 +32,13 @@ type Writer struct {
// The returned Writer implements io.WriteCloser. Close should be called when
// finished with the Writer. Close will not close the underlying io.Writer.
func NewWriter(w io.Writer) *Writer {
- zlib := zlibPool.Get().(*zlib.Writer)
- zlib.Reset(w)
-
+ zlib := sync.GetZlibWriter(w)
return &Writer{
raw: w,
zlib: zlib,
}
}
-var zlibPool = sync.Pool{
- New: func() interface{} {
- return zlib.NewWriter(nil)
- },
-}
-
// WriteHeader writes the type and the size and prepares to accept the object's
// contents. If an invalid t is provided, plumbing.ErrInvalidType is returned. If a
// negative size is provided, ErrNegativeSize is returned.
@@ -110,7 +102,7 @@ func (w *Writer) Hash() plumbing.Hash {
// Calling Close does not close the wrapped io.Writer originally passed to
// NewWriter.
func (w *Writer) Close() error {
- defer zlibPool.Put(w.zlib)
+ defer sync.PutZlibWriter(w.zlib)
if err := w.zlib.Close(); err != nil {
return err
}