Skip to content
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

Use Rc::try_unwrap for (possibly?) better performance #85

Merged
merged 6 commits into from
Nov 17, 2022

Conversation

matheus23
Copy link
Member

Putting this up here for discussion.

We can make use of mutability in Node::set_value and Node::remove_value, by turning values of type Rc<Node<...>> into mut Node<...> using Rc::try_unwrap(...).unwrap_or_else(|rc| (*rc).clone()).
Essentially that'll just give us an owned Node if we hold the only reference to it, or it'll clone the inner Rc.
Theory is that if the calling code is basically doing lots of changes on the HAMT in a tight loop, that's going to get sped up, since that'll end up being lots of in-place mutation.

The benchmarks don't look too promising though. I got ~10% improvements.
To test this, just compare the two commits in the PR against each other. Specifically their cargo bench -p wnfs-bench node.

Also, I had to hand-expand the async_recursion macro for set_value and remove_value, because otherwise there would've been lifetime issues with the original versions. We should probably figure out another way, but only if we really want to keep this PR.

@matheus23 matheus23 requested a review from a team as a code owner November 7, 2022 20:57
@codecov
Copy link

codecov bot commented Nov 7, 2022

Codecov Report

Merging #85 (60a4233) into main (80dbe82) will decrease coverage by 0.29%.
The diff coverage is 57.35%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #85      +/-   ##
==========================================
- Coverage   66.05%   65.75%   -0.30%     
==========================================
  Files          26       26              
  Lines        1903     1907       +4     
  Branches      486      489       +3     
==========================================
- Hits         1257     1254       -3     
- Misses        248      253       +5     
- Partials      398      400       +2     
Impacted Files Coverage Δ
wnfs/src/common/metadata.rs 67.56% <0.00%> (ø)
wnfs/src/private/hamt/node.rs 62.87% <58.73%> (-2.77%) ⬇️
wnfs/src/private/forest.rs 56.33% <66.66%> (ø)
wnfs/src/private/hamt/pointer.rs 49.12% <0.00%> (-1.76%) ⬇️

@matheus23
Copy link
Member Author

@appcypher and I figured out that adding + 'static to K, V and H type parameters would make the lifetime annotations redundant.

It's for sure safe to annotate H: 'static, but for K and V it's not too obvious. It'd limit users of Node<K, V, H> to only put owned things (never references e.g. no &str for keys) into the HAMT. That's the case for all of rs-wnfs, so technically we could just do that? That'd make the code cleaner again and we could use #[async_recursion(?Send)] again.

Copy link
Member

@appcypher appcypher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🎉

And like you suggested, maybe we add 'static for H at the impl level. Doesn't matter much either way.

@matheus23 matheus23 merged commit 9ff76f6 into main Nov 17, 2022
@matheus23 matheus23 deleted the matheus23/rc-make-mut branch November 17, 2022 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants