Skip to content

Commit 7d79cc7

Browse files
committed
auto merge of #11616 : huonw/rust/ast_map, r=pnkfelix
NodeIds are sequential integers starting at zero, so we can achieve some memory savings by just storing the items all in a line in a vector. The occupancy for typical crates seems to be 75-80%, so we're already more efficient than a HashMap (maximum occupancy 75%), not even counting the extra book-keeping that HashMap does.
2 parents 6d55211 + 68517a2 commit 7d79cc7

25 files changed

+195
-217
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,8 @@ fn encode_reexported_static_methods(ecx: &EncodeContext,
496496
ebml_w: &mut writer::Encoder,
497497
mod_path: &[ast_map::PathElem],
498498
exp: &middle::resolve::Export2) {
499-
let items = ecx.tcx.items.borrow();
500-
match items.get().find(&exp.def_id.node) {
501-
Some(&ast_map::NodeItem(item, path)) => {
499+
match ecx.tcx.items.find(exp.def_id.node) {
500+
Some(ast_map::NodeItem(item, path)) => {
502501
let original_name = ecx.tcx.sess.str_of(item.ident);
503502

504503
//
@@ -1347,8 +1346,7 @@ fn my_visit_item(i: &Item,
13471346
ebml_w: &mut writer::Encoder,
13481347
ecx_ptr: *int,
13491348
index: @RefCell<~[entry<i64>]>) {
1350-
let items = items.borrow();
1351-
match items.get().get_copy(&i.id) {
1349+
match items.get(i.id) {
13521350
ast_map::NodeItem(_, pt) => {
13531351
let mut ebml_w = unsafe {
13541352
ebml_w.unsafe_clone()
@@ -1366,8 +1364,7 @@ fn my_visit_foreign_item(ni: &ForeignItem,
13661364
ebml_w: &mut writer::Encoder,
13671365
ecx_ptr:*int,
13681366
index: @RefCell<~[entry<i64>]>) {
1369-
let items = items.borrow();
1370-
match items.get().get_copy(&ni.id) {
1367+
match items.get(ni.id) {
13711368
ast_map::NodeForeignItem(_, abi, _, pt) => {
13721369
debug!("writing foreign item {}::{}",
13731370
ast_map::path_to_str(

src/librustc/middle/borrowck/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,8 @@ impl BorrowckCtxt {
550550
move_data::Declared => {}
551551

552552
move_data::MoveExpr => {
553-
let items = self.tcx.items.borrow();
554-
let (expr_ty, expr_span) = match items.get().find(&move.id) {
555-
Some(&ast_map::NodeExpr(expr)) => {
553+
let (expr_ty, expr_span) = match self.tcx.items.find(move.id) {
554+
Some(ast_map::NodeExpr(expr)) => {
556555
(ty::expr_ty_adjusted(self.tcx, expr), expr.span)
557556
}
558557
r => self.tcx.sess.bug(format!("MoveExpr({:?}) maps to {:?}, not Expr",
@@ -578,9 +577,8 @@ impl BorrowckCtxt {
578577
}
579578

580579
move_data::Captured => {
581-
let items = self.tcx.items.borrow();
582-
let (expr_ty, expr_span) = match items.get().find(&move.id) {
583-
Some(&ast_map::NodeExpr(expr)) => {
580+
let (expr_ty, expr_span) = match self.tcx.items.find(move.id) {
581+
Some(ast_map::NodeExpr(expr)) => {
584582
(ty::expr_ty_adjusted(self.tcx, expr), expr.span)
585583
}
586584
r => self.tcx.sess.bug(format!("Captured({:?}) maps to {:?}, not Expr",
@@ -768,9 +766,8 @@ impl BorrowckCtxt {
768766
out: &mut ~str) {
769767
match *loan_path {
770768
LpVar(id) => {
771-
let items = self.tcx.items.borrow();
772-
match items.get().find(&id) {
773-
Some(&ast_map::NodeLocal(ref ident, _)) => {
769+
match self.tcx.items.find(id) {
770+
Some(ast_map::NodeLocal(ref ident, _)) => {
774771
out.push_str(token::ident_to_str(ident));
775772
}
776773
r => {

src/librustc/middle/check_const.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ impl<'a> Visitor<()> for CheckItemRecursionVisitor<'a> {
250250
match def_map.get().find(&e.id) {
251251
Some(&DefStatic(def_id, _)) if
252252
ast_util::is_local(def_id) => {
253-
let ast_map = self.ast_map.borrow();
254-
match ast_map.get().get_copy(&def_id.node) {
253+
match self.ast_map.get(def_id.node) {
255254
ast_map::NodeItem(it, _) => {
256255
self.visit_item(it, ());
257256
}

src/librustc/middle/const_eval.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ pub fn lookup_variant_by_id(tcx: ty::ctxt,
108108

109109
if ast_util::is_local(enum_def) {
110110
{
111-
let items = tcx.items.borrow();
112-
match items.get().find(&enum_def.node) {
111+
match tcx.items.find(enum_def.node) {
113112
None => None,
114-
Some(&ast_map::NodeItem(it, _)) => match it.node {
113+
Some(ast_map::NodeItem(it, _)) => match it.node {
115114
ItemEnum(ast::EnumDef { variants: ref variants }, _) => {
116115
variant_expr(*variants, variant_def.node)
117116
}
@@ -161,10 +160,9 @@ pub fn lookup_const_by_id(tcx: ty::ctxt, def_id: ast::DefId)
161160
-> Option<@Expr> {
162161
if ast_util::is_local(def_id) {
163162
{
164-
let items = tcx.items.borrow();
165-
match items.get().find(&def_id.node) {
163+
match tcx.items.find(def_id.node) {
166164
None => None,
167-
Some(&ast_map::NodeItem(it, _)) => match it.node {
165+
Some(ast_map::NodeItem(it, _)) => match it.node {
168166
ItemStatic(_, ast::MutImmutable, const_expr) => {
169167
Some(const_expr)
170168
}

src/librustc/middle/dead.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ fn should_explore(tcx: ty::ctxt, def_id: ast::DefId) -> bool {
3838
return false;
3939
}
4040

41-
let items = tcx.items.borrow();
42-
match items.get().find(&def_id.node) {
43-
Some(&ast_map::NodeItem(..))
44-
| Some(&ast_map::NodeMethod(..))
45-
| Some(&ast_map::NodeForeignItem(..))
46-
| Some(&ast_map::NodeTraitMethod(..)) => true,
41+
match tcx.items.find(def_id.node) {
42+
Some(ast_map::NodeItem(..))
43+
| Some(ast_map::NodeMethod(..))
44+
| Some(ast_map::NodeForeignItem(..))
45+
| Some(ast_map::NodeTraitMethod(..)) => true,
4746
_ => false
4847
}
4948
}
@@ -136,9 +135,8 @@ impl MarkSymbolVisitor {
136135
}
137136
scanned.insert(id);
138137

139-
let items = self.tcx.items.borrow();
140-
match items.get().find(&id) {
141-
Some(node) => {
138+
match self.tcx.items.find(id) {
139+
Some(ref node) => {
142140
self.live_symbols.insert(id);
143141
self.visit_node(node);
144142
}

src/librustc/middle/entry.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ fn find_item(item: &Item, ctxt: &mut EntryContext) {
7575
ItemFn(..) => {
7676
if item.ident.name == special_idents::main.name {
7777
{
78-
let ast_map = ctxt.ast_map.borrow();
79-
match ast_map.get().find(&item.id) {
80-
Some(&ast_map::NodeItem(_, path)) => {
78+
match ctxt.ast_map.find(item.id) {
79+
Some(ast_map::NodeItem(_, path)) => {
8180
if path.len() == 0 {
8281
// This is a top-level function so can be 'main'
8382
if ctxt.main_fn.is_none() {

src/librustc/middle/lint.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,8 +1330,7 @@ fn check_stability(cx: &Context, e: &ast::Expr) {
13301330

13311331
let stability = if ast_util::is_local(id) {
13321332
// this crate
1333-
let items = cx.tcx.items.borrow();
1334-
match items.get().find(&id.node) {
1333+
match cx.tcx.items.find(id.node) {
13351334
Some(ast_node) => {
13361335
let s = ast_node.with_attrs(|attrs| {
13371336
attrs.map(|a| {

src/librustc/middle/privacy.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,7 @@ impl<'a> PrivacyVisitor<'a> {
431431
let mut closest_private_id = did.node;
432432
loop {
433433
debug!("privacy - examining {}", self.nodestr(closest_private_id));
434-
let items = self.tcx.items.borrow();
435-
let vis = match items.get().find(&closest_private_id) {
434+
let vis = match self.tcx.items.find(closest_private_id) {
436435
// If this item is a method, then we know for sure that it's an
437436
// actual method and not a static method. The reason for this is
438437
// that these cases are only hit in the ExprMethodCall
@@ -449,22 +448,22 @@ impl<'a> PrivacyVisitor<'a> {
449448
// invocation.
450449
// FIXME(#10573) is this the right behavior? Why not consider
451450
// where the method was defined?
452-
Some(&ast_map::NodeMethod(ref m, imp, _)) => {
451+
Some(ast_map::NodeMethod(ref m, imp, _)) => {
453452
match ty::impl_trait_ref(self.tcx, imp) {
454453
Some(..) => return Allowable,
455454
_ if m.vis == ast::Public => return Allowable,
456455
_ => m.vis
457456
}
458457
}
459-
Some(&ast_map::NodeTraitMethod(..)) => {
458+
Some(ast_map::NodeTraitMethod(..)) => {
460459
return Allowable;
461460
}
462461

463462
// This is not a method call, extract the visibility as one
464463
// would normally look at it
465-
Some(&ast_map::NodeItem(it, _)) => it.vis,
466-
Some(&ast_map::NodeForeignItem(_, _, v, _)) => v,
467-
Some(&ast_map::NodeVariant(ref v, _, _)) => {
464+
Some(ast_map::NodeItem(it, _)) => it.vis,
465+
Some(ast_map::NodeForeignItem(_, _, v, _)) => v,
466+
Some(ast_map::NodeVariant(ref v, _, _)) => {
468467
// sadly enum variants still inherit visibility, so only
469468
// break out of this is explicitly private
470469
if v.node.vis == ast::Private { break }
@@ -538,9 +537,8 @@ impl<'a> PrivacyVisitor<'a> {
538537
self.tcx.sess.span_err(span, format!("{} is inaccessible",
539538
msg));
540539
}
541-
let items = self.tcx.items.borrow();
542-
match items.get().find(&id) {
543-
Some(&ast_map::NodeItem(item, _)) => {
540+
match self.tcx.items.find(id) {
541+
Some(ast_map::NodeItem(item, _)) => {
544542
let desc = match item.node {
545543
ast::ItemMod(..) => "module",
546544
ast::ItemTrait(..) => "trait",

src/librustc/middle/reachable.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ fn method_might_be_inlined(tcx: ty::ctxt, method: &ast::Method,
6666
}
6767
if is_local(impl_src) {
6868
{
69-
let items = tcx.items.borrow();
70-
match items.get().find(&impl_src.node) {
71-
Some(&ast_map::NodeItem(item, _)) => {
69+
match tcx.items.find(impl_src.node) {
70+
Some(ast_map::NodeItem(item, _)) => {
7271
item_might_be_inlined(item)
7372
}
7473
Some(..) | None => {
@@ -213,30 +212,29 @@ impl ReachableContext {
213212
}
214213

215214
let node_id = def_id.node;
216-
let items = tcx.items.borrow();
217-
match items.get().find(&node_id) {
218-
Some(&ast_map::NodeItem(item, _)) => {
215+
match tcx.items.find(node_id) {
216+
Some(ast_map::NodeItem(item, _)) => {
219217
match item.node {
220218
ast::ItemFn(..) => item_might_be_inlined(item),
221219
_ => false,
222220
}
223221
}
224-
Some(&ast_map::NodeTraitMethod(trait_method, _, _)) => {
222+
Some(ast_map::NodeTraitMethod(trait_method, _, _)) => {
225223
match *trait_method {
226224
ast::Required(_) => false,
227225
ast::Provided(_) => true,
228226
}
229227
}
230-
Some(&ast_map::NodeMethod(method, impl_did, _)) => {
228+
Some(ast_map::NodeMethod(method, impl_did, _)) => {
231229
if generics_require_inlining(&method.generics) ||
232230
attributes_specify_inlining(method.attrs) {
233231
true
234232
} else {
235233
// Check the impl. If the generics on the self type of the
236234
// impl require inlining, this method does too.
237235
assert!(impl_did.crate == ast::LOCAL_CRATE);
238-
match items.get().find(&impl_did.node) {
239-
Some(&ast_map::NodeItem(item, _)) => {
236+
match tcx.items.find(impl_did.node) {
237+
Some(ast_map::NodeItem(item, _)) => {
240238
match item.node {
241239
ast::ItemImpl(ref generics, _, _, _) => {
242240
generics_require_inlining(generics)
@@ -294,9 +292,8 @@ impl ReachableContext {
294292
};
295293

296294
scanned.insert(search_item);
297-
let items = self.tcx.items.borrow();
298-
match items.get().find(&search_item) {
299-
Some(item) => self.propagate_node(item, search_item,
295+
match self.tcx.items.find(search_item) {
296+
Some(ref item) => self.propagate_node(item, search_item,
300297
&mut visitor),
301298
None if search_item == ast::CRATE_NODE_ID => {}
302299
None => {

src/librustc/middle/trans/base.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,8 +1731,7 @@ impl Visitor<()> for TransItemVisitor {
17311731
pub fn trans_item(ccx: @CrateContext, item: &ast::Item) {
17321732
let _icx = push_ctxt("trans_item");
17331733
let path = {
1734-
let items = ccx.tcx.items.borrow();
1735-
match items.get().get_copy(&item.id) {
1734+
match ccx.tcx.items.get(item.id) {
17361735
ast_map::NodeItem(_, p) => p,
17371736
// tjc: ?
17381737
_ => fail!("trans_item"),
@@ -2034,10 +2033,7 @@ pub fn get_item_val(ccx: @CrateContext, id: ast::NodeId) -> ValueRef {
20342033
Some(v) => v,
20352034
None => {
20362035
let mut foreign = false;
2037-
let item = {
2038-
let items = ccx.tcx.items.borrow();
2039-
items.get().get_copy(&id)
2040-
};
2036+
let item = ccx.tcx.items.get(id);
20412037
let val = match item {
20422038
ast_map::NodeItem(i, pth) => {
20432039

src/librustc/middle/trans/callee.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,12 @@ pub fn trans_fn_ref_with_vtables(
365365
must_monomorphise = true;
366366
} else if def_id.crate == ast::LOCAL_CRATE {
367367
{
368-
let items = ccx.tcx.items.borrow();
369368
let map_node = session::expect(
370369
ccx.sess,
371-
items.get().find(&def_id.node),
370+
ccx.tcx.items.find(def_id.node),
372371
|| format!("local item should be in ast map"));
373372

374-
match *map_node {
373+
match map_node {
375374
ast_map::NodeForeignItem(_, abis, _, _) => {
376375
must_monomorphise = abis.is_intrinsic()
377376
}

src/librustc/middle/trans/consts.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ pub fn get_const_val(cx: @CrateContext,
166166
def_id = inline::maybe_instantiate_inline(cx, def_id);
167167
}
168168

169-
let opt_item = {
170-
let items = cx.tcx.items.borrow();
171-
items.get().get_copy(&def_id.node)
172-
};
169+
let opt_item = cx.tcx.items.get(def_id.node);
173170

174171
match opt_item {
175172
ast_map::NodeItem(item, _) => {

src/librustc/middle/trans/debuginfo.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,8 @@ pub fn create_captured_var_metadata(bcx: &Block,
322322

323323
let cx = bcx.ccx();
324324

325-
let ast_item = {
326-
let items = cx.tcx.items.borrow();
327-
items.get().find_copy(&node_id)
328-
};
325+
let ast_item = cx.tcx.items.find(node_id);
326+
329327
let variable_ident = match ast_item {
330328
None => {
331329
cx.sess.span_bug(span, "debuginfo::create_captured_var_metadata() - NodeId not found");
@@ -423,10 +421,7 @@ pub fn create_self_argument_metadata(bcx: &Block,
423421
}
424422

425423
// Extract the span of the self argument from the method's AST
426-
let fnitem = {
427-
let items = bcx.ccx().tcx.items.borrow();
428-
items.get().get_copy(&bcx.fcx.id)
429-
};
424+
let fnitem = bcx.ccx().tcx.items.get(bcx.fcx.id);
430425
let span = match fnitem {
431426
ast_map::NodeMethod(method, _, _) => {
432427
method.explicit_self.span
@@ -613,10 +608,8 @@ pub fn create_function_debug_context(cx: &CrateContext,
613608

614609
let empty_generics = ast::Generics { lifetimes: opt_vec::Empty, ty_params: opt_vec::Empty };
615610

616-
let fnitem = {
617-
let items = cx.tcx.items.borrow();
618-
items.get().get_copy(&fn_ast_id)
619-
};
611+
let fnitem = cx.tcx.items.get(fn_ast_id);
612+
620613
let (ident, fn_decl, generics, top_level_block, span, has_path) = match fnitem {
621614
ast_map::NodeItem(ref item, _) => {
622615
match item.node {
@@ -1098,8 +1091,7 @@ fn scope_metadata(fcx: &FunctionContext,
10981091
match scope_map.get().find_copy(&node_id) {
10991092
Some(scope_metadata) => scope_metadata,
11001093
None => {
1101-
let items = fcx.ccx.tcx.items.borrow();
1102-
let node = items.get().get_copy(&node_id);
1094+
let node = fcx.ccx.tcx.items.get(node_id);
11031095

11041096
fcx.ccx.sess.span_bug(span,
11051097
format!("debuginfo: Could not find scope info for node {:?}", node));
@@ -1419,9 +1411,8 @@ fn describe_enum_variant(cx: &CrateContext,
14191411
// Find the source code location of the variant's definition
14201412
let variant_definition_span = if variant_info.id.crate == ast::LOCAL_CRATE {
14211413
{
1422-
let items = cx.tcx.items.borrow();
1423-
match items.get().find(&variant_info.id.node) {
1424-
Some(&ast_map::NodeVariant(ref variant, _, _)) => variant.span,
1414+
match cx.tcx.items.find(variant_info.id.node) {
1415+
Some(ast_map::NodeVariant(ref variant, _, _)) => variant.span,
14251416
ref node => {
14261417
cx.sess.span_warn(span,
14271418
format!("debuginfo::enum_metadata()::\
@@ -2300,9 +2291,8 @@ fn get_namespace_and_span_for_item(cx: &CrateContext,
23002291
let containing_scope = namespace_for_item(cx, def_id, warning_span).scope;
23012292
let definition_span = if def_id.crate == ast::LOCAL_CRATE {
23022293
{
2303-
let items = cx.tcx.items.borrow();
2304-
let definition_span = match items.get().find(&def_id.node) {
2305-
Some(&ast_map::NodeItem(item, _)) => item.span,
2294+
let definition_span = match cx.tcx.items.find(def_id.node) {
2295+
Some(ast_map::NodeItem(item, _)) => item.span,
23062296
ref node => {
23072297
cx.sess.span_warn(warning_span,
23082298
format!("debuginfo::\

src/librustc/middle/trans/foreign.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,8 @@ pub fn trans_foreign_mod(ccx: @CrateContext,
354354
for &foreign_item in foreign_mod.items.iter() {
355355
match foreign_item.node {
356356
ast::ForeignItemFn(..) => {
357-
let items = ccx.tcx.items.borrow();
358357
let (abis, mut path) =
359-
match items.get().get_copy(&foreign_item.id) {
358+
match ccx.tcx.items.get(foreign_item.id) {
360359
ast_map::NodeForeignItem(_, abis, _, path) => {
361360
(abis, (*path).clone())
362361
}

0 commit comments

Comments
 (0)