aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/objfile
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2022-10-26 18:12:39 +0100
committerPaulo Gomes <pjbgf@linux.com>2022-11-07 14:41:07 +0000
commit123cdde6f2f6282cb779e03745d384833ac1265b (patch)
treedc20c37ece501c3d66935c3832269393f2c18f63 /plumbing/format/objfile
parent08cffa1efade914020497a73907763e8d3707a77 (diff)
downloadgo-git-123cdde6f2f6282cb779e03745d384833ac1265b.tar.gz
Use Sync.Pool pointers to optimise memory usage
Signed-off-by: Paulo Gomes <pjbgf@linux.com>
Diffstat (limited to 'plumbing/format/objfile')
-rw-r--r--plumbing/format/objfile/writer.go15
1 files changed, 13 insertions, 2 deletions
diff --git a/plumbing/format/objfile/writer.go b/plumbing/format/objfile/writer.go
index 2a96a43..248f81b 100644
--- a/plumbing/format/objfile/writer.go
+++ b/plumbing/format/objfile/writer.go
@@ -5,6 +5,7 @@ import (
"errors"
"io"
"strconv"
+ "sync"
"github.com/go-git/go-git/v5/plumbing"
)
@@ -18,9 +19,9 @@ var (
// not close the underlying io.Writer.
type Writer struct {
raw io.Writer
- zlib io.WriteCloser
hasher plumbing.Hasher
multi io.Writer
+ zlib io.WriteCloser
closed bool
pending int64 // number of unwritten bytes
@@ -31,12 +32,21 @@ 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)
+
return &Writer{
raw: w,
- zlib: zlib.NewWriter(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.
@@ -100,6 +110,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)
if err := w.zlib.Close(); err != nil {
return err
}