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

Method encodeLits

zstd/blockenc.go:337–430  ·  view source on GitHub ↗

encodeLits can be used if the block is only litLen.

(lits []byte, raw bool)

Source from the content-addressed store, hash-verified

335
336// encodeLits can be used if the block is only litLen.
337func (b *blockEnc) encodeLits(lits []byte, raw bool) error {
338 var bh blockHeader
339 bh.setLast(b.last)
340 bh.setSize(uint32(len(lits)))
341
342 // Don't compress extremely small blocks
343 if len(lits) < 8 || (len(lits) < 32 && b.dictLitEnc == nil) || raw {
344 if debugEncoder {
345 println("Adding RAW block, length", len(lits), "last:", b.last)
346 }
347 bh.setType(blockTypeRaw)
348 b.output = bh.appendTo(b.output)
349 b.output = append(b.output, lits...)
350 return nil
351 }
352
353 var (
354 out []byte
355 reUsed, single bool
356 err error
357 )
358 if b.dictLitEnc != nil {
359 b.litEnc.TransferCTable(b.dictLitEnc)
360 b.litEnc.Reuse = huff0.ReusePolicyAllow
361 b.dictLitEnc = nil
362 }
363 if len(lits) >= 1024 {
364 // Use 4 Streams.
365 out, reUsed, err = huff0.Compress4X(lits, b.litEnc)
366 } else if len(lits) > 16 {
367 // Use 1 stream
368 single = true
369 out, reUsed, err = huff0.Compress1X(lits, b.litEnc)
370 } else {
371 err = huff0.ErrIncompressible
372 }
373 if err == nil && len(out)+5 > len(lits) {
374 // If we are close, we may still be worse or equal to raw.
375 var lh literalsHeader
376 lh.setSizes(len(out), len(lits), single)
377 if len(out)+lh.size() >= len(lits) {
378 err = huff0.ErrIncompressible
379 }
380 }
381 switch err {
382 case huff0.ErrIncompressible:
383 if debugEncoder {
384 println("Adding RAW block, length", len(lits), "last:", b.last)
385 }
386 bh.setType(blockTypeRaw)
387 b.output = bh.appendTo(b.output)
388 b.output = append(b.output, lits...)
389 return nil
390 case huff0.ErrUseRLE:
391 if debugEncoder {
392 println("Adding RLE block, length", len(lits))
393 }
394 bh.setType(blockTypeRLE)

Callers 2

encodeMethod · 0.95
ConvertMethod · 0.80

Calls 12

setLastMethod · 0.95
setSizeMethod · 0.95
setTypeMethod · 0.95
appendToMethod · 0.95
setSizesMethod · 0.95
sizeMethod · 0.95
setTypeMethod · 0.95
appendToMethod · 0.95
Compress4XFunction · 0.92
Compress1XFunction · 0.92
printlnFunction · 0.85
TransferCTableMethod · 0.80

Tested by

no test coverage detected