Documentation
¶
Overview ¶
This file contains the specialisation of Decoder.Decompress4X and Decoder.Decompress1X that use an asm implementation of thir main loops.
Package huff0 provides fast huffman encoding as used in zstd.
See README.md at https://github.com/klauspost/compress/tree/master/huff0 for details.
Index ¶
- Constants
- Variables
- func Compress1X(in []byte, s *Scratch) (out []byte, reUsed bool, err error)
- func Compress4X(in []byte, s *Scratch) (out []byte, reUsed bool, err error)
- func EstimateSizes(in []byte, s *Scratch) (tableSz, dataSz, reuseSz int, err error)
- type Decoder
- type ReusePolicy
- type Scratch
- func (s *Scratch) AppendTable(dst []byte) ([]byte, error)
- func (s *Scratch) BuildCTable(count *[256]uint32) error
- func (s *Scratch) CanUseTable(hist *[256]uint32) bool
- func (s *Scratch) Decoder() *Decoder
- func (s *Scratch) Decompress1X(in []byte) (out []byte, err error)
- func (s *Scratch) Decompress4X(in []byte, dstSize int) (out []byte, err error)
- func (s *Scratch) EstimateSize(hist *[256]uint32) int
- func (s *Scratch) TransferCTable(src *Scratch)
Constants ¶
const (
// BlockSizeMax is maximum input size for a single block uncompressed.
BlockSizeMax = 1<<18 - 1
)
Variables ¶
var ( // ErrIncompressible is returned when input is judged to be too hard to compress. ErrIncompressible = errors.New("input is not compressible") // ErrUseRLE is returned from the compressor when the input is a single byte value repeated. ErrUseRLE = errors.New("input is single value repeated") // ErrTooBig is return if input is too large for a single block. ErrTooBig = errors.New("input too big") // ErrMaxDecodedSizeExceeded is return if input is too large for a single block. ErrMaxDecodedSizeExceeded = errors.New("maximum output size exceeded") )
Functions ¶
func Compress1X ¶
Compress1X will compress the input. The output can be decoded using Decompress1X. Supply a Scratch object. The scratch object contains state about re-use, So when sharing across independent encodes, be sure to set the re-use policy.
func Compress4X ¶
Compress4X will compress the input. The input is split into 4 independent blocks and compressed similar to Compress1X. The output can be decoded using Decompress4X. Supply a Scratch object. The scratch object contains state about re-use, So when sharing across independent encodes, be sure to set the re-use policy.
Types ¶
type Decoder ¶ added in v1.10.7
type Decoder struct {
// contains filtered or unexported fields
}
Decoder provides stateless decoding.
func (*Decoder) Decompress1X ¶ added in v1.10.7
Decompress1X will decompress a 1X encoded stream. The cap of the output buffer will be the maximum decompressed size. The length of the supplied input must match the end of a block exactly.
func (*Decoder) Decompress4X ¶ added in v1.10.7
Decompress4X will decompress a 4X encoded stream. The length of the supplied input must match the end of a block exactly. The *capacity* of the dst slice must match the destination size of the uncompressed data exactly.
type ReusePolicy ¶
type ReusePolicy uint8
const ( // ReusePolicyAllow will allow reuse if it produces smaller output. ReusePolicyAllow ReusePolicy = iota // ReusePolicyPrefer will re-use aggressively if possible. // This will not check if a new table will produce smaller output, // except if the current table is impossible to use or // compressed output is bigger than input. ReusePolicyPrefer // ReusePolicyNone will disable re-use of tables. // This is slightly faster than ReusePolicyAllow but may produce larger output. ReusePolicyNone // ReusePolicyMust must allow reuse and produce smaller output. ReusePolicyMust )
type Scratch ¶
type Scratch struct {
// Out is output buffer.
// If the scratch is re-used before the caller is done processing the output,
// set this field to nil.
// Otherwise the output buffer will be re-used for next Compression/Decompression step
// and allocation will be avoided.
Out []byte
// OutTable will contain the table data only, if a new table has been generated.
// Slice of the returned data.
OutTable []byte
// OutData will contain the compressed data.
// Slice of the returned data.
OutData []byte
// MaxDecodedSize will set the maximum allowed output size.
// This value will automatically be set to BlockSizeMax if not set.
// Decoders will return ErrMaxDecodedSizeExceeded is this limit is exceeded.
MaxDecodedSize int
// MaxSymbolValue will override the maximum symbol value of the next block.
MaxSymbolValue uint8
// TableLog will attempt to override the tablelog for the next block.
// Must be <= 11 and >= 5.
TableLog uint8
// Reuse will specify the reuse policy
Reuse ReusePolicy
// WantLogLess allows to specify a log 2 reduction that should at least be achieved,
// otherwise the block will be returned as incompressible.
// The reduction should then at least be (input size >> WantLogLess)
// If WantLogLess == 0 any improvement will do.
WantLogLess uint8
// contains filtered or unexported fields
}
func ReadTable ¶
ReadTable will read a table from the input. The size of the input may be larger than the table definition. Any content remaining after the table definition will be returned. If no Scratch is provided a new one is allocated. The returned Scratch can be used for encoding or decoding input using this table.
func (*Scratch) AppendTable ¶ added in v1.19.0
AppendTable serializes the table currently stored in prevTable (e.g. as installed by BuildCTable or carried over from a previous Compress call) into a self-delimiting zstd-style header and appends it to dst. The returned slice can be parsed back by ReadTable.
func (*Scratch) BuildCTable ¶ added in v1.19.0
BuildCTable builds a Huffman compression table from a precomputed symbol histogram and installs it as the previous (reuse) table on s.
After this call:
- EstimateSize/CanUseTable can probe the table against other histograms.
- Compress1X/Compress4X with Reuse = ReusePolicyMust will encode without emitting a new table header.
- TransferCTable can hand the table to a sibling Scratch.
count[i] is the number of occurrences of symbol i. The histogram must have at least 2 distinct non-zero symbols; ErrUseRLE is returned for a single symbol and an error is returned for an empty histogram.
func (*Scratch) CanUseTable ¶ added in v1.19.0
CanUseTable reports whether the table in prevTable can encode every non-zero symbol present in hist.
func (*Scratch) Decoder ¶ added in v1.10.7
Decoder will return a stateless decoder that can be used by multiple decompressors concurrently. Before this is called, the table must be initialized with ReadTable. The Decoder is still linked to the scratch buffer so that cannot be reused. However, it is safe to discard the scratch.
func (*Scratch) Decompress1X ¶
Decompress1X will decompress a 1X encoded stream. The length of the supplied input must match the end of a block exactly. Before this is called, the table must be initialized with ReadTable unless the encoder re-used the table. deprecated: Use the stateless Decoder() to get a concurrent version.
func (*Scratch) Decompress4X ¶
Decompress4X will decompress a 4X encoded stream. Before this is called, the table must be initialized with ReadTable unless the encoder re-used the table. The length of the supplied input must match the end of a block exactly. The destination size of the uncompressed data must be known and provided. deprecated: Use the stateless Decoder() to get a concurrent version.
func (*Scratch) EstimateSize ¶ added in v1.19.0
EstimateSize returns an estimated compressed payload size in bytes for the supplied histogram using the table currently stored in prevTable. It returns -1 when the table cannot encode every non-zero symbol of hist (i.e. when CanUseTable would return false). The estimate excludes the table header.
func (*Scratch) TransferCTable ¶ added in v1.11.0
TransferCTable will transfer the previously used compression table.