|
@@ -18,7 +18,6 @@ type Writer struct {
|
|
|
buf [19]byte // magic number(4) + header(flags(2)+[Size(8)+DictID(4)]+checksum(1)) does not exceed 19 bytes
|
|
buf [19]byte // magic number(4) + header(flags(2)+[Size(8)+DictID(4)]+checksum(1)) does not exceed 19 bytes
|
|
|
dst io.Writer // Destination.
|
|
dst io.Writer // Destination.
|
|
|
checksum xxh32.XXHZero // Frame checksum.
|
|
checksum xxh32.XXHZero // Frame checksum.
|
|
|
- zdata []byte // Compressed data.
|
|
|
|
|
data []byte // Data to be compressed.
|
|
data []byte // Data to be compressed.
|
|
|
idx int // Index into data.
|
|
idx int // Index into data.
|
|
|
hashtable [winSize]int // Hash table used in CompressBlock().
|
|
hashtable [winSize]int // Hash table used in CompressBlock().
|
|
@@ -46,13 +45,12 @@ func (z *Writer) writeHeader() error {
|
|
|
bSizeID := blockSizeValueToIndex(bSize)
|
|
bSizeID := blockSizeValueToIndex(bSize)
|
|
|
// Allocate the compressed/uncompressed buffers.
|
|
// Allocate the compressed/uncompressed buffers.
|
|
|
// The compressed buffer cannot exceed the uncompressed one.
|
|
// The compressed buffer cannot exceed the uncompressed one.
|
|
|
- if cap(z.zdata) < bSize {
|
|
|
|
|
|
|
+ if cap(z.data) < bSize {
|
|
|
// Only allocate if there is not enough capacity.
|
|
// Only allocate if there is not enough capacity.
|
|
|
// Allocate both buffers at once.
|
|
// Allocate both buffers at once.
|
|
|
- z.zdata = make([]byte, 2*bSize)
|
|
|
|
|
|
|
+ z.data = make([]byte, 2*bSize)
|
|
|
}
|
|
}
|
|
|
- z.data = z.zdata[:bSize] // Uncompressed buffer is the first half.
|
|
|
|
|
- z.zdata = z.zdata[:cap(z.zdata)][bSize:] // Compressed buffer is the second half.
|
|
|
|
|
|
|
+ z.data = z.data[:bSize] // Uncompressed buffer is the first half, compressed buffer is the second half.
|
|
|
z.idx = 0
|
|
z.idx = 0
|
|
|
|
|
|
|
|
// Size is optional.
|
|
// Size is optional.
|
|
@@ -159,13 +157,13 @@ func (z *Writer) compressBlock(data []byte) error {
|
|
|
var zn int
|
|
var zn int
|
|
|
var err error
|
|
var err error
|
|
|
|
|
|
|
|
|
|
+ zdata := z.data[z.Header.BlockMaxSize:cap(z.data)]
|
|
|
if level := z.Header.CompressionLevel; level != 0 {
|
|
if level := z.Header.CompressionLevel; level != 0 {
|
|
|
- zn, err = CompressBlockHC(data, z.zdata, level)
|
|
|
|
|
|
|
+ zn, err = CompressBlockHC(data, zdata, level)
|
|
|
} else {
|
|
} else {
|
|
|
- zn, err = CompressBlock(data, z.zdata, z.hashtable[:])
|
|
|
|
|
|
|
+ zn, err = CompressBlock(data, zdata, z.hashtable[:])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var zdata []byte
|
|
|
|
|
var bLen uint32
|
|
var bLen uint32
|
|
|
if debugFlag {
|
|
if debugFlag {
|
|
|
debug("block compression %d => %d", len(data), zn)
|
|
debug("block compression %d => %d", len(data), zn)
|
|
@@ -173,7 +171,7 @@ func (z *Writer) compressBlock(data []byte) error {
|
|
|
if err == nil && zn > 0 && zn < len(data) {
|
|
if err == nil && zn > 0 && zn < len(data) {
|
|
|
// Compressible and compressed size smaller than uncompressed: ok!
|
|
// Compressible and compressed size smaller than uncompressed: ok!
|
|
|
bLen = uint32(zn)
|
|
bLen = uint32(zn)
|
|
|
- zdata = z.zdata[:zn]
|
|
|
|
|
|
|
+ zdata = zdata[:zn]
|
|
|
} else {
|
|
} else {
|
|
|
// Uncompressed block.
|
|
// Uncompressed block.
|
|
|
bLen = uint32(len(data)) | compressedBlockFlag
|
|
bLen = uint32(len(data)) | compressedBlockFlag
|
|
@@ -261,7 +259,6 @@ func (z *Writer) Reset(w io.Writer) {
|
|
|
z.Header = Header{}
|
|
z.Header = Header{}
|
|
|
z.dst = w
|
|
z.dst = w
|
|
|
z.checksum.Reset()
|
|
z.checksum.Reset()
|
|
|
- z.zdata = z.zdata[:0]
|
|
|
|
|
z.data = z.data[:0]
|
|
z.data = z.data[:0]
|
|
|
z.idx = 0
|
|
z.idx = 0
|
|
|
}
|
|
}
|