aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/objfile
diff options
context:
space:
mode:
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
}