Skip to content

Commit 9a7fa1a

Browse files
committed
rustc: use a TypedArena to allocate types in the type context.
1 parent f297366 commit 9a7fa1a

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

src/librustc/driver/driver.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use serialize::{json, Encodable};
3131

3232
use std::io;
3333
use std::io::fs;
34+
use arena::TypedArena;
3435
use syntax::ast;
3536
use syntax::attr;
3637
use syntax::attr::{AttrMetaMethods};
@@ -85,8 +86,9 @@ pub fn compile_input(sess: Session,
8586

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

89+
let type_arena = TypedArena::new();
8890
let analysis = phase_3_run_analysis_passes(sess, &expanded_crate,
89-
ast_map, id);
91+
ast_map, &type_arena, id);
9092
phase_save_analysis(&analysis.ty_cx.sess, &expanded_crate, &analysis, outdir);
9193
if stop_after_phase_3(&analysis.ty_cx.sess) { return; }
9294
let (tcx, trans) = phase_4_translate_to_llvm(expanded_crate, analysis);
@@ -298,11 +300,11 @@ pub fn phase_2_configure_and_expand(sess: &Session,
298300
Some((krate, map))
299301
}
300302

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

320323
time(time_passes, "external crate/lib resolution", (), |_|
@@ -361,6 +364,7 @@ pub fn phase_3_run_analysis_passes(sess: Session,
361364
stability::Index::build(krate));
362365

363366
let ty_cx = ty::mk_ctxt(sess,
367+
type_arena,
364368
def_map,
365369
named_region_map,
366370
ast_map,

src/librustc/driver/pretty.rs

+6-3
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

+20-15
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};
@@ -410,10 +411,13 @@ pub struct TransmuteRestriction {
410411
/// The data structure to keep track of all the information that typechecker
411412
/// generates so that so that it can be reused and doesn't have to be redone
412413
/// later on.
413-
pub struct ctxt {
414+
pub struct ctxt<'tcx> {
415+
/// The arena that types are allocated from.
416+
type_arena: &'tcx TypedArena<t_box_>,
417+
414418
/// Specifically use a speedy hash algorithm for this hash map, it's used
415419
/// quite often.
416-
pub interner: RefCell<FnvHashMap<intern_key, Box<t_box_>>>,
420+
interner: RefCell<FnvHashMap<intern_key, &'tcx t_box_>>,
417421
pub next_id: Cell<uint>,
418422
pub sess: Session,
419423
pub def_map: resolve::DefMap,
@@ -1365,21 +1369,22 @@ impl UnboxedClosureKind {
13651369
}
13661370
}
13671371

1368-
pub fn mk_ctxt(s: Session,
1369-
dm: resolve::DefMap,
1370-
named_region_map: resolve_lifetime::NamedRegionMap,
1371-
map: ast_map::Map,
1372-
freevars: freevars::freevar_map,
1373-
capture_modes: freevars::CaptureModeMap,
1374-
region_maps: middle::region::RegionMaps,
1375-
lang_items: middle::lang_items::LanguageItems,
1376-
stability: stability::Index)
1377-
-> ctxt {
1372+
pub fn mk_ctxt<'tcx>(s: Session,
1373+
type_arena: &'tcx TypedArena<t_box_>,
1374+
dm: resolve::DefMap,
1375+
named_region_map: resolve_lifetime::NamedRegionMap,
1376+
map: ast_map::Map,
1377+
freevars: freevars::freevar_map,
1378+
capture_modes: freevars::CaptureModeMap,
1379+
region_maps: middle::region::RegionMaps,
1380+
lang_items: middle::lang_items::LanguageItems,
1381+
stability: stability::Index) -> ctxt<'tcx> {
13781382
ctxt {
1383+
type_arena: type_arena,
1384+
interner: RefCell::new(FnvHashMap::new()),
13791385
named_region_map: named_region_map,
13801386
item_variance_map: RefCell::new(DefIdMap::new()),
13811387
variance_computed: Cell::new(false),
1382-
interner: RefCell::new(FnvHashMap::new()),
13831388
next_id: Cell::new(primitives::LAST_PRIMITIVE_ID),
13841389
sess: s,
13851390
def_map: dm,
@@ -1546,11 +1551,11 @@ pub fn mk_t(cx: &ctxt, st: sty) -> t {
15461551
}
15471552
}
15481553

1549-
let t = box t_box_ {
1554+
let t = cx.type_arena.alloc(t_box_ {
15501555
sty: st,
15511556
id: cx.next_id.get(),
15521557
flags: flags,
1553-
};
1558+
});
15541559

15551560
let sty_ptr = &t.sty as *const sty;
15561561

0 commit comments

Comments
 (0)