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 <martin@swende.se>
This commit is contained in:
Felföldi Zsolt 2025-01-17 16:54:19 +01:00 committed by GitHub
parent a7f9523ae1
commit ea31bd9faf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 6 deletions

View File

@ -18,7 +18,6 @@
package memorydb
import (
"bytes"
"errors"
"sort"
"strings"
@ -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