-
Notifications
You must be signed in to change notification settings - Fork 21.1k
core/rawdb: reduce allocations in rawdb.ReadHeaderNumber #31913
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: reduce allocations in rawdb.ReadHeaderNumber #31913
Conversation
core/rawdb/accessors_chain.go
Outdated
// ReadHeaderNumber returns the header number assigned to a hash. | ||
func ReadHeaderNumber(db ethdb.KeyValueReader, hash common.Hash) *uint64 { | ||
func ReadHeaderNumber(db ethdb.KeyValueReader, hash common.Hash) uint64 { |
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.
why can't we just make this return (uint64, bool)
to signify that the uint64 returned is valid or not?
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.
or we could use math.MaxUint64
as the sentinel value
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.
Yeah, thats a way better idea
if block == nil { | ||
break | ||
} | ||
blocks = append(blocks, block) | ||
hash = block.ParentHash() | ||
*number-- |
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.
Here we should be careful. Previously we changed the undelying value, now we change the copy. Is this OK?
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.
This is fine, it was newly allocated anyway.
if block == nil { | ||
break | ||
} | ||
blocks = append(blocks, block) | ||
hash = block.ParentHash() | ||
*number-- |
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.
This is fine, it was newly allocated anyway.
This is something interesting I came across during my benchmarks, we spent ~3.8% of all allocations allocating the header number on the heap.
Opening this to discuss the idea, I know that rawdb.EmptyNumber is not a great name for the variable, open to suggestions