Skip to content

Commit 22f8b84

Browse files
committed
rustc: use a TypedArena to allocate types in the type context.
1 parent ab7b1c8 commit 22f8b84

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

src/librustc/driver/driver.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use serialize::{json, Encodable};
3232

3333
use std::io;
3434
use std::io::fs;
35+
use arena::TypedArena;
3536
use syntax::ast;
3637
use syntax::attr;
3738
use syntax::attr::{AttrMetaMethods};
@@ -86,8 +87,9 @@ pub fn compile_input(sess: Session,
8687

8788
if stop_after_phase_2(&sess) { return; }
8889

90+
let type_arena = TypedArena::new();
8991
let analysis = phase_3_run_analysis_passes(sess, &expanded_crate,
90-
ast_map, id);
92+
ast_map, &type_arena, id);
9193
phase_save_analysis(&analysis.ty_cx.sess, &expanded_crate, &analysis, outdir);
9294
if stop_after_phase_3(&analysis.ty_cx.sess) { return; }
9395
let (tcx, trans) = phase_4_translate_to_llvm(expanded_crate, analysis);
@@ -299,11 +301,11 @@ pub fn phase_2_configure_and_expand(sess: &Session,
299301
Some((krate, map))
300302
}
301303

302-
pub struct CrateAnalysis {
304+
pub struct CrateAnalysis<'tcx> {
303305
pub exp_map2: middle::resolve::ExportMap2,
304306
pub exported_items: middle::privacy::ExportedItems,
305307
pub public_items: middle::privacy::PublicItems,
306-
pub ty_cx: ty::ctxt,
308+
pub ty_cx: ty::ctxt<'tcx>,
307309
pub reachable: NodeSet,
308310
pub name: String,
309311
}
@@ -312,10 +314,11 @@ pub struct CrateAnalysis {
312314
/// Run the resolution, typechecking, region checking and other
313315
/// miscellaneous analysis passes on the crate. Return various
314316
/// structures carrying the results of the analysis.
315-
pub fn phase_3_run_analysis_passes(sess: Session,
316-
krate: &ast::Crate,
317-
ast_map: syntax::ast_map::Map,
318-
name: String) -> CrateAnalysis {
317+
pub fn phase_3_run_analysis_passes<'tcx>(sess: Session,
318+
krate: &ast::Crate,
319+
ast_map: syntax::ast_map::Map,
320+
type_arena: &'tcx TypedArena<ty::t_box_>,
321+
name: String) -> CrateAnalysis<'tcx> {
319322
let time_passes = sess.time_passes();
320323

321324
time(time_passes, "external crate/lib resolution", (), |_|
@@ -362,6 +365,7 @@ pub fn phase_3_run_analysis_passes(sess: Session,
362365
stability::Index::build(krate));
363366

364367
let ty_cx = ty::mk_ctxt(sess,
368+
type_arena,
365369
def_map,
366370
named_region_map,
367371
ast_map,

src/librustc/driver/pretty.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use graphviz as dot;
3333
use std::io::{mod, MemReader};
3434
use std::from_str::FromStr;
3535
use std::option;
36-
36+
use arena::TypedArena;
3737

3838
#[deriving(PartialEq, Show)]
3939
pub enum PpSourceMode {
@@ -114,7 +114,9 @@ impl PpSourceMode {
114114
}
115115
PpmTyped => {
116116
let ast_map = ast_map.expect("--pretty=typed missing ast_map");
117-
let analysis = driver::phase_3_run_analysis_passes(sess, krate, ast_map, id);
117+
let type_arena = TypedArena::new();
118+
let analysis = driver::phase_3_run_analysis_passes(sess, krate, ast_map,
119+
&type_arena, id);
118120
let annotation = TypedAnnotation { analysis: analysis };
119121
f(&annotation, payload)
120122
}
@@ -531,8 +533,9 @@ pub fn pretty_print_input(sess: Session,
531533
match code {
532534
Some(code) => {
533535
let variants = gather_flowgraph_variants(&sess);
536+
let type_arena = TypedArena::new();
534537
let analysis = driver::phase_3_run_analysis_passes(sess, &krate,
535-
ast_map, id);
538+
ast_map, &type_arena, id);
536539
print_flowgraph(variants, analysis, code, out)
537540
}
538541
None => {

src/librustc/middle/ty.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use std::mem;
5050
use std::ops;
5151
use std::rc::Rc;
5252
use std::collections::{HashMap, HashSet};
53+
use arena::TypedArena;
5354
use syntax::abi;
5455
use syntax::ast::{CrateNum, DefId, FnStyle, Ident, ItemTrait, LOCAL_CRATE};
5556
use syntax::ast::{MutImmutable, MutMutable, Name, NamedField, NodeId};
@@ -418,10 +419,13 @@ pub struct TransmuteRestriction {
418419
/// The data structure to keep track of all the information that typechecker
419420
/// generates so that so that it can be reused and doesn't have to be redone
420421
/// later on.
421-
pub struct ctxt {
422+
pub struct ctxt<'tcx> {
423+
/// The arena that types are allocated from.
424+
type_arena: &'tcx TypedArena<t_box_>,
425+
422426
/// Specifically use a speedy hash algorithm for this hash map, it's used
423427
/// quite often.
424-
pub interner: RefCell<FnvHashMap<intern_key, Box<t_box_>>>,
428+
interner: RefCell<FnvHashMap<intern_key, &'tcx t_box_>>,
425429
pub next_id: Cell<uint>,
426430
pub sess: Session,
427431
pub def_map: resolve::DefMap,
@@ -1373,21 +1377,22 @@ impl UnboxedClosureKind {
13731377
}
13741378
}
13751379

1376-
pub fn mk_ctxt(s: Session,
1377-
dm: resolve::DefMap,
1378-
named_region_map: resolve_lifetime::NamedRegionMap,
1379-
map: ast_map::Map,
1380-
freevars: freevars::freevar_map,
1381-
capture_modes: freevars::CaptureModeMap,
1382-
region_maps: middle::region::RegionMaps,
1383-
lang_items: middle::lang_items::LanguageItems,
1384-
stability: stability::Index)
1385-
-> ctxt {
1380+
pub fn mk_ctxt<'tcx>(s: Session,
1381+
type_arena: &'tcx TypedArena<t_box_>,
1382+
dm: resolve::DefMap,
1383+
named_region_map: resolve_lifetime::NamedRegionMap,
1384+
map: ast_map::Map,
1385+
freevars: freevars::freevar_map,
1386+
capture_modes: freevars::CaptureModeMap,
1387+
region_maps: middle::region::RegionMaps,
1388+
lang_items: middle::lang_items::LanguageItems,
1389+
stability: stability::Index) -> ctxt<'tcx> {
13861390
ctxt {
1391+
type_arena: type_arena,
1392+
interner: RefCell::new(FnvHashMap::new()),
13871393
named_region_map: named_region_map,
13881394
item_variance_map: RefCell::new(DefIdMap::new()),
13891395
variance_computed: Cell::new(false),
1390-
interner: RefCell::new(FnvHashMap::new()),
13911396
next_id: Cell::new(primitives::LAST_PRIMITIVE_ID),
13921397
sess: s,
13931398
def_map: dm,
@@ -1554,11 +1559,11 @@ pub fn mk_t(cx: &ctxt, st: sty) -> t {
15541559
}
15551560
}
15561561

1557-
let t = box t_box_ {
1562+
let t = cx.type_arena.alloc(t_box_ {
15581563
sty: st,
15591564
id: cx.next_id.get(),
15601565
flags: flags,
1561-
};
1566+
});
15621567

15631568
let sty_ptr = &t.sty as *const sty;
15641569

0 commit comments

Comments
 (0)