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

Function EstimateBlockSize

s2/encode.go:64–100  ·  view source on GitHub ↗

EstimateBlockSize will perform a very fast compression without outputting the result and return the compressed output size. The function returns -1 if no improvement could be achieved. Using actual compression will most often produce better compression than the estimate.

(src []byte)

Source from the content-addressed store, hash-verified

62// The function returns -1 if no improvement could be achieved.
63// Using actual compression will most often produce better compression than the estimate.
64func EstimateBlockSize(src []byte) (d int) {
65 if len(src) <= inputMargin || int64(len(src)) > 0xffffffff {
66 return -1
67 }
68 if len(src) <= 1024 {
69 const sz, pool = 2048, 0
70 tmp, ok := estblockPool[pool].Get().(*[sz]byte)
71 if !ok {
72 tmp = &[sz]byte{}
73 }
74 race.WriteSlice(tmp[:])
75 defer estblockPool[pool].Put(tmp)
76
77 d = calcBlockSizeSmall(src, tmp)
78 } else {
79 const sz, pool = 32768, 1
80 tmp, ok := estblockPool[pool].Get().(*[sz]byte)
81 if !ok {
82 tmp = &[sz]byte{}
83 }
84 race.WriteSlice(tmp[:])
85 defer estblockPool[pool].Put(tmp)
86
87 d = calcBlockSize(src, tmp)
88 }
89
90 if d == 0 {
91 return -1
92 }
93 // Size of the varint encoded block size.
94 d += (bits.Len64(uint64(len(src))) + 7) / 7
95
96 if d >= len(src) {
97 return -1
98 }
99 return d
100}
101
102// EncodeBetter returns the encoded form of src. The returned slice may be a sub-
103// slice of dst if dst was large enough to hold the entire encoded block.

Callers 3

FuzzEncodingBlocksFunction · 0.85
TestEstimateBlockSizeFunction · 0.85
benchFileFunction · 0.85

Calls 3

WriteSliceFunction · 0.92
calcBlockSizeSmallFunction · 0.70
calcBlockSizeFunction · 0.70

Tested by 3

FuzzEncodingBlocksFunction · 0.68
TestEstimateBlockSizeFunction · 0.68
benchFileFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…