-
Notifications
You must be signed in to change notification settings - Fork 63
Use inline storage for small hashes #47
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
20c5c89
Use inline storage for small hashes
rklaehn 4e0632b
Clippy
rklaehn 0559bed
Rename copy_from_slice to just from_slice
rklaehn 02cf114
Explicity store the bytes size
rklaehn 9ab33d9
Add comment about the rationale for the 38 byte limit.
rklaehn 5a91f48
PR feedback
rklaehn ec44135
Add quickcheck tests for from_slices
rklaehn b729685
Add check_invariants to make sure we don't create heap storage for sm…
rklaehn 3456e07
PR feedback
rklaehn 62692e2
Make debug instance useful
rklaehn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::sync::Arc; | ||
|
||
const MAX_INLINE: usize = 39; | ||
|
||
#[derive(Clone)] | ||
pub enum Storage { | ||
rklaehn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// hash is stored inline. if it is smaller than 39 bytes it should be padded with 0u8 | ||
Inline([u8; MAX_INLINE]), | ||
/// hash is stored on the heap. this must be only used if the hash is actually larger than | ||
/// 39 bytes to ensure an unique representation. | ||
Heap(Arc<[u8]>), | ||
} | ||
|
||
impl Storage { | ||
/// The raw bytes. Note that this can be longer than the data this storage has been created from. | ||
pub fn bytes(&self) -> &[u8] { | ||
match self { | ||
Storage::Inline(bytes) => bytes, | ||
Storage::Heap(data) => &data, | ||
} | ||
} | ||
|
||
/// creates storage from a vec. Note that this will not preserve the size. | ||
pub fn copy_from_slice(slice: &[u8]) -> Self { | ||
rklaehn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if slice.len() <= MAX_INLINE { | ||
let mut data: [u8; MAX_INLINE] = [0; MAX_INLINE]; | ||
&data[..slice.len()].copy_from_slice(slice); | ||
Storage::Inline(data) | ||
} else { | ||
Storage::Heap(slice.into()) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Storage; | ||
|
||
#[test] | ||
fn test_size() { | ||
assert_eq!(std::mem::size_of::<Storage>(), 40); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why 39?
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.
So the total size is 40, a multiple of 8. 1 byte is needed for the enum discriminator.
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.
39 fits all 256 bit hashes with some room to spare. 512 byte hashes won't work though, and I think once you go to such large hashes, you are better off with an
Arc<[u8]>
.But the size of the inline buffer can of course be adjusted when even larger hashes become common. The whole mechanism is completely opaque.