Skip to content

Commit

Permalink
Performance Improvements to block iterator (hypermodeinc#977)
Browse files Browse the repository at this point in the history
Signed-off-by: thomassong <[email protected]>
  • Loading branch information
mYmNeo committed Jan 12, 2023
1 parent 612e2f4 commit 1a899f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
31 changes: 16 additions & 15 deletions table/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ type blockIterator struct {
last header // The last header we saw.
}

func (itr *blockIterator) Reset() {
func (itr *blockIterator) setBlock(b *block) {
itr.pos = 0
itr.err = nil
itr.baseKey = []byte{}
itr.key = []byte{}
itr.val = []byte{}
itr.baseKey = itr.baseKey[:0]
itr.key = itr.key[:0]
itr.val = itr.val[:0]
itr.init = false
itr.last = header{}
itr.data = b.data
}

func (itr *blockIterator) Init() {
Expand Down Expand Up @@ -77,7 +78,7 @@ func (itr *blockIterator) Seek(key []byte, whence int) {

switch whence {
case origin:
itr.Reset()
// We don't need to do anything. startIndex is already at 0
case current:
}

Expand Down Expand Up @@ -195,7 +196,7 @@ func (itr *blockIterator) Value() []byte {
type Iterator struct {
t *Table
bpos int
bi *blockIterator
bi blockIterator
err error

// Internally, Iterator is bidirectional. However, we only expose the
Expand Down Expand Up @@ -238,7 +239,7 @@ func (itr *Iterator) seekToFirst() {
itr.err = err
return
}
itr.bi = block.NewIterator()
itr.bi.setBlock(block)
itr.bi.SeekToFirst()
itr.err = itr.bi.Error()
}
Expand All @@ -255,7 +256,7 @@ func (itr *Iterator) seekToLast() {
itr.err = err
return
}
itr.bi = block.NewIterator()
itr.bi.setBlock(block)
itr.bi.SeekToLast()
itr.err = itr.bi.Error()
}
Expand All @@ -267,7 +268,7 @@ func (itr *Iterator) seekHelper(blockIdx int, key []byte) {
itr.err = err
return
}
itr.bi = block.NewIterator()
itr.bi.setBlock(block)
itr.bi.Seek(key, origin)
itr.err = itr.bi.Error()
}
Expand Down Expand Up @@ -334,13 +335,13 @@ func (itr *Iterator) next() {
return
}

if itr.bi == nil {
if len(itr.bi.data) == 0 {
block, err := itr.t.block(itr.bpos)
if err != nil {
itr.err = err
return
}
itr.bi = block.NewIterator()
itr.bi.setBlock(block)
itr.bi.SeekToFirst()
itr.err = itr.bi.Error()
return
Expand All @@ -349,7 +350,7 @@ func (itr *Iterator) next() {
itr.bi.Next()
if !itr.bi.Valid() {
itr.bpos++
itr.bi = nil
itr.bi.data = nil
itr.next()
return
}
Expand All @@ -362,13 +363,13 @@ func (itr *Iterator) prev() {
return
}

if itr.bi == nil {
if len(itr.bi.data) == 0 {
block, err := itr.t.block(itr.bpos)
if err != nil {
itr.err = err
return
}
itr.bi = block.NewIterator()
itr.bi.setBlock(block)
itr.bi.SeekToLast()
itr.err = itr.bi.Error()
return
Expand All @@ -377,7 +378,7 @@ func (itr *Iterator) prev() {
itr.bi.Prev()
if !itr.bi.Valid() {
itr.bpos--
itr.bi = nil
itr.bi.data = nil
itr.prev()
return
}
Expand Down
9 changes: 5 additions & 4 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import (
"sync/atomic"

"github.com/AndreasBriese/bbloom"
"github.com/pkg/errors"

"github.com/dgraph-io/badger/options"
"github.com/dgraph-io/badger/y"
"github.com/pkg/errors"
)

const fileSuffix = ".sst"
Expand Down Expand Up @@ -283,14 +284,14 @@ func (t *Table) readIndex() error {
return nil
}

func (t *Table) block(idx int) (block, error) {
func (t *Table) block(idx int) (*block, error) {
y.AssertTruef(idx >= 0, "idx=%d", idx)
if idx >= len(t.blockIndex) {
return block{}, errors.New("block out of index")
return nil, errors.New("block out of index")
}

ko := t.blockIndex[idx]
blk := block{
blk := &block{
offset: ko.offset,
}
var err error
Expand Down

0 comments on commit 1a899f0

Please sign in to comment.