Skip to content

Commit

Permalink
let's go fast
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Jan 7, 2024
1 parent 019ce53 commit e454ccb
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"path/filepath"
"sync"

"github.com/cockroachdb/pebble"
)
Expand Down Expand Up @@ -67,6 +68,12 @@ type PebbleDB struct {
db *pebble.DB
}

var iteratorPool = sync.Pool{
New: func() interface{} {
return &pebbleDBIterator{}
},
}

var _ DB = (*PebbleDB)(nil)

func NewPebbleDB(name string, dir string) (*PebbleDB, error) {
Expand Down Expand Up @@ -349,6 +356,17 @@ type pebbleDBIterator struct {
var _ Iterator = (*pebbleDBIterator)(nil)

func newPebbleDBIterator(source *pebble.Iterator, start, end []byte, isReverse bool) *pebbleDBIterator {
item := iteratorPool.Get()
itr, ok := item.(*pebbleDBIterator)
if !ok {
panic("item in iteratorPool is not of type *pebbleDBIterator")
}
itr.source = source
itr.start = start
itr.end = end
itr.isReverse = isReverse
itr.isInvalid = false

if isReverse {
if end == nil {
source.Last()
Expand All @@ -358,13 +376,7 @@ func newPebbleDBIterator(source *pebble.Iterator, start, end []byte, isReverse b
source.First()
}
}
return &pebbleDBIterator{
source: source,
start: start,
end: end,
isReverse: isReverse,
isInvalid: false,
}
return itr
}

// Domain implements Iterator.
Expand Down Expand Up @@ -448,6 +460,12 @@ func (itr *pebbleDBIterator) Close() error {
if err != nil {
return err
}
itr.source = nil
itr.start = nil
itr.end = nil
itr.isReverse = false
itr.isInvalid = true
iteratorPool.Put(itr)
return nil
}

Expand Down

0 comments on commit e454ccb

Please sign in to comment.