diff --git a/pkg/compressio/compressio.go b/pkg/compressio/compressio.go index f8e92fa96..dfd86869d 100644 --- a/pkg/compressio/compressio.go +++ b/pkg/compressio/compressio.go @@ -690,27 +690,30 @@ func (w *Writer) Write(p []byte) (int, error) { pendingInline = 0 ) callback := func(c *chunk) error { - if pendingPre == 0 && pendingInline > 0 { + if pendingPre > 0 { + pendingPre-- + err := w.flush(c) + c.uncompressed.Reset() + bufPool.Put(c.uncompressed) + return err + } + if pendingInline > 0 { pendingInline-- return w.flush(c) } - if pendingPre > 0 { - pendingPre-- - } - err := w.flush(c) - c.uncompressed.Reset() - bufPool.Put(c.uncompressed) - return err + panic("both pendingPre and pendingInline exhausted") } for done := 0; done < len(p); { // Construct an inline buffer if we're doing an inline // encoding; see above regarding the bytes.MinRead constraint. + inline := false if w.buf.Len() == 0 && len(p) >= done+int(w.chunkSize) && len(p) >= done+bytes.MinRead { bufPool.Put(w.buf) // Return to the pool; never scheduled. w.buf = bytes.NewBuffer(p[done : done+int(w.chunkSize)]) done += int(w.chunkSize) pendingInline++ + inline = true } // Do we need to flush w.buf? Note that this case should be hit @@ -720,6 +723,9 @@ func (w *Writer) Write(p []byte) (int, error) { if err := w.schedule(newChunk(nil, nil, nil, w.buf), callback); err != nil { return done, err } + if !inline { + pendingPre++ + } // Reset the buffer, since this has now been scheduled // for compression. Note that this may be trampled // immediately by the bufPool.Put(w.buf) above if the