Skip to content

Commit 82defe5

Browse files
committed
common/compress: internalize encoders, add length wrappers
1 parent cf19586 commit 82defe5

3 files changed

Lines changed: 125 additions & 70 deletions

File tree

‎common/bitutil/compress.go‎

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ package bitutil
1919
import "errors"
2020

2121
var (
22-
// ErrMissingData is returned from decompression if the byte referenced by
22+
// errMissingData is returned from decompression if the byte referenced by
2323
// the bitset header overflows the input data.
24-
ErrMissingData = errors.New("missing bytes on input")
24+
errMissingData = errors.New("missing bytes on input")
2525

26-
// ErrUnreferencedData is returned from decompression if not all bytes were used
26+
// errUnreferencedData is returned from decompression if not all bytes were used
2727
// up from the input data after decompressing it.
28-
ErrUnreferencedData = errors.New("extra bytes on input")
28+
errUnreferencedData = errors.New("extra bytes on input")
2929

30-
// ErrExceededTarget is returned from decompression if the bitset header has
30+
// errExceededTarget is returned from decompression if the bitset header has
3131
// more bits defined than the number of target buffer space available.
32-
ErrExceededTarget = errors.New("target data size exceeded")
32+
errExceededTarget = errors.New("target data size exceeded")
3333

34-
// ErrZeroContent is returned from decompression if a data byte referenced in
34+
// errZeroContent is returned from decompression if a data byte referenced in
3535
// the bitset header is actually a zero byte.
36-
ErrZeroContent = errors.New("zero byte in input content")
36+
errZeroContent = errors.New("zero byte in input content")
3737
)
3838

3939
// The compression algorithm implemented by CompressBytes and DecompressBytes is
@@ -55,8 +55,20 @@ var (
5555
// nonZeroBytes(data) contains the non-zero bytes of data in the same order
5656

5757
// CompressBytes compresses the input byte slice according to the sparse bitset
58-
// representation algorithm.
58+
// representation algorithm. If the result is bigger than the original input, no
59+
// compression is done.
5960
func CompressBytes(data []byte) []byte {
61+
if out := bitsetEncodeBytes(data); len(out) < len(data) {
62+
return out
63+
}
64+
cpy := make([]byte, len(data))
65+
copy(cpy, data)
66+
return cpy
67+
}
68+
69+
// bitsetEncodeBytes compresses the input byte slice according to the sparse
70+
// bitset representation algorithm.
71+
func bitsetEncodeBytes(data []byte) []byte {
6072
// Empty slices get compressed to nil
6173
if len(data) == 0 {
6274
return nil
@@ -81,27 +93,41 @@ func CompressBytes(data []byte) []byte {
8193
if len(nonZeroBytes) == 0 {
8294
return nil
8395
}
84-
return append(CompressBytes(nonZeroBitset), nonZeroBytes...)
96+
return append(bitsetEncodeBytes(nonZeroBitset), nonZeroBytes...)
8597
}
8698

87-
// DecompressBytes decompresses data with a known target size. In addition to the
88-
// decompressed output, the function returns the length of compressed input data
89-
// corresponding to the output as the input slice may be longer.
99+
// DecompressBytes decompresses data with a known target size. If the input data
100+
// matches the size of the target, it means no compression was done in the first
101+
// place.
90102
func DecompressBytes(data []byte, target int) ([]byte, error) {
91-
out, size, err := decompressBytes(data, target)
103+
if len(data) > target {
104+
return nil, errExceededTarget
105+
}
106+
if len(data) == target {
107+
cpy := make([]byte, len(data))
108+
copy(cpy, data)
109+
return cpy, nil
110+
}
111+
return bitsetDecodeBytes(data, target)
112+
}
113+
114+
// bitsetDecodeBytes decompresses data with a known target size.
115+
func bitsetDecodeBytes(data []byte, target int) ([]byte, error) {
116+
out, size, err := bitsetDecodePartialBytes(data, target)
92117
if err != nil {
93118
return nil, err
94119
}
95120
if size != len(data) {
96-
return nil, ErrUnreferencedData
121+
return nil, errUnreferencedData
97122
}
98123
return out, nil
99124
}
100125

101-
// decompressBytes decompresses data with a known target size. In addition to the
102-
// decompressed output, the function returns the length of compressed input data
103-
// corresponding to the output as the input slice may be longer.
104-
func decompressBytes(data []byte, target int) ([]byte, int, error) {
126+
// bitsetDecodePartialBytes decompresses data with a known target size, but does
127+
// not enforce consuming all the input bytes. In addition to the decompressed
128+
// output, the function returns the length of compressed input data corresponding
129+
// to the output as the input slice may be longer.
130+
func bitsetDecodePartialBytes(data []byte, target int) ([]byte, int, error) {
105131
// Sanity check 0 targets to avoid infinite recursion
106132
if target == 0 {
107133
return nil, 0, nil
@@ -119,22 +145,22 @@ func decompressBytes(data []byte, target int) ([]byte, int, error) {
119145
return decomp, 0, nil
120146
}
121147
// Decompress the bitset of set bytes and distribute the non zero bytes
122-
nonZeroBitset, ptr, err := decompressBytes(data, (target+7)/8)
148+
nonZeroBitset, ptr, err := bitsetDecodePartialBytes(data, (target+7)/8)
123149
if err != nil {
124150
return nil, ptr, err
125151
}
126152
for i := 0; i < 8*len(nonZeroBitset); i++ {
127153
if nonZeroBitset[i/8]&(1<<byte(7-i%8)) != 0 {
128154
// Make sure we have enough data to push into the correct slot
129155
if ptr >= len(data) {
130-
return nil, 0, ErrMissingData
156+
return nil, 0, errMissingData
131157
}
132158
if i >= len(decomp) {
133-
return nil, 0, ErrExceededTarget
159+
return nil, 0, errExceededTarget
134160
}
135161
// Make sure the data is valid and push into the slot
136162
if data[ptr] == 0 {
137-
return nil, 0, ErrZeroContent
163+
return nil, 0, errZeroContent
138164
}
139165
decomp[i] = data[ptr]
140166
ptr++

‎common/bitutil/compress_fuzz.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,36 @@ package bitutil
2020

2121
import "bytes"
2222

23-
// Fuzz implements a go-fuzz fuzzer method to test various compression method
23+
// Fuzz implements a go-fuzz fuzzer method to test various encoding method
2424
// invocations.
2525
func Fuzz(data []byte) int {
2626
if len(data) == 0 {
2727
return -1
2828
}
2929
if data[0]%2 == 0 {
30-
return fuzzCompress(data[1:])
30+
return fuzzEncode(data[1:])
3131
}
32-
return fuzzDecompress(data[1:])
32+
return fuzzDecode(data[1:])
3333
}
3434

35-
// fuzzCompress implements a go-fuzz fuzzer method to test the bit compression and
36-
// decompression algorithm.
37-
func fuzzCompress(data []byte) int {
38-
proc, _ := DecompressBytes(CompressBytes(data), len(data))
35+
// fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and
36+
// decoding algorithm.
37+
func fuzzEncode(data []byte) int {
38+
proc, _ := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
3939
if !bytes.Equal(data, proc) {
4040
panic("content mismatch")
4141
}
4242
return 0
4343
}
4444

45-
// fuzzDecompress implements a go-fuzz fuzzer method to test the bit decompression
46-
// and recompression algorithm.
47-
func fuzzDecompress(data []byte) int {
48-
blob, err := DecompressBytes(data, 1024)
45+
// fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and
46+
// reencoding algorithm.
47+
func fuzzDecode(data []byte) int {
48+
blob, err := bitsetDecodeBytes(data, 1024)
4949
if err != nil {
5050
return 0
5151
}
52-
if comp := CompressBytes(blob); !bytes.Equal(comp, data) {
52+
if comp := bitsetEncodeBytes(blob); !bytes.Equal(comp, data) {
5353
panic("content mismatch")
5454
}
5555
return 0

‎common/bitutil/compress_test.go‎

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
"github.com/ethereum/go-ethereum/common/hexutil"
2525
)
2626

27-
// Tests that data compression and decompression works correctly.
28-
func TestCompressCycle(t *testing.T) {
27+
// Tests that data bitset encoding and decoding works and is bijective.
28+
func TestEncodingCycle(t *testing.T) {
2929
tests := []string{
3030
// Tests generated by go-fuzz to maximize code coverage
3131
"0x000000000000000000",
@@ -50,7 +50,7 @@ func TestCompressCycle(t *testing.T) {
5050
for i, tt := range tests {
5151
data := hexutil.MustDecode(tt)
5252

53-
proc, err := DecompressBytes(CompressBytes(data), len(data))
53+
proc, err := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
5454
if err != nil {
5555
t.Errorf("test %d: failed to decompress compressed data: %v", i, err)
5656
continue
@@ -61,8 +61,8 @@ func TestCompressCycle(t *testing.T) {
6161
}
6262
}
6363

64-
// Tests that data decompression works
65-
func TestDecompress(t *testing.T) {
64+
// Tests that data bitset decoding and rencoding works and is bijective.
65+
func TestDecodingCycle(t *testing.T) {
6666
tests := []struct {
6767
size int
6868
input string
@@ -71,22 +71,22 @@ func TestDecompress(t *testing.T) {
7171
{size: 0, input: "0x"},
7272

7373
// Crashers generated by go-fuzz
74-
{size: 0, input: "0x0020", fail: ErrUnreferencedData},
75-
{size: 0, input: "0x30", fail: ErrUnreferencedData},
76-
{size: 1, input: "0x00", fail: ErrUnreferencedData},
77-
{size: 2, input: "0x07", fail: ErrMissingData},
78-
{size: 1024, input: "0x8000", fail: ErrZeroContent},
74+
{size: 0, input: "0x0020", fail: errUnreferencedData},
75+
{size: 0, input: "0x30", fail: errUnreferencedData},
76+
{size: 1, input: "0x00", fail: errUnreferencedData},
77+
{size: 2, input: "0x07", fail: errMissingData},
78+
{size: 1024, input: "0x8000", fail: errZeroContent},
7979

8080
// Tests generated by go-fuzz to maximize code coverage
81-
{size: 29490, input: "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e", fail: ErrMissingData},
82-
{size: 59395, input: "0x00", fail: ErrUnreferencedData},
83-
{size: 52574, input: "0x70706336346c65c0de", fail: ErrExceededTarget},
84-
{size: 42264, input: "0x07", fail: ErrMissingData},
85-
{size: 52, input: "0xa5045bad48f4", fail: ErrExceededTarget},
86-
{size: 52574, input: "0xc0de", fail: ErrMissingData},
81+
{size: 29490, input: "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e", fail: errMissingData},
82+
{size: 59395, input: "0x00", fail: errUnreferencedData},
83+
{size: 52574, input: "0x70706336346c65c0de", fail: errExceededTarget},
84+
{size: 42264, input: "0x07", fail: errMissingData},
85+
{size: 52, input: "0xa5045bad48f4", fail: errExceededTarget},
86+
{size: 52574, input: "0xc0de", fail: errMissingData},
8787
{size: 52574, input: "0x"},
88-
{size: 29490, input: "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e", fail: ErrMissingData},
89-
{size: 29491, input: "0x3973333930783e30783e", fail: ErrMissingData},
88+
{size: 29490, input: "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e", fail: errMissingData},
89+
{size: 29491, input: "0x3973333930783e30783e", fail: errMissingData},
9090

9191
{size: 1024, input: "0x808080608080"},
9292
{size: 1024, input: "0x808470705e3632383337363033313434303137393130306c6580ef46806380635a80"},
@@ -101,37 +101,66 @@ func TestDecompress(t *testing.T) {
101101
for i, tt := range tests {
102102
data := hexutil.MustDecode(tt.input)
103103

104-
orig, err := DecompressBytes(data, tt.size)
104+
orig, err := bitsetDecodeBytes(data, tt.size)
105105
if err != tt.fail {
106106
t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.fail)
107107
}
108108
if err != nil {
109109
continue
110110
}
111-
if comp := CompressBytes(orig); !bytes.Equal(comp, data) {
111+
if comp := bitsetEncodeBytes(orig); !bytes.Equal(comp, data) {
112112
t.Errorf("test %d: decompress/compress mismatch: have %x, want %x", i, comp, data)
113113
}
114114
}
115115
}
116116

117+
// TestCompression tests that compression works by returning either the bitset
118+
// encoded input, or the actual input if the bitset version is longer.
119+
func TestCompression(t *testing.T) {
120+
// Check the the compression returns the bitset encoding is shorter
121+
in := hexutil.MustDecode("0x4912385c0e7b64000000")
122+
out := hexutil.MustDecode("0x80fe4912385c0e7b64")
123+
124+
if data := CompressBytes(in); bytes.Compare(data, out) != 0 {
125+
t.Errorf("encoding mismatch for sparse data: have %x, want %x", data, out)
126+
}
127+
if data, err := DecompressBytes(out, len(in)); err != nil || bytes.Compare(data, in) != 0 {
128+
t.Errorf("decoding mismatch for sparse data: have %x, want %x, error %v", data, in, err)
129+
}
130+
// Check the the compression returns the input if the bitset encoding is longer
131+
in = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb")
132+
out = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb")
133+
134+
if data := CompressBytes(in); bytes.Compare(data, out) != 0 {
135+
t.Errorf("encoding mismatch for dense data: have %x, want %x", data, out)
136+
}
137+
if data, err := DecompressBytes(out, len(in)); err != nil || bytes.Compare(data, in) != 0 {
138+
t.Errorf("decoding mismatch for dense data: have %x, want %x, error %v", data, in, err)
139+
}
140+
// Check that decompressing a longer input than the target fails
141+
if _, err := DecompressBytes([]byte{0xc0, 0x01, 0x01}, 2); err != errExceededTarget {
142+
t.Errorf("decoding error mismatch for long data: have %v, want %v", err, errExceededTarget)
143+
}
144+
}
145+
117146
// Crude benchmark for compressing random slices of bytes.
118-
func BenchmarkCompress1KBVerySparse(b *testing.B) { benchmarkCompress(b, 1024, 0.0001) }
119-
func BenchmarkCompress2KBVerySparse(b *testing.B) { benchmarkCompress(b, 2048, 0.0001) }
120-
func BenchmarkCompress4KBVerySparse(b *testing.B) { benchmarkCompress(b, 4096, 0.0001) }
147+
func BenchmarkEncoding1KBVerySparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.0001) }
148+
func BenchmarkEncoding2KBVerySparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.0001) }
149+
func BenchmarkEncoding4KBVerySparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.0001) }
121150

122-
func BenchmarkCompress1KBSparse(b *testing.B) { benchmarkCompress(b, 1024, 0.001) }
123-
func BenchmarkCompress2KBSparse(b *testing.B) { benchmarkCompress(b, 2048, 0.001) }
124-
func BenchmarkCompress4KBSparse(b *testing.B) { benchmarkCompress(b, 4096, 0.001) }
151+
func BenchmarkEncoding1KBSparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.001) }
152+
func BenchmarkEncoding2KBSparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.001) }
153+
func BenchmarkEncoding4KBSparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.001) }
125154

126-
func BenchmarkCompress1KBDense(b *testing.B) { benchmarkCompress(b, 1024, 0.1) }
127-
func BenchmarkCompress2KBDense(b *testing.B) { benchmarkCompress(b, 2048, 0.1) }
128-
func BenchmarkCompress4KBDense(b *testing.B) { benchmarkCompress(b, 4096, 0.1) }
155+
func BenchmarkEncoding1KBDense(b *testing.B) { benchmarkEncoding(b, 1024, 0.1) }
156+
func BenchmarkEncoding2KBDense(b *testing.B) { benchmarkEncoding(b, 2048, 0.1) }
157+
func BenchmarkEncoding4KBDense(b *testing.B) { benchmarkEncoding(b, 4096, 0.1) }
129158

130-
func BenchmarkCompress1KBSaturated(b *testing.B) { benchmarkCompress(b, 1024, 0.5) }
131-
func BenchmarkCompress2KBSaturated(b *testing.B) { benchmarkCompress(b, 2048, 0.5) }
132-
func BenchmarkCompress4KBSaturated(b *testing.B) { benchmarkCompress(b, 4096, 0.5) }
159+
func BenchmarkEncoding1KBSaturated(b *testing.B) { benchmarkEncoding(b, 1024, 0.5) }
160+
func BenchmarkEncoding2KBSaturated(b *testing.B) { benchmarkEncoding(b, 2048, 0.5) }
161+
func BenchmarkEncoding4KBSaturated(b *testing.B) { benchmarkEncoding(b, 4096, 0.5) }
133162

134-
func benchmarkCompress(b *testing.B, bytes int, fill float64) {
163+
func benchmarkEncoding(b *testing.B, bytes int, fill float64) {
135164
// Generate a random slice of bytes to compress
136165
random := rand.NewSource(0) // reproducible and comparable
137166

@@ -143,10 +172,10 @@ func benchmarkCompress(b *testing.B, bytes int, fill float64) {
143172
bit := uint(random.Int63() % 8)
144173
data[idx] |= 1 << bit
145174
}
146-
// Reset the benchmark and measure compression/decompression
175+
// Reset the benchmark and measure encoding/decoding
147176
b.ResetTimer()
148177
b.ReportAllocs()
149178
for i := 0; i < b.N; i++ {
150-
DecompressBytes(CompressBytes(data), len(data))
179+
bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
151180
}
152181
}

0 commit comments

Comments
 (0)