Skip to content

Commit

Permalink
feat: switch to file io and reduce max table size
Browse files Browse the repository at this point in the history
It's actually _faster_ (4x) when reading, on my system. But that may just be
because I use BTRFS + SSDs? I'm really not sure.
  • Loading branch information
Stebalien committed Mar 28, 2020
1 parent ba5d532 commit 818d0d7
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

badger "github.com/dgraph-io/badger"
options "github.com/dgraph-io/badger/options"
ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
logger "github.com/ipfs/go-log"
Expand Down Expand Up @@ -71,10 +72,30 @@ func init() {
GcDiscardRatio: 0.2,
GcInterval: 15 * time.Minute,
GcSleep: 10 * time.Second,
Options: badger.DefaultOptions(""),
Options: badger.LSMOnlyOptions(""),
}
// This is to optimize the database on close so it can be opened
// read-only and efficiently queried. We don't do that and hanging on
// stop isn't nice.
DefaultOptions.Options.CompactL0OnClose = false

// The alternative is "crash on start and tell the user to fix it". This
// will truncate corrupt and unsynced data, which we don't guarantee to
// persist anyways.
DefaultOptions.Options.Truncate = true

// Uses less memory, is no slower when writing, and is faster when
// reading (in some tests).
DefaultOptions.Options.ValueLogLoadingMode = options.FileIO

// Explicitly set this to mmap. This doesn't use much memory anyways.
DefaultOptions.Options.TableLoadingMode = options.MemoryMap

// Reduce this from 64MiB to 16MiB. That means badger will hold on to
// 20MiB by default instead of 80MiB.
//
// This does not appear to have a significant performance hit.
DefaultOptions.Options.MaxTableSize = 16 << 20
}

var _ ds.Datastore = (*Datastore)(nil)
Expand Down

0 comments on commit 818d0d7

Please sign in to comment.