Skip to content

Commit

Permalink
zstd: If first block and 'final', encode direct. (#251)
Browse files Browse the repository at this point in the history
* zstd: If first block and 'final', encode direct.

If if writing header (ie first block) and it is the final block, use block compression instead of async.

Addition for #248
  • Loading branch information
klauspost authored Mar 27, 2020
1 parent d8b029a commit e8d1c04
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions zstd/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@ type encoder interface {
}

type encoderState struct {
w io.Writer
filling []byte
current []byte
previous []byte
encoder encoder
writing *blockEnc
err error
writeErr error
nWritten int64
headerWritten bool
eofWritten bool
w io.Writer
filling []byte
current []byte
previous []byte
encoder encoder
writing *blockEnc
err error
writeErr error
nWritten int64
headerWritten bool
eofWritten bool
fullFrameWritten bool

// This waitgroup indicates an encode is running.
wg sync.WaitGroup
Expand Down Expand Up @@ -114,6 +115,7 @@ func (e *Encoder) Reset(w io.Writer) {
s.encoder.Reset()
s.headerWritten = false
s.eofWritten = false
s.fullFrameWritten = false
s.w = w
s.err = nil
s.nWritten = 0
Expand Down Expand Up @@ -172,6 +174,22 @@ func (e *Encoder) nextBlock(final bool) error {
return fmt.Errorf("block > maxStoreBlockSize")
}
if !s.headerWritten {
// If we have a single block encode, do a sync compression.
if final && len(s.filling) > 0 {
s.current = e.EncodeAll(s.filling, s.current[:0])
var n2 int
n2, s.err = s.w.Write(s.current)
if s.err != nil {
return s.err
}
s.nWritten += int64(n2)
s.current = s.current[:0]
s.filling = s.filling[:0]
s.headerWritten = true
s.fullFrameWritten = true
return nil
}

var tmp [maxHeaderSize]byte
fh := frameHeader{
ContentSize: 0,
Expand Down Expand Up @@ -361,6 +379,9 @@ func (e *Encoder) Close() error {
if err != nil {
return err
}
if e.state.fullFrameWritten {
return s.err
}
s.wg.Wait()
s.wWg.Wait()

Expand Down

0 comments on commit e8d1c04

Please sign in to comment.