pgzip icon indicating copy to clipboard operation
pgzip copied to clipboard

buf leak in (*Writer).Close()

Open yoavj-arpeely opened this issue 1 year ago • 1 comments

I use a writer in a loop, which looks like this:

z := gzip.NewWriter(nil)
for {
  buf := new(bytes.Buffer)
  z.Reset(buf)
  msg := getMessage()
  z.Write(msg)
  z.Close()
  processMessage(msg)
}    

When closing z, Close() calls compressCurrent(), which gets two buffers from z.dstPool. The second one, in gzip.go:272, is never put back to z.dstPool, since z.init (called from z.Reset) sets z.currentBuffer = nil.

It means that dstPool is not fully utilized, and there are many redundant alllcations

yoavj-arpeely avatar Feb 28 '24 06:02 yoavj-arpeely

@yoavj-arpeely This library shouldn't be used for small compression tasks, so largely this shouldn't affect anything.

You are welcome to send a PR.

klauspost avatar Feb 28 '24 17:02 klauspost