Skip to content

Commit ea31bd9

Browse files
zsfelfoldiholiman
andauthored
ethdb/memorydb: faster DeleteRange (#31038)
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]>
1 parent a7f9523 commit ea31bd9

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

ethdb/memorydb/memorydb.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package memorydb
1919

2020
import (
21-
"bytes"
2221
"errors"
2322
"sort"
2423
"strings"
@@ -125,12 +124,15 @@ func (db *Database) Delete(key []byte) error {
125124
// DeleteRange deletes all of the keys (and values) in the range [start,end)
126125
// (inclusive on start, exclusive on end).
127126
func (db *Database) DeleteRange(start, end []byte) error {
128-
it := db.NewIterator(nil, start)
129-
defer it.Release()
127+
db.lock.Lock()
128+
defer db.lock.Unlock()
129+
if db.db == nil {
130+
return errMemorydbClosed
131+
}
130132

131-
for it.Next() && bytes.Compare(end, it.Key()) > 0 {
132-
if err := db.Delete(it.Key()); err != nil {
133-
return err
133+
for key := range db.db {
134+
if key >= string(start) && key < string(end) {
135+
delete(db.db, key)
134136
}
135137
}
136138
return nil

0 commit comments

Comments
 (0)