Skip to content

Commit

Permalink
db: reduce allocations of Iterator.prefixOrFullSeekKey
Browse files Browse the repository at this point in the history
  • Loading branch information
moredure authored and sumeerbhola committed Mar 3, 2021
1 parent e755a05 commit 070a80f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
34 changes: 18 additions & 16 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,11 +670,12 @@ func (d *DB) commitWrite(b *Batch, syncWG *sync.WaitGroup, syncErr *error) (*mem
}

type iterAlloc struct {
dbi Iterator
keyBuf []byte
merging mergingIter
mlevels [3 + numLevels]mergingIterLevel
levels [3 + numLevels]levelIter
dbi Iterator
keyBuf []byte
prefixOrFullSeekKey []byte
merging mergingIter
mlevels [3 + numLevels]mergingIterLevel
levels [3 + numLevels]levelIter
}

var iterAllocPool = sync.Pool{
Expand Down Expand Up @@ -709,17 +710,18 @@ func (d *DB) newIterInternal(batch *Batch, s *Snapshot, o *IterOptions) *Iterato
buf := iterAllocPool.Get().(*iterAlloc)
dbi := &buf.dbi
*dbi = Iterator{
alloc: buf,
cmp: d.cmp,
equal: d.equal,
iter: &buf.merging,
merge: d.merge,
split: d.split,
readState: readState,
keyBuf: buf.keyBuf,
batch: batch,
newIters: d.newIters,
seqNum: seqNum,
alloc: buf,
cmp: d.cmp,
equal: d.equal,
iter: &buf.merging,
merge: d.merge,
split: d.split,
readState: readState,
keyBuf: buf.keyBuf,
prefixOrFullSeekKey: buf.prefixOrFullSeekKey,
batch: batch,
newIters: d.newIters,
seqNum: seqNum,
}
if o != nil {
dbi.opts = *o
Expand Down
5 changes: 5 additions & 0 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,11 @@ func (i *Iterator) Close() error {
} else {
alloc.keyBuf = i.keyBuf
}
if cap(i.prefixOrFullSeekKey) >= maxKeyBufCacheSize {
alloc.prefixOrFullSeekKey = nil
} else {
alloc.prefixOrFullSeekKey = i.prefixOrFullSeekKey
}
*i = Iterator{}
iterAllocPool.Put(alloc)
}
Expand Down

0 comments on commit 070a80f

Please sign in to comment.