Skip to content

Commit

Permalink
Disable cache by default (#1257)
Browse files Browse the repository at this point in the history
Disabling cache by default leads to better performance when compression
and encryption is disabled.

Result of read bench tool (data is not encrypted or compressed)
1. With 1 GB cache:
       	Average read speed              : 41.3833 MB/s
      	Total bytes read in 1 minute    : 2.4 GB

2. Without cache:
	Average read speed		: 58.86 MB/s
	Total bytes read in 1 minute	: 3.3 GB

This PR also fixes a small issue with the boolean condition in table.Open().
  • Loading branch information
Ibrahim Jarif authored Mar 16, 2020
1 parent eaf64c0 commit 91c31eb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 8 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func DefaultOptions(path string) Options {
KeepL0InMemory: true,
VerifyValueChecksum: false,
Compression: options.None,
MaxCacheSize: 1 << 30, // 1 GB
MaxCacheSize: 0,
MaxBfCacheSize: 0,
LoadBloomsOnOpen: true,
// The following benchmarks were done on a 4 KB block size (default block size). The
Expand Down Expand Up @@ -547,8 +547,13 @@ func (opt Options) WithChecksumVerificationMode(cvMode options.ChecksumVerificat
// WithMaxCacheSize returns a new Options value with MaxCacheSize set to the given value.
//
// This value specifies how much data cache should hold in memory. A small size of cache means lower
// memory consumption and lookups/iterations would take longer. Setting size to zero disables the
// memory consumption and lookups/iterations would take longer.
// It is recommended to use a cache if you're using compression or encryption.
// If compression and encryption both are disabled, adding a cache will lead to
// unnecessary overhead which will affect the read performance. Setting size to zero disables the
// cache altogether.
//
// Default value of MaxCacheSize is zero.
func (opt Options) WithMaxCacheSize(size int64) Options {
opt.MaxCacheSize = size
return opt
Expand Down Expand Up @@ -615,7 +620,7 @@ func (opt Options) WithMaxBfCacheSize(size int64) Options {
return opt
}

// WithLoadsBloomOnOpen returns a new Options value with LoadBloomsOnOpen set to the given value.
// WithLoadBloomsOnOpen returns a new Options value with LoadBloomsOnOpen set to the given value.
//
// Badger uses bloom filters to speed up key lookups. When LoadBloomsOnOpen is set
// to false, all bloom filters will be loaded on DB open. This is supposed to
Expand Down
4 changes: 2 additions & 2 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (t *Table) readIndex() error {
t.estimatedSize = index.EstimatedSize
t.blockIndex = index.Offsets

if !t.opt.LoadBloomsOnOpen {
if t.opt.LoadBloomsOnOpen {
t.bf, _ = t.readBloomFilter()
}

Expand Down Expand Up @@ -513,7 +513,7 @@ func (t *Table) DoesNotHave(hash uint64) bool {
if t.opt.BfCache == nil {
// Load bloomfilter into memory if the cache is absent.
if t.bf == nil {
y.AssertTrue(t.opt.LoadBloomsOnOpen)
y.AssertTrue(!t.opt.LoadBloomsOnOpen)
t.bf, _ = t.readBloomFilter()
}
return !t.bf.Has(hash)
Expand Down

0 comments on commit 91c31eb

Please sign in to comment.