Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement NewBatchWithSize method on DB interface to pre-allocate buffer #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion goleveldb.go
Expand Up @@ -172,7 +172,12 @@ func (db *GoLevelDB) ForceCompact(start, limit []byte) error {

// NewBatch implements DB.
func (db *GoLevelDB) NewBatch() Batch {
return newGoLevelDBBatch(db)
return newGoLevelDBBatchWithSize(db, 0)
}

// NewBatchWithSize implements DB.
func (db *GoLevelDB) NewBatchWithSize(size int) Batch {
return newGoLevelDBBatchWithSize(db, size)
}

// Iterator implements DB.
Expand Down
4 changes: 2 additions & 2 deletions goleveldb_batch.go
Expand Up @@ -12,10 +12,10 @@ type goLevelDBBatch struct {

var _ Batch = (*goLevelDBBatch)(nil)

func newGoLevelDBBatch(db *GoLevelDB) *goLevelDBBatch {
func newGoLevelDBBatchWithSize(db *GoLevelDB, size int) *goLevelDBBatch {
return &goLevelDBBatch{
db: db,
batch: new(leveldb.Batch),
batch: leveldb.MakeBatch(size),
}
}

Expand Down
7 changes: 6 additions & 1 deletion memdb.go
Expand Up @@ -170,7 +170,12 @@ func (db *MemDB) Stats() map[string]string {

// NewBatch implements DB.
func (db *MemDB) NewBatch() Batch {
return newMemDBBatch(db)
return newMemDBBatchWithSize(db, 0)
}

// NewBatchWithSize implements DB.
func (db *MemDB) NewBatchWithSize(size int) Batch {
return newMemDBBatchWithSize(db, size)
}

// Iterator implements DB.
Expand Down
6 changes: 3 additions & 3 deletions memdb_batch.go
Expand Up @@ -24,11 +24,11 @@ type memDBBatch struct {

var _ Batch = (*memDBBatch)(nil)

// newMemDBBatch creates a new memDBBatch
func newMemDBBatch(db *MemDB) *memDBBatch {
// newMemDBBatchWithSize creates a new memDBBatch
func newMemDBBatchWithSize(db *MemDB, size int) *memDBBatch {
return &memDBBatch{
db: db,
ops: []operation{},
ops: make([]operation, 0, size),
}
}

Expand Down
8 changes: 8 additions & 0 deletions prefixdb.go
Expand Up @@ -162,6 +162,14 @@ func (pdb *PrefixDB) NewBatch() Batch {
return newPrefixBatch(pdb.prefix, pdb.db.NewBatch())
}

// NewBatchWithSize implements DB.
func (pdb *PrefixDB) NewBatchWithSize(size int) Batch {
pdb.mtx.Lock()
defer pdb.mtx.Unlock()

return newPrefixBatch(pdb.prefix, pdb.db.NewBatchWithSize(size))
}

// Close implements DB.
func (pdb *PrefixDB) Close() error {
pdb.mtx.Lock()
Expand Down
4 changes: 2 additions & 2 deletions remotedb/batch.go
Expand Up @@ -17,10 +17,10 @@ type batch struct {

var _ db.Batch = (*batch)(nil)

func newBatch(rdb *RemoteDB) *batch {
func newBatchWithSize(rdb *RemoteDB, size int) *batch {
return &batch{
db: rdb,
ops: []*protodb.Operation{},
ops: make([]*protodb.Operation, 0, size),
}
}

Expand Down
6 changes: 5 additions & 1 deletion remotedb/remotedb.go
Expand Up @@ -97,7 +97,11 @@ func (rd *RemoteDB) ReverseIterator(start, end []byte) (db.Iterator, error) {
}

func (rd *RemoteDB) NewBatch() db.Batch {
return newBatch(rd)
return newBatchWithSize(rd, 0)
}

func (rd *RemoteDB) NewBatchWithSize(size int) db.Batch {
return newBatchWithSize(rd, size)
}

// TODO: Implement Print when db.DB implements a method
Expand Down
3 changes: 3 additions & 0 deletions types.go
Expand Up @@ -63,6 +63,9 @@ type DB interface {
// NewBatch creates a batch for atomic updates. The caller must call Batch.Close.
NewBatch() Batch

// NewBatch creates a batch for atomic updates with pre-allocated buffer. The caller must call Batch.Close.
NewBatchWithSize(size int) Batch

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

part of me thinks it would make sense to not break that api and merely use it goleveldb. Unless we see improvements on rocks and others

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

I'm making changes to IAVL now to benchmark how much of a difference this change makes. I will come back here after and look into other databases. Otherwise, rethink this PR


// Print is used for debugging.
Print() error

Expand Down