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

Make the second layer hashmap more memory efficient #11

Merged

Conversation

CosmicHorrorDev
Copy link
Contributor

The bulk of IndexedSignature is taken up by the internal hashmap. The second layer of this hashmap almost always has 1 entry (when I was testing manually with over 7 million blocks, there were still only one value within +99.7% of the second layer hashmaps).

These changes optimize the second layer hashmap for the common case of a single entry where a std::mem::size_of::<SecondLayerMap<&[u8], u32>>() shows 24 bytes while std::mem::size_of<HashMap<&[u8], u32>>() shows 48 bytes on my machine.

This should also make .get() a bit faster with a single entry as well, but it was never a significant amount of time when I've been profiling, so I saw a less than 2% improvement on a couple benchmarks with no regressions.

Copy link
Contributor

@goffrie goffrie left a comment

Choose a reason for hiding this comment

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

This looks great, thanks! Just a couple nits.

use std::{collections::HashMap, hash::Hash, mem};

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SecondLayerMap<K, V>
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to have a doc comment explaining for the reader what this is, and why it's there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went ahead and added docs for that whole file. Let me know if everything looks good to you

src/hashmap_variant.rs Outdated Show resolved Hide resolved
@CosmicHorrorDev
Copy link
Contributor Author

Thanks for the fast review! I just pushed the changes for your suggestions

@goffrie goffrie merged commit 515937f into dropbox:master Oct 1, 2021
@goffrie
Copy link
Contributor

goffrie commented Oct 1, 2021

Good stuff!

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.

2 participants