Skip to content

Commit

Permalink
ethdb/memorydb: faster DeleteRange (ethereum#31038)
Browse files Browse the repository at this point in the history
This PR replaces the iterator based DeleteRange implementation of
memorydb with a simpler and much faster loop that directly deletes keys
in the order of iteration instead of unnecessarily collecting keys in
memory and sorting them.

---------

Co-authored-by: Martin HS <[email protected]>
  • Loading branch information
zsfelfoldi and holiman authored Jan 17, 2025
1 parent a7f9523 commit ea31bd9
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions ethdb/memorydb/memorydb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package memorydb

import (
"bytes"
"errors"
"sort"
"strings"
Expand Down Expand Up @@ -125,12 +124,15 @@ func (db *Database) Delete(key []byte) error {
// DeleteRange deletes all of the keys (and values) in the range [start,end)
// (inclusive on start, exclusive on end).
func (db *Database) DeleteRange(start, end []byte) error {
it := db.NewIterator(nil, start)
defer it.Release()
db.lock.Lock()
defer db.lock.Unlock()
if db.db == nil {
return errMemorydbClosed
}

for it.Next() && bytes.Compare(end, it.Key()) > 0 {
if err := db.Delete(it.Key()); err != nil {
return err
for key := range db.db {
if key >= string(start) && key < string(end) {
delete(db.db, key)
}
}
return nil
Expand Down

0 comments on commit ea31bd9

Please sign in to comment.