Skip to content

Commit 016ea6b

Browse files
committed
Use a side-table of consts instead of matching on the DepKind enum.
1 parent d1220fd commit 016ea6b

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ use std::hash::Hash;
7070

7171
pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
7272

73+
/// This struct stores metadata about each DepKind.
74+
///
75+
/// Information is retrieved by indexing the `DEP_KINDS` array using the integer value
76+
/// of the `DepKind`. Overall, this allows to implement `DepContext` using this manual
77+
/// jump table instead of large matches.
78+
pub struct DepKindStruct {}
79+
80+
impl std::ops::Deref for DepKind {
81+
type Target = DepKindStruct;
82+
fn deref(&self) -> &DepKindStruct {
83+
&DEP_KINDS[*self as usize]
84+
}
85+
}
86+
7387
// erase!() just makes tokens go away. It's used to specify which macro argument
7488
// is repeated (i.e., which sub-expression of the macro we are in) but don't need
7589
// to actually use any of the arguments.
@@ -103,14 +117,46 @@ macro_rules! contains_eval_always_attr {
103117
($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_eval_always_attr!($attr) | )* false});
104118
}
105119

120+
#[allow(non_upper_case_globals)]
121+
pub mod dep_kind {
122+
use super::*;
123+
124+
// We use this for most things when incr. comp. is turned off.
125+
pub const Null: DepKindStruct = DepKindStruct {};
126+
127+
// Represents metadata from an extern crate.
128+
pub const CrateMetadata: DepKindStruct = DepKindStruct {};
129+
130+
pub const TraitSelect: DepKindStruct = DepKindStruct {};
131+
132+
pub const CompileCodegenUnit: DepKindStruct = DepKindStruct {};
133+
134+
macro_rules! define_query_dep_kinds {
135+
($(
136+
[$($attrs:tt)*]
137+
$variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
138+
,)*) => (
139+
$(pub const $variant: DepKindStruct = {
140+
DepKindStruct {
141+
}
142+
};)*
143+
);
144+
}
145+
146+
rustc_dep_node_append!([define_query_dep_kinds!][]);
147+
}
148+
106149
macro_rules! define_dep_nodes {
107150
(<$tcx:tt>
108151
$(
109152
[$($attrs:tt)*]
110153
$variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
111154
,)*
112155
) => (
113-
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
156+
static DEP_KINDS: &[DepKindStruct] = &[ $(dep_kind::$variant),* ];
157+
158+
/// This enum serves as an index into the `DEP_KINDS` array.
159+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
114160
#[allow(non_camel_case_types)]
115161
pub enum DepKind {
116162
$($variant),*

compiler/rustc_query_system/src/dep_graph/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub trait DepContext: Copy {
6161
}
6262

6363
/// Describe the different families of dependency nodes.
64-
pub trait DepKind: Copy + fmt::Debug + Eq + Ord + Hash {
64+
pub trait DepKind: Copy + fmt::Debug + Eq + Hash {
6565
const NULL: Self;
6666

6767
/// Return whether this kind always require evaluation.

0 commit comments

Comments
 (0)