-
Notifications
You must be signed in to change notification settings - Fork 886
feat: Optimise HDiffBuffer
by using Arc instead of Vec
#7675
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
base: unstable
Are you sure you want to change the base?
Conversation
dc358d1
to
3deaeb7
Compare
HDiffBuffer
by using Arc instead of Vec
beacon_node/store/src/hdiff.rs
Outdated
let mut validators_vec = source.validators.to_vec(); | ||
self.validators_diff().apply(&mut validators_vec, config)?; | ||
source.validators = Arc::from(validators_vec); |
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 don't see how using Arc<[Validator]>
helps when we do this conversion to and from Vec<Validator>
whenever we apply a diff.
Are you trying to save memory or CPU cycles?
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.
You're right. i was trying to save memory as the issue mentioned, but converting to Vec<> fails that purpose.
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.
Oh yeah I forgot about that issue, I think the structure I was imagining was Vec<Arc<Validator>>
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.
Got it. It has to be a collection of individual Arcs. Made a mistake on wrapping all the validators in a single Arc. Will be changing that
beacon_node/store/src/hdiff.rs
Outdated
let validators = std::mem::take(beacon_state.validators_mut()).to_vec(); | ||
let validators = Arc::from(validators); |
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.
If we did want to save memory, a promising approach might be to keep the List
, which is a copy-on-write (persistent) data structure
However, List
s have slower O(log n)
indexing than vecs, and we do random-access indexing here:
lighthouse/beacon_node/store/src/hdiff.rs
Line 553 in 6be646c
if let Some(x) = xs.get_mut(index as usize) { |
We also do it here, but this use could be replaced by a zip
on two iterators:
lighthouse/beacon_node/store/src/hdiff.rs
Line 461 in 6be646c
let validator_diff = if let Some(x) = xs.get(i) { |
62f85a6
to
014e076
Compare
…e and apply functions, alongside tests
c04e467
to
1a1bb09
Compare
.cloned() | ||
.map(Arc::new) |
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 maybe OK, but we could make it even better if we added an iter_arc
method to List
. In the case of validators, they are not packed in leaves, and are stored behind an Arc
:
Would you be interested in making a PR to milhouse
to add this? I think the API would have to consider the case where T
is packed, and return an error in this case. Something like:
pub fn iter_arc(&self) -> Result<impl Iterator<Item = &Arc<T>>, Error> {
if T::tree_hash_type() == TreeHashType::Basic {
// Can't return `Arc`s for packed leaves.
return Err(Error::PackedLeavesNoArc);
}
// TODO
Ok(iterator)
}
I'm happy to help advise on the implementation. Take a look at the other iterators in:
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.
Sure! I will look into it and try to implement the iter_arc
method to List
. Leaving a comment on the issue in Milhouse
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.
Thanks!
Issue Addressed
Fixes #6579
Proposed Changes
Modified
HDiffBuffer
to useVex<Arc<Validator>>
instead ofVec<Validator>
, and made necessary changes regarding that