feat: Optimise HDiffBuffer by using Arc instead of Vec#7675
feat: Optimise HDiffBuffer by using Arc instead of Vec#7675PoulavBhowmick03 wants to merge 3 commits intosigp:unstablefrom
HDiffBuffer by using Arc instead of Vec#7675Conversation
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.
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.
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.
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.
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.
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, Lists 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
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
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.
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:
https://github.com/sigp/milhouse/blob/6da903ce8f783295714a68de4a2dcfc8b7c6ee01/src/leaf.rs#L17
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.
Sure! I will look into it and try to implement the iter_arc method to List. Leaving a comment on the issue in Milhouse
|
Hi @PoulavBhowmick03, this pull request has been closed automatically due to 30 days of inactivity. If you’d like to continue working on it, feel free to reopen at any time. |
|
Hi @PoulavBhowmick03, this pull request has been closed automatically due to 30 days of inactivity. If you’d like to continue working on it, feel free to reopen at any time. |
Issue Addressed
Fixes #6579
Proposed Changes
Modified
HDiffBufferto useVex<Arc<Validator>>instead ofVec<Validator>, and made necessary changes regarding that