-
Notifications
You must be signed in to change notification settings - Fork 20.1k
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
core/rawdb: allocate database keys with explicit size to avoid slice growth #27772
Conversation
core/rawdb/schema.go
Outdated
@@ -188,7 +188,11 @@ func accountSnapshotKey(hash common.Hash) []byte { | |||
|
|||
// storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash | |||
func storageSnapshotKey(accountHash, storageHash common.Hash) []byte { | |||
return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...) | |||
res := make([]byte, 1+len(accountHash)*2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
res := make([]byte, 1+len(accountHash)*2) | |
res := make([]byte, len(SnapshotStoragePrefix)+common.HashLength*2) |
core/rawdb/schema.go
Outdated
@@ -247,7 +251,11 @@ func accountTrieNodeKey(path []byte) []byte { | |||
|
|||
// storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath. | |||
func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte { | |||
return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...) | |||
res := make([]byte, 1+len(accountHash)+len(path)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
res := make([]byte, 1+len(accountHash)+len(path)) | |
res := make([]byte, len(trieNodeStoragePrefix)+common.HashLength+len(path)) |
core/rawdb/schema.go
Outdated
@@ -247,7 +251,11 @@ func accountTrieNodeKey(path []byte) []byte { | |||
|
|||
// storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath. | |||
func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte { | |||
return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...) | |||
res := make([]byte, 1+len(accountHash)+len(path)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
res := make([]byte, 1+len(accountHash)+len(path)) | |
buf := make([]byte, len(trieNodeStoragePrefix)+common.HashLength+len(path)) | |
n := copy(buf, trieNodeStoragePrefix) | |
n += copy(buf[n:], accountHash.Bytes()) | |
copy(buf[n:], path) | |
return buf |
could be something like this
buf := make([]byte, len(SnapshotStoragePrefix)+common.HashLength+common.HashLength) | ||
n := copy(buf, SnapshotStoragePrefix) | ||
n += copy(buf[n:], accountHash.Bytes()) | ||
copy(buf[n:], storageHash.Bytes()) | ||
return buf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect that a more minimal change would have the same effect
buf := make([]byte, len(SnapshotStoragePrefix)+common.HashLength+common.HashLength) | |
n := copy(buf, SnapshotStoragePrefix) | |
n += copy(buf[n:], accountHash.Bytes()) | |
copy(buf[n:], storageHash.Bytes()) | |
return buf | |
buf := make([]byte, 0, len(SnapshotStoragePrefix)+common.HashLength+common.HashLength) | |
return append(append(append(buf, SnapshotStoragePrefix...), accountHash.Bytes()...), storageHash.Bytes()...) |
It is slightly less complex since it relies on the compiler instead of fiddling with n
.
…d slice growth (ethereum#27772)" This reverts commit 4639725.
…d slice growth (ethereum#27772)" This reverts commit 4639725.
During profiling I noticed that a lot of allocations are spent trying to compute the keys for accessing the database:
This PR reduces the number of allocs, and increases the speed of creating these keys
Benchmarks (the suffix 2 denotes the new ones):