-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Encode DepKind as u16 #115391
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
Encode DepKind as u16 #115391
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,12 +80,56 @@ macro_rules! define_dep_nodes { | |
} | ||
|
||
/// This enum serves as an index into arrays built by `make_dep_kind_array`. | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Encodable, Decodable)] | ||
// This enum has more than u8::MAX variants so we need some kind of multi-byte | ||
// encoding. The derived Encodable/Decodable uses leb128 encoding which is | ||
// dense when only considering this enum. But DepKind is encoded in a larger | ||
// struct, and there we can take advantage of the unused bits in the u16. | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
#[allow(non_camel_case_types)] | ||
#[repr(u16)] | ||
pub enum DepKind { | ||
$( $( #[$attr] )* $variant),* | ||
} | ||
|
||
impl DepKind { | ||
// This const implements two things: A bounds check so that we can decode | ||
// a DepKind from a u16 with just one check, and a const check that the | ||
// discriminants of the variants have been assigned consecutively from 0 | ||
// so that just the one comparison suffices to check that the u16 can be | ||
// transmuted to a DepKind. | ||
const VARIANTS: u16 = { | ||
let deps: &[DepKind] = &[$(DepKind::$variant,)*]; | ||
let mut i = 0; | ||
while i < deps.len() { | ||
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 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. Since #110393 I'm pretty sure the only way to do this is with |
||
if i as u16 != deps[i] as u16 { | ||
panic!(); | ||
} | ||
i += 1; | ||
} | ||
deps.len() as u16 | ||
}; | ||
} | ||
|
||
impl<S: rustc_serialize::Encoder> rustc_serialize::Encodable<S> for DepKind { | ||
#[inline] | ||
fn encode(&self, s: &mut S) { | ||
s.emit_u16(*self as u16); | ||
} | ||
} | ||
|
||
impl<D: rustc_serialize::Decoder> rustc_serialize::Decodable<D> for DepKind { | ||
#[inline] | ||
fn decode(d: &mut D) -> DepKind { | ||
let discrim = d.read_u16(); | ||
assert!(discrim < DepKind::VARIANTS); | ||
// SAFETY: DepKind::VARIANTS checks that the discriminant values permit | ||
// this one check to soundly guard the transmute. | ||
unsafe { | ||
std::mem::transmute::<u16, DepKind>(discrim) | ||
} | ||
} | ||
} | ||
|
||
pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> { | ||
match label { | ||
$(stringify!($variant) => Ok(DepKind::$variant),)* | ||
|
@@ -114,6 +158,8 @@ rustc_query_append!(define_dep_nodes![ | |
[] fn CompileMonoItem() -> (), | ||
]); | ||
|
||
static_assert_size!(DepKind, 2); | ||
|
||
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys. | ||
// Be very careful changing this type signature! | ||
pub(crate) fn make_compile_codegen_unit(tcx: TyCtxt<'_>, name: Symbol) -> DepNode { | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.