Skip to content

Commit 2536302

Browse files
Allocate numerical values of DefIndexes from two seperate ranges.
This way we can have all item-likes occupy a dense range of DefIndexes, which is good for making fast, array-based dictionaries.
1 parent 2efd813 commit 2536302

File tree

7 files changed

+237
-72
lines changed

7 files changed

+237
-72
lines changed

src/librustc/hir/def_id.rs

+53
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,86 @@ impl serialize::UseSpecializedDecodable for CrateNum {
7878
/// A DefIndex is an index into the hir-map for a crate, identifying a
7979
/// particular definition. It should really be considered an interned
8080
/// shorthand for a particular DefPath.
81+
///
82+
/// At the moment we are allocating the numerical values of DefIndexes into two
83+
/// ranges: the "low" range (starting at zero) and the "high" range (starting at
84+
/// DEF_INDEX_HI_START). This allows us to allocate the DefIndexes of all
85+
/// item-likes (Items, TraitItems, and ImplItems) into one of these ranges and
86+
/// consequently use a simple array for lookup tables keyed by DefIndex and
87+
/// known to be densely populated. This is especially important for the HIR map.
88+
///
89+
/// Since the DefIndex is mostly treated as an opaque ID, you probably don't
90+
/// have to care about these ranges.
8191
#[derive(Clone, Debug, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
8292
RustcDecodable, Hash, Copy)]
8393
pub struct DefIndex(u32);
8494

8595
impl DefIndex {
96+
#[inline]
8697
pub fn new(x: usize) -> DefIndex {
8798
assert!(x < (u32::MAX as usize));
8899
DefIndex(x as u32)
89100
}
90101

102+
#[inline]
91103
pub fn from_u32(x: u32) -> DefIndex {
92104
DefIndex(x)
93105
}
94106

107+
#[inline]
95108
pub fn as_usize(&self) -> usize {
96109
self.0 as usize
97110
}
98111

112+
#[inline]
99113
pub fn as_u32(&self) -> u32 {
100114
self.0
101115
}
116+
117+
#[inline]
118+
pub fn address_space(&self) -> DefIndexAddressSpace {
119+
if self.0 < DEF_INDEX_HI_START.0 {
120+
DefIndexAddressSpace::Low
121+
} else {
122+
DefIndexAddressSpace::High
123+
}
124+
}
125+
126+
/// Converts this DefIndex into a zero-based array index.
127+
/// This index is the offset within the given "range" of the DefIndex,
128+
/// that is, if the DefIndex is part of the "high" range, the resulting
129+
/// index will be (DefIndex - DEF_INDEX_HI_START).
130+
#[inline]
131+
pub fn as_array_index(&self) -> usize {
132+
(self.0 & !DEF_INDEX_HI_START.0) as usize
133+
}
102134
}
103135

136+
/// The start of the "high" range of DefIndexes.
137+
const DEF_INDEX_HI_START: DefIndex = DefIndex(1 << 31);
138+
104139
/// The crate root is always assigned index 0 by the AST Map code,
105140
/// thanks to `NodeCollector::new`.
106141
pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
107142

143+
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
144+
pub enum DefIndexAddressSpace {
145+
Low = 0,
146+
High = 1,
147+
}
148+
149+
impl DefIndexAddressSpace {
150+
#[inline]
151+
pub fn index(&self) -> usize {
152+
*self as usize
153+
}
154+
155+
#[inline]
156+
pub fn start(&self) -> usize {
157+
self.index() * DEF_INDEX_HI_START.as_usize()
158+
}
159+
}
160+
108161
/// A DefId identifies a particular *definition*, by combining a crate
109162
/// index and a def index.
110163
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, RustcDecodable, Hash, Copy)]

src/librustc/hir/lowering.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
// in the HIR, especially for multiple identifiers.
4242

4343
use hir;
44-
use hir::map::{Definitions, DefKey};
44+
use hir::map::{Definitions, DefKey, REGULAR_SPACE};
4545
use hir::map::definitions::DefPathData;
4646
use hir::def_id::{DefIndex, DefId, CRATE_DEF_INDEX};
4747
use hir::def::{Def, PathResolution};
@@ -2689,7 +2689,10 @@ impl<'a> LoweringContext<'a> {
26892689
let def_id = {
26902690
let defs = self.resolver.definitions();
26912691
let def_path_data = DefPathData::Binding(name.as_str());
2692-
let def_index = defs.create_def_with_parent(parent_def, id, def_path_data);
2692+
let def_index = defs.create_def_with_parent(parent_def,
2693+
id,
2694+
def_path_data,
2695+
REGULAR_SPACE);
26932696
DefId::local(def_index)
26942697
};
26952698

src/librustc/hir/map/def_collector.rs

+46-21
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
// except according to those terms.
1010

1111
use hir::map::definitions::*;
12-
use hir::def_id::{CRATE_DEF_INDEX, DefIndex};
12+
use hir::def_id::{CRATE_DEF_INDEX, DefIndex, DefIndexAddressSpace};
1313

1414
use syntax::ast::*;
1515
use syntax::ext::hygiene::Mark;
1616
use syntax::visit;
1717
use syntax::symbol::{Symbol, keywords};
1818

19+
use hir::map::{ITEM_LIKE_SPACE, REGULAR_SPACE};
20+
1921
/// Creates def ids for nodes in the AST.
2022
pub struct DefCollector<'a> {
2123
definitions: &'a mut Definitions,
@@ -39,23 +41,31 @@ impl<'a> DefCollector<'a> {
3941
}
4042

4143
pub fn collect_root(&mut self) {
42-
let root = self.create_def_with_parent(None, CRATE_NODE_ID, DefPathData::CrateRoot);
44+
let root = self.create_def_with_parent(None,
45+
CRATE_NODE_ID,
46+
DefPathData::CrateRoot,
47+
ITEM_LIKE_SPACE);
4348
assert_eq!(root, CRATE_DEF_INDEX);
4449
self.parent_def = Some(root);
4550
}
4651

47-
fn create_def(&mut self, node_id: NodeId, data: DefPathData) -> DefIndex {
52+
fn create_def(&mut self,
53+
node_id: NodeId,
54+
data: DefPathData,
55+
address_space: DefIndexAddressSpace)
56+
-> DefIndex {
4857
let parent_def = self.parent_def;
4958
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
50-
self.definitions.create_def_with_parent(parent_def, node_id, data)
59+
self.definitions.create_def_with_parent(parent_def, node_id, data, address_space)
5160
}
5261

5362
fn create_def_with_parent(&mut self,
5463
parent: Option<DefIndex>,
5564
node_id: NodeId,
56-
data: DefPathData)
65+
data: DefPathData,
66+
address_space: DefIndexAddressSpace)
5767
-> DefIndex {
58-
self.definitions.create_def_with_parent(parent, node_id, data)
68+
self.definitions.create_def_with_parent(parent, node_id, data, address_space)
5969
}
6070

6171
pub fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
@@ -76,7 +86,7 @@ impl<'a> DefCollector<'a> {
7686
_ => {}
7787
}
7888

79-
self.create_def(expr.id, DefPathData::Initializer);
89+
self.create_def(expr.id, DefPathData::Initializer, REGULAR_SPACE);
8090
}
8191

8292
fn visit_macro_invoc(&mut self, id: NodeId, const_expr: bool) {
@@ -118,27 +128,32 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
118128
ViewPathSimple(..) => {}
119129
ViewPathList(_, ref imports) => {
120130
for import in imports {
121-
self.create_def(import.node.id, DefPathData::Misc);
131+
self.create_def(import.node.id,
132+
DefPathData::Misc,
133+
ITEM_LIKE_SPACE);
122134
}
123135
}
124136
}
125137
DefPathData::Misc
126138
}
127139
};
128-
let def = self.create_def(i.id, def_data);
140+
let def = self.create_def(i.id, def_data, ITEM_LIKE_SPACE);
129141

130142
self.with_parent(def, |this| {
131143
match i.node {
132144
ItemKind::Enum(ref enum_definition, _) => {
133145
for v in &enum_definition.variants {
134146
let variant_def_index =
135147
this.create_def(v.node.data.id(),
136-
DefPathData::EnumVariant(v.node.name.name.as_str()));
148+
DefPathData::EnumVariant(v.node.name.name.as_str()),
149+
REGULAR_SPACE);
137150
this.with_parent(variant_def_index, |this| {
138151
for (index, field) in v.node.data.fields().iter().enumerate() {
139152
let name = field.ident.map(|ident| ident.name)
140153
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
141-
this.create_def(field.id, DefPathData::Field(name.as_str()));
154+
this.create_def(field.id,
155+
DefPathData::Field(name.as_str()),
156+
REGULAR_SPACE);
142157
}
143158

144159
if let Some(ref expr) = v.node.disr_expr {
@@ -151,13 +166,14 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
151166
// If this is a tuple-like struct, register the constructor.
152167
if !struct_def.is_struct() {
153168
this.create_def(struct_def.id(),
154-
DefPathData::StructCtor);
169+
DefPathData::StructCtor,
170+
REGULAR_SPACE);
155171
}
156172

157173
for (index, field) in struct_def.fields().iter().enumerate() {
158174
let name = field.ident.map(|ident| ident.name.as_str())
159175
.unwrap_or(Symbol::intern(&index.to_string()).as_str());
160-
this.create_def(field.id, DefPathData::Field(name));
176+
this.create_def(field.id, DefPathData::Field(name), REGULAR_SPACE);
161177
}
162178
}
163179
_ => {}
@@ -168,7 +184,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
168184

169185
fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
170186
let def = self.create_def(foreign_item.id,
171-
DefPathData::ValueNs(foreign_item.ident.name.as_str()));
187+
DefPathData::ValueNs(foreign_item.ident.name.as_str()),
188+
REGULAR_SPACE);
172189

173190
self.with_parent(def, |this| {
174191
visit::walk_foreign_item(this, foreign_item);
@@ -177,7 +194,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
177194

178195
fn visit_generics(&mut self, generics: &'a Generics) {
179196
for ty_param in generics.ty_params.iter() {
180-
self.create_def(ty_param.id, DefPathData::TypeParam(ty_param.ident.name.as_str()));
197+
self.create_def(ty_param.id,
198+
DefPathData::TypeParam(ty_param.ident.name.as_str()),
199+
REGULAR_SPACE);
181200
}
182201

183202
visit::walk_generics(self, generics);
@@ -191,7 +210,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
191210
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id, false),
192211
};
193212

194-
let def = self.create_def(ti.id, def_data);
213+
let def = self.create_def(ti.id, def_data, ITEM_LIKE_SPACE);
195214
self.with_parent(def, |this| {
196215
if let TraitItemKind::Const(_, Some(ref expr)) = ti.node {
197216
this.visit_const_expr(expr);
@@ -209,7 +228,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
209228
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id, false),
210229
};
211230

212-
let def = self.create_def(ii.id, def_data);
231+
let def = self.create_def(ii.id, def_data, ITEM_LIKE_SPACE);
213232
self.with_parent(def, |this| {
214233
if let ImplItemKind::Const(_, ref expr) = ii.node {
215234
this.visit_const_expr(expr);
@@ -225,7 +244,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
225244
match pat.node {
226245
PatKind::Mac(..) => return self.visit_macro_invoc(pat.id, false),
227246
PatKind::Ident(_, id, _) => {
228-
let def = self.create_def(pat.id, DefPathData::Binding(id.node.name.as_str()));
247+
let def = self.create_def(pat.id,
248+
DefPathData::Binding(id.node.name.as_str()),
249+
REGULAR_SPACE);
229250
self.parent_def = Some(def);
230251
}
231252
_ => {}
@@ -242,7 +263,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
242263
ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id, false),
243264
ExprKind::Repeat(_, ref count) => self.visit_const_expr(count),
244265
ExprKind::Closure(..) => {
245-
let def = self.create_def(expr.id, DefPathData::ClosureExpr);
266+
let def = self.create_def(expr.id,
267+
DefPathData::ClosureExpr,
268+
REGULAR_SPACE);
246269
self.parent_def = Some(def);
247270
}
248271
_ => {}
@@ -257,7 +280,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
257280
TyKind::Mac(..) => return self.visit_macro_invoc(ty.id, false),
258281
TyKind::Array(_, ref length) => self.visit_const_expr(length),
259282
TyKind::ImplTrait(..) => {
260-
self.create_def(ty.id, DefPathData::ImplTrait);
283+
self.create_def(ty.id, DefPathData::ImplTrait, REGULAR_SPACE);
261284
}
262285
TyKind::Typeof(ref expr) => self.visit_const_expr(expr),
263286
_ => {}
@@ -266,7 +289,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
266289
}
267290

268291
fn visit_lifetime_def(&mut self, def: &'a LifetimeDef) {
269-
self.create_def(def.lifetime.id, DefPathData::LifetimeDef(def.lifetime.name.as_str()));
292+
self.create_def(def.lifetime.id,
293+
DefPathData::LifetimeDef(def.lifetime.name.as_str()),
294+
REGULAR_SPACE);
270295
}
271296

272297
fn visit_stmt(&mut self, stmt: &'a Stmt) {

0 commit comments

Comments
 (0)