MCPcopy
hub / github.com/klauspost/compress / encode

Method encode

zstd/blockenc.go:481–827  ·  view source on GitHub ↗

encode will encode the block and append the output in b.output. Previous offset codes must be pushed if more blocks are expected.

(org []byte, raw, rawAllLits bool)

Source from the content-addressed store, hash-verified

479// encode will encode the block and append the output in b.output.
480// Previous offset codes must be pushed if more blocks are expected.
481func (b *blockEnc) encode(org []byte, raw, rawAllLits bool) error {
482 if len(b.sequences) == 0 {
483 return b.encodeLits(b.literals, rawAllLits)
484 }
485 if len(b.sequences) == 1 && len(org) > 0 && len(b.literals) <= 1 {
486 // Check common RLE cases.
487 seq := b.sequences[0]
488 if seq.litLen == uint32(len(b.literals)) && seq.offset-3 == 1 {
489 // Offset == 1 and 0 or 1 literals.
490 b.encodeRLE(org[0], b.sequences[0].matchLen+zstdMinMatch+seq.litLen)
491 return nil
492 }
493 }
494
495 // We want some difference to at least account for the headers.
496 saved := b.size - len(b.literals) - (b.size >> 6)
497 if saved < 16 {
498 if org == nil {
499 return errIncompressible
500 }
501 b.popOffsets()
502 return b.encodeLits(org, rawAllLits)
503 }
504
505 var bh blockHeader
506 var lh literalsHeader
507 bh.setLast(b.last)
508 bh.setType(blockTypeCompressed)
509 // Store offset of the block header. Needed when we know the size.
510 bhOffset := len(b.output)
511 b.output = bh.appendTo(b.output)
512
513 var (
514 out []byte
515 reUsed, single bool
516 err error
517 )
518 if b.dictLitEnc != nil {
519 b.litEnc.TransferCTable(b.dictLitEnc)
520 b.litEnc.Reuse = huff0.ReusePolicyAllow
521 b.dictLitEnc = nil
522 }
523 if len(b.literals) >= 1024 && !raw {
524 // Use 4 Streams.
525 out, reUsed, err = huff0.Compress4X(b.literals, b.litEnc)
526 } else if len(b.literals) > 16 && !raw {
527 // Use 1 stream
528 single = true
529 out, reUsed, err = huff0.Compress1X(b.literals, b.litEnc)
530 } else {
531 err = huff0.ErrIncompressible
532 }
533
534 if err == nil && len(out)+5 > len(b.literals) {
535 // If we are close, we may still be worse or equal to raw.
536 var lh literalsHeader
537 lh.setSize(len(b.literals))
538 szRaw := lh.size()

Callers 4

compressJobMethod · 0.45
nextBlockMethod · 0.45
encodeAllMethod · 0.45
ConvertMethod · 0.45

Calls 15

encodeLitsMethod · 0.95
encodeRLEMethod · 0.95
popOffsetsMethod · 0.95
setLastMethod · 0.95
setTypeMethod · 0.95
appendToMethod · 0.95
setSizeMethod · 0.95
sizeMethod · 0.95
setSizesMethod · 0.95
setTypeMethod · 0.95
appendToMethod · 0.95
genCodesMethod · 0.95

Tested by

no test coverage detected