-
Notifications
You must be signed in to change notification settings - Fork 322
[Draft] Benchmarking 8-bits Tag instead of 7 #653
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
Draft
gaujay
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
gaujay:8-bits-tag
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+73
−40
Draft
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,50 +3,79 @@ use core::{fmt, mem}; | |
/// Single tag in a control group. | ||
#[derive(Copy, Clone, PartialEq, Eq)] | ||
#[repr(transparent)] | ||
pub(crate) struct Tag(pub(super) u8); | ||
pub(crate) struct Tag(pub(super) i8); | ||
impl Tag { | ||
/// Control tag value for an empty bucket. | ||
pub(crate) const EMPTY: Tag = Tag(0b1111_1111); | ||
pub(crate) const EMPTY: Tag = Tag(0b0111_1111); // 127 | ||
pub(crate) const EMPTY32: i32 = 0x7F7F7F7F; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should probably be a separate |
||
|
||
/// Control tag value for a deleted bucket. | ||
pub(crate) const DELETED: Tag = Tag(0b1000_0000); | ||
pub(crate) const DELETED: Tag = Tag(0b0111_1110); // 126 | ||
pub(crate) const DELETED32: i32 = 0x7E7E7E7E; | ||
|
||
pub(crate) const MAX_TAG32: i32 = 0x7D7D7D7D; // 4*125 | ||
|
||
/// Checks whether a control tag represents a full bucket (top bit is clear). | ||
#[inline] | ||
pub(crate) const fn is_full(self) -> bool { | ||
self.0 & 0x80 == 0 | ||
self.0 < Tag::DELETED.0 | ||
} | ||
|
||
/// Checks whether a control tag represents a special value (top bit is set). | ||
#[inline] | ||
pub(crate) const fn is_special(self) -> bool { | ||
self.0 & 0x80 != 0 | ||
self.0 >= Tag::DELETED.0 | ||
} | ||
|
||
/// Checks whether a special control value is EMPTY (just check 1 bit). | ||
#[inline] | ||
pub(crate) const fn special_is_empty(self) -> bool { | ||
debug_assert!(self.is_special()); | ||
self.0 & 0x01 != 0 | ||
self.0 == Tag::EMPTY.0 | ||
} | ||
|
||
/// Creates a control tag representing a full bucket with the given hash. | ||
#[inline] | ||
#[inline(always)] | ||
#[allow(clippy::cast_possible_truncation)] | ||
pub(crate) const fn full(hash: u64) -> Tag { | ||
// Constant for function that grabs the top 7 bits of the hash. | ||
Tag(Self::full32(hash) as i8) | ||
} | ||
|
||
#[inline] | ||
#[allow(clippy::cast_possible_truncation)] | ||
pub(crate) const fn full32(hash: u64) -> i32 { | ||
// Constant for function that grabs the top 8 bits * 4 of the hash. | ||
const MIN_HASH_LEN: usize = if mem::size_of::<usize>() < mem::size_of::<u64>() { | ||
mem::size_of::<usize>() | ||
} else { | ||
mem::size_of::<u64>() | ||
}; | ||
|
||
// Grab the top 7 bits of the hash. While the hash is normally a full 64-bit | ||
// Constant array of 8 bits duplicated as 32 bits | ||
// with re-mapping of special values. | ||
const fn compute_control() -> [i32; 256] { | ||
let mut result = [0; 256]; | ||
|
||
let mut i: u32 = 0; | ||
while i < 256 { | ||
result[i as usize] = (i | (i << 8) | (i << 16) | (i << 24)) as i32; | ||
i += 1; | ||
} | ||
|
||
// Avoid overlap with special values. | ||
result[Tag::EMPTY.0 as usize] = 0x29292929; | ||
result[Tag::DELETED.0 as usize] = 0x53535353; | ||
|
||
result | ||
} | ||
const CONTROL: [i32; 256] = compute_control(); | ||
|
||
// Grab the top 8 bits of the hash. While the hash is normally a full 64-bit | ||
// value, some hash functions (such as FxHash) produce a usize result | ||
// instead, which means that the top 32 bits are 0 on 32-bit platforms. | ||
// So we use MIN_HASH_LEN constant to handle this. | ||
let top7 = hash >> (MIN_HASH_LEN * 8 - 7); | ||
Tag((top7 & 0x7f) as u8) // truncation | ||
let top8 = (hash >> (MIN_HASH_LEN * 8 - 8)) as usize; | ||
CONTROL[top8] // truncation | ||
} | ||
} | ||
impl fmt::Debug for Tag { | ||
|
@@ -58,7 +87,7 @@ impl fmt::Debug for Tag { | |
f.pad("DELETED") | ||
} | ||
} else { | ||
f.debug_tuple("full").field(&(self.0 & 0x7F)).finish() | ||
f.debug_tuple("full").field(&self.0).finish() | ||
} | ||
} | ||
} | ||
|
@@ -78,6 +107,6 @@ impl TagSliceExt for [Tag] { | |
#[inline] | ||
fn fill_tag(&mut self, tag: Tag) { | ||
// SAFETY: We have access to the entire slice, so, we can write to the entire slice. | ||
unsafe { self.as_mut_ptr().write_bytes(tag.0, self.len()) } | ||
unsafe { self.as_mut_ptr().write_bytes(tag.0 as u8, self.len()) } | ||
} | ||
} |
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
Oops, something went wrong.
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.
You can simplify this: the compare gives you
0
and-1
, to which you can just add127
to getEMPTY
andDELETED
.