Skip to content

Commit c3333a5

Browse files
author
Ibrahim Jarif
authored
Disable compression and set ZSTD Compression Level to 1 (#1191)
This PR - Disables compression. By default, badger does not use any compression. - Set default ZSTD compression level to 1 Level 15 is very slow for any practical use of badger. ``` no_compression-16 10 502848865 ns/op 165.46 MB/s zstd_compression/level_1-16 7 739037966 ns/op 112.58 MB/s zstd_compression/level_3-16 7 756950250 ns/op 109.91 MB/s zstd_compression/level_15-16 1 11135686219 ns/op 7.47 MB/s ```
1 parent 0acb3f6 commit c3333a5

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

options.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/dgraph-io/badger/v2/options"
2323
"github.com/dgraph-io/badger/v2/table"
24-
"github.com/dgraph-io/badger/v2/y"
2524
)
2625

2726
// Note: If you add a new option X make sure you also add a WithX method on Options.
@@ -102,11 +101,6 @@ type Options struct {
102101
// DefaultOptions sets a list of recommended options for good performance.
103102
// Feel free to modify these to suit your needs with the WithX methods.
104103
func DefaultOptions(path string) Options {
105-
defaultCompression := options.ZSTD
106-
// Use snappy as default compression algorithm if badger is built without CGO.
107-
if !y.CgoEnabled {
108-
defaultCompression = options.Snappy
109-
}
110104
return Options{
111105
Dir: path,
112106
ValueDir: path,
@@ -129,16 +123,19 @@ func DefaultOptions(path string) Options {
129123
CompactL0OnClose: true,
130124
KeepL0InMemory: true,
131125
VerifyValueChecksum: false,
132-
Compression: defaultCompression,
126+
Compression: options.None,
133127
MaxCacheSize: 1 << 30, // 1 GB
134-
// Benchmarking compression level against performance showed that level 15 gives
135-
// the best speed vs ratio tradeoff.
136-
// For a data size of 4KB we get
137-
// Level: 3 Ratio: 2.72 Time: 24112 n/s
138-
// Level: 10 Ratio: 2.95 Time: 75655 n/s
139-
// Level: 15 Ratio: 4.38 Time: 239042 n/s
140-
// See https://github.com/dgraph-io/badger/pull/1111#issue-338120757
141-
ZSTDCompressionLevel: 15,
128+
// The following benchmarks were done on a 4 KB block size (default block size). The
129+
// compression is ratio supposed to increase with increasing compression level but since the
130+
// input for compression algorithm is small (4 KB), we don't get significant benefit at
131+
// level 3.
132+
// no_compression-16 10 502848865 ns/op 165.46 MB/s -
133+
// zstd_compression/level_1-16 7 739037966 ns/op 112.58 MB/s 2.93
134+
// zstd_compression/level_3-16 7 756950250 ns/op 109.91 MB/s 2.72
135+
// zstd_compression/level_15-16 1 11135686219 ns/op 7.47 MB/s 4.38
136+
// Benchmark code can be found in table/builder_test.go file
137+
ZSTDCompressionLevel: 1,
138+
142139
// Nothing to read/write value log using standard File I/O
143140
// MemoryMap to mmap() the value log files
144141
// (2^30 - 1)*2 when mmapping < 2^31 - 1, max int32.
@@ -561,7 +558,18 @@ func (opt Options) WithInMemory(b bool) Options {
561558
// The ZSTD compression algorithm supports 20 compression levels. The higher the compression
562559
// level, the better is the compression ratio but lower is the performance. Lower levels
563560
// have better performance and higher levels have better compression ratios.
564-
// The default value of ZSTDCompressionLevel is 15.
561+
// We recommend using level 1 ZSTD Compression Level. Any level higher than 1 seems to
562+
// deteriorate badger's performance.
563+
// The following benchmarks were done on a 4 KB block size (default block size). The compression is
564+
// ratio supposed to increase with increasing compression level but since the input for compression
565+
// algorithm is small (4 KB), we don't get significant benefit at level 3. It is advised to write
566+
// your own benchmarks before choosing a compression algorithm or level.
567+
//
568+
// no_compression-16 10 502848865 ns/op 165.46 MB/s -
569+
// zstd_compression/level_1-16 7 739037966 ns/op 112.58 MB/s 2.93
570+
// zstd_compression/level_3-16 7 756950250 ns/op 109.91 MB/s 2.72
571+
// zstd_compression/level_15-16 1 11135686219 ns/op 7.47 MB/s 4.38
572+
// Benchmark code can be found in table/builder_test.go file
565573
func (opt Options) WithZSTDCompressionLevel(cLevel int) Options {
566574
opt.ZSTDCompressionLevel = cLevel
567575
return opt

table/builder_test.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,42 @@ func BenchmarkBuilder(b *testing.B) {
124124
vs := y.ValueStruct{Value: []byte(val)}
125125

126126
keysCount := 1300000 // This number of entries consumes ~64MB of memory.
127-
for i := 0; i < b.N; i++ {
128-
opts := Options{BlockSize: 4 * 1024, BloomFalsePositive: 0.01}
129-
builder := NewTableBuilder(opts)
130127

131-
for i := 0; i < keysCount; i++ {
132-
builder.Add(key(i), vs, 0)
133-
}
128+
bench := func(b *testing.B, opt *Options) {
129+
// KeyCount * (keySize + ValSize)
130+
b.SetBytes(int64(keysCount) * (32 + 32))
131+
for i := 0; i < b.N; i++ {
132+
opt.BlockSize = 4 * 1024
133+
opt.BloomFalsePositive = 0.01
134+
builder := NewTableBuilder(*opt)
135+
136+
for i := 0; i < keysCount; i++ {
137+
builder.Add(key(i), vs, 0)
138+
}
134139

135-
_ = builder.Finish()
140+
_ = builder.Finish()
141+
}
136142
}
143+
144+
b.Run("no compression", func(b *testing.B) {
145+
var opt Options
146+
opt.Compression = options.None
147+
bench(b, &opt)
148+
})
149+
b.Run("zstd compression", func(b *testing.B) {
150+
var opt Options
151+
opt.Compression = options.ZSTD
152+
b.Run("level 1", func(b *testing.B) {
153+
opt.ZSTDCompressionLevel = 1
154+
bench(b, &opt)
155+
})
156+
b.Run("level 3", func(b *testing.B) {
157+
opt.ZSTDCompressionLevel = 3
158+
bench(b, &opt)
159+
})
160+
b.Run("level 15", func(b *testing.B) {
161+
opt.ZSTDCompressionLevel = 15
162+
bench(b, &opt)
163+
})
164+
})
137165
}

0 commit comments

Comments
 (0)