-
Notifications
You must be signed in to change notification settings - Fork 662
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
refactor: improve trie prefetching #8205
Conversation
a89f285
to
93fe680
Compare
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 am not super familiar with the code but trying to get more into it. So some drive-by comments. Feel free to ignore if they do not make sense.
Co-authored-by: Akhilesh Singhania <akhi@near.org>
cfd5e64
to
fe9703e
Compare
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.
Looks great, happy to see the code improving! 😄
I left very few comments, mostly optional ones. Feel free to merge anytime you think it's ready.
No action required but some thoughts:
On a more code-philosophical note, I think PrefetchError
is a positive change here but there are two reasons why it was like this.
- It is a common pattern in Rust code that a method that are designed to fail often under normal circumstances should return ownership of all arguments that it owns. This would allow to retry, for example. But here it's not so important as we can clone all args. And in the current state of the code, we don't make use of the returned resources anyway, so no harm in removing that. But generally speaking, I would argue this is good practice and a patterns that can be useful to know in Rust code.
- Personally, I only create error enums when there is more than one failure case. I guess it's mostly a matter of taste. But my main argument would be that refactoring from an implicit error to a enum error is trivial when a second variant becomes necessary. But going back can be tricky if the type gets integrated into other types, for example as #[source] of another error. Also, going back to bullet point 1, returning resources is awkward when using a specific error type.
But again, looking at your code changes, it has made things more readable. So in this case I am in favor of making the change. 👍
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.
LGTM. It will be best if someone more familiar with the code also did a review though.
self.prefetch_enqueued.inc(); | ||
Ok(()) | ||
} | ||
fn prefetch_trie_key(&self, trie_key: TrieKey) -> Result<(), PrefetchError> { |
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.
Definitely not part of this PR but we should improve the API for this function. Currently all callers are calling with small TrieKeys. However, some variants e.g. ContractData can be very large and in such cases, the API should take them by value.
An option might be for this function to accept AccountId
instead of TrieKey or for the function to take TrieKey
as a reference (but this will introduce a lot of lifetime issues).
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.
Just to confirm my understanding: you would like to avoid all .clone()
calls involved in creating TrieKey
from the main thread, is that right?
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 feel like an API that encourages one to clone()
a large data structure is problematic. So we should think hard about it. In this case, I wonder if changing the function to take AccountId
will make the API less "error prone" to use.
d08d304
to
001f08e
Compare
clippy::result_unit_err
warningprefetching.0.lock().expect(POISONED_LOCK_ERR)
by introducing helperlock
functionreserved_memory
toPrefetchSlot
Related to #8145 and #7723