-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Similar to decompressing, you can reuse *lz4.Writer and *gzip.Writer objects by calling Reset() between uses. Add two more sync.Pools so we can reuse writers easily. The gzip writer is only reused when using the default compression. We could maintain a separate sync.Pool for each gzip compression level, but that adds more complexity and gzip wasn't that slow without this optimization. When producing 100 msgs/second across 10 producers, CPU usage dropped from 70% to 4% on my machine (lz4). Gzip dropped from 15% to 5%.
- Loading branch information
Muir Manders
committed
Oct 5, 2018
1 parent
afe6b1d
commit f352e5c
Showing
4 changed files
with
88 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package sarama | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/eapache/go-xerial-snappy" | ||
"github.com/pierrec/lz4" | ||
) | ||
|
||
var ( | ||
lz4WriterPool = sync.Pool{ | ||
New: func() interface{} { | ||
return lz4.NewWriter(nil) | ||
}, | ||
} | ||
|
||
gzipWriterPool = sync.Pool{ | ||
New: func() interface{} { | ||
return gzip.NewWriter(nil) | ||
}, | ||
} | ||
) | ||
|
||
func compress(cc CompressionCodec, level int, data []byte) ([]byte, error) { | ||
switch cc { | ||
case CompressionNone: | ||
return data, nil | ||
case CompressionGZIP: | ||
var ( | ||
err error | ||
buf bytes.Buffer | ||
writer *gzip.Writer | ||
) | ||
if level != CompressionLevelDefault { | ||
writer, err = gzip.NewWriterLevel(&buf, level) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
writer = gzipWriterPool.Get().(*gzip.Writer) | ||
defer gzipWriterPool.Put(writer) | ||
writer.Reset(&buf) | ||
} | ||
if _, err := writer.Write(data); err != nil { | ||
return nil, err | ||
} | ||
if err := writer.Close(); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
case CompressionSnappy: | ||
return snappy.Encode(data), nil | ||
case CompressionLZ4: | ||
writer := lz4WriterPool.Get().(*lz4.Writer) | ||
defer lz4WriterPool.Put(writer) | ||
|
||
var buf bytes.Buffer | ||
writer.Reset(&buf) | ||
|
||
if _, err := writer.Write(data); err != nil { | ||
return nil, err | ||
} | ||
if err := writer.Close(); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
case CompressionZSTD: | ||
return zstdCompressLevel(nil, data, level) | ||
default: | ||
return nil, PacketEncodingError{fmt.Sprintf("unsupported compression codec (%d)", cc)} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters