Skip to content

Commit 0c73e5f

Browse files
committed
auto merge of #12809 : eddyb/rust/ty-arena, r=cmr
This was inspired by seeing a LLVM flatline of **~600MB** when running rustc with jemalloc (each type's `t_box_` is allocated on the heap, creating a lot of fragmentation, which jemalloc can deal with, unlike glibc).
2 parents 6f34760 + 8bfbcdd commit 0c73e5f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+2299
-2378
lines changed

src/librustc/driver/driver.rs

+11-7
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

+12-9
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
}
@@ -260,25 +262,25 @@ impl pprust::PpAnn for HygieneAnnotation {
260262
}
261263

262264

263-
struct TypedAnnotation {
264-
analysis: CrateAnalysis,
265+
struct TypedAnnotation<'tcx> {
266+
analysis: CrateAnalysis<'tcx>,
265267
}
266268

267-
impl PrinterSupport for TypedAnnotation {
269+
impl<'tcx> PrinterSupport for TypedAnnotation<'tcx> {
268270
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn { self as &pprust::PpAnn }
269271
}
270272

271-
impl SessionCarrier for TypedAnnotation {
273+
impl<'tcx> SessionCarrier for TypedAnnotation<'tcx> {
272274
fn sess<'a>(&'a self) -> &'a Session { &self.analysis.ty_cx.sess }
273275
}
274276

275-
impl AstMapCarrier for TypedAnnotation {
277+
impl<'tcx> AstMapCarrier for TypedAnnotation<'tcx> {
276278
fn ast_map<'a>(&'a self) -> Option<&'a ast_map::Map> {
277279
Some(&self.analysis.ty_cx.map)
278280
}
279281
}
280282

281-
impl pprust::PpAnn for TypedAnnotation {
283+
impl<'tcx> pprust::PpAnn for TypedAnnotation<'tcx> {
282284
fn pre(&self,
283285
s: &mut pprust::State,
284286
node: pprust::AnnNode) -> io::IoResult<()> {
@@ -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/lint/builtin.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,11 @@ impl LintPass for TypeLimits {
340340
declare_lint!(CTYPES, Warn,
341341
"proper use of libc types in foreign modules")
342342

343-
struct CTypesVisitor<'a> {
344-
cx: &'a Context<'a>
343+
struct CTypesVisitor<'a, 'tcx: 'a> {
344+
cx: &'a Context<'a, 'tcx>
345345
}
346346

347-
impl<'a> CTypesVisitor<'a> {
347+
impl<'a, 'tcx> CTypesVisitor<'a, 'tcx> {
348348
fn check_def(&mut self, sp: Span, ty_id: ast::NodeId, path_id: ast::NodeId) {
349349
match self.cx.tcx.def_map.borrow().get_copy(&path_id) {
350350
def::DefPrimTy(ast::TyInt(ast::TyI)) => {
@@ -375,7 +375,7 @@ impl<'a> CTypesVisitor<'a> {
375375
}
376376
}
377377

378-
impl<'a> Visitor<()> for CTypesVisitor<'a> {
378+
impl<'a, 'tcx> Visitor<()> for CTypesVisitor<'a, 'tcx> {
379379
fn visit_ty(&mut self, ty: &ast::Ty, _: ()) {
380380
match ty.node {
381381
ast::TyPath(_, _, id) => self.check_def(ty.span, ty.id, id),
@@ -505,11 +505,11 @@ impl LintPass for HeapMemory {
505505
declare_lint!(RAW_POINTER_DERIVING, Warn,
506506
"uses of #[deriving] with raw pointers are rarely correct")
507507

508-
struct RawPtrDerivingVisitor<'a> {
509-
cx: &'a Context<'a>
508+
struct RawPtrDerivingVisitor<'a, 'tcx: 'a> {
509+
cx: &'a Context<'a, 'tcx>
510510
}
511511

512-
impl<'a> Visitor<()> for RawPtrDerivingVisitor<'a> {
512+
impl<'a, 'tcx> Visitor<()> for RawPtrDerivingVisitor<'a, 'tcx> {
513513
fn visit_ty(&mut self, ty: &ast::Ty, _: ()) {
514514
static MSG: &'static str = "use of `#[deriving]` with a raw pointer";
515515
match ty.node {

src/librustc/lint/context.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ impl LintStore {
231231
}
232232

233233
/// Context for lint checking.
234-
pub struct Context<'a> {
234+
pub struct Context<'a, 'tcx: 'a> {
235235
/// Type context we're checking in.
236-
pub tcx: &'a ty::ctxt,
236+
pub tcx: &'a ty::ctxt<'tcx>,
237237

238238
/// The crate being checked.
239239
pub krate: &'a ast::Crate,
@@ -345,10 +345,10 @@ pub fn raw_emit_lint(sess: &Session, lint: &'static Lint,
345345
}
346346
}
347347

348-
impl<'a> Context<'a> {
349-
fn new(tcx: &'a ty::ctxt,
348+
impl<'a, 'tcx> Context<'a, 'tcx> {
349+
fn new(tcx: &'a ty::ctxt<'tcx>,
350350
krate: &'a ast::Crate,
351-
exported_items: &'a ExportedItems) -> Context<'a> {
351+
exported_items: &'a ExportedItems) -> Context<'a, 'tcx> {
352352
// We want to own the lint store, so move it out of the session.
353353
let lint_store = mem::replace(&mut *tcx.sess.lint_store.borrow_mut(),
354354
LintStore::new());
@@ -476,8 +476,8 @@ impl<'a> Context<'a> {
476476
}
477477
}
478478

479-
impl<'a> AstConv for Context<'a>{
480-
fn tcx<'a>(&'a self) -> &'a ty::ctxt { self.tcx }
479+
impl<'a, 'tcx> AstConv<'tcx> for Context<'a, 'tcx>{
480+
fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> { self.tcx }
481481

482482
fn get_item_ty(&self, id: ast::DefId) -> ty::Polytype {
483483
ty::lookup_item_type(self.tcx, id)
@@ -492,7 +492,7 @@ impl<'a> AstConv for Context<'a>{
492492
}
493493
}
494494

495-
impl<'a> Visitor<()> for Context<'a> {
495+
impl<'a, 'tcx> Visitor<()> for Context<'a, 'tcx> {
496496
fn visit_item(&mut self, it: &ast::Item, _: ()) {
497497
self.with_lint_attrs(it.attrs.as_slice(), |cx| {
498498
run_lints!(cx, check_item, it);
@@ -663,7 +663,7 @@ impl<'a> Visitor<()> for Context<'a> {
663663
}
664664

665665
// Output any lints that were previously added to the session.
666-
impl<'a> IdVisitingOperation for Context<'a> {
666+
impl<'a, 'tcx> IdVisitingOperation for Context<'a, 'tcx> {
667667
fn visit_id(&self, id: ast::NodeId) {
668668
match self.tcx.sess.lints.borrow_mut().pop(&id) {
669669
None => {}

src/librustc/metadata/encoder.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ pub type EncodeInlinedItem<'a> = |ecx: &EncodeContext,
7171
rbml_w: &mut Encoder,
7272
ii: InlinedItemRef|: 'a;
7373

74-
pub struct EncodeParams<'a> {
74+
pub struct EncodeParams<'a, 'tcx: 'a> {
7575
pub diag: &'a SpanHandler,
76-
pub tcx: &'a ty::ctxt,
76+
pub tcx: &'a ty::ctxt<'tcx>,
7777
pub reexports2: &'a middle::resolve::ExportMap2,
7878
pub item_symbols: &'a RefCell<NodeMap<String>>,
7979
pub non_inlineable_statics: &'a RefCell<NodeSet>,
@@ -83,9 +83,9 @@ pub struct EncodeParams<'a> {
8383
pub reachable: &'a NodeSet,
8484
}
8585

86-
pub struct EncodeContext<'a> {
86+
pub struct EncodeContext<'a, 'tcx: 'a> {
8787
pub diag: &'a SpanHandler,
88-
pub tcx: &'a ty::ctxt,
88+
pub tcx: &'a ty::ctxt<'tcx>,
8989
pub reexports2: &'a middle::resolve::ExportMap2,
9090
pub item_symbols: &'a RefCell<NodeMap<String>>,
9191
pub non_inlineable_statics: &'a RefCell<NodeSet>,
@@ -1793,12 +1793,12 @@ fn encode_struct_field_attrs(rbml_w: &mut Encoder, krate: &Crate) {
17931793

17941794

17951795

1796-
struct ImplVisitor<'a,'b:'a,'c:'a> {
1797-
ecx: &'a EncodeContext<'b>,
1796+
struct ImplVisitor<'a, 'b:'a, 'c:'a, 'tcx:'b> {
1797+
ecx: &'a EncodeContext<'b, 'tcx>,
17981798
rbml_w: &'a mut Encoder<'c>,
17991799
}
18001800

1801-
impl<'a,'b,'c> Visitor<()> for ImplVisitor<'a,'b,'c> {
1801+
impl<'a, 'b, 'c, 'tcx> Visitor<()> for ImplVisitor<'a, 'b, 'c, 'tcx> {
18021802
fn visit_item(&mut self, item: &Item, _: ()) {
18031803
match item.node {
18041804
ItemImpl(_, Some(ref trait_ref), _, _) => {

src/librustc/metadata/tydecode.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ pub enum DefIdSource {
5959
pub type conv_did<'a> =
6060
|source: DefIdSource, ast::DefId|: 'a -> ast::DefId;
6161

62-
pub struct PState<'a> {
62+
pub struct PState<'a, 'tcx: 'a> {
6363
data: &'a [u8],
6464
krate: ast::CrateNum,
6565
pos: uint,
66-
tcx: &'a ty::ctxt
66+
tcx: &'a ty::ctxt<'tcx>
6767
}
6868

6969
fn peek(st: &PState) -> char {
@@ -105,8 +105,9 @@ fn parse_ident_(st: &mut PState, is_last: |char| -> bool) -> ast::Ident {
105105
})
106106
}
107107

108-
pub fn parse_state_from_data<'a>(data: &'a [u8], crate_num: ast::CrateNum,
109-
pos: uint, tcx: &'a ty::ctxt) -> PState<'a> {
108+
pub fn parse_state_from_data<'a, 'tcx>(data: &'a [u8], crate_num: ast::CrateNum,
109+
pos: uint, tcx: &'a ty::ctxt<'tcx>)
110+
-> PState<'a, 'tcx> {
110111
PState {
111112
data: data,
112113
krate: crate_num,

src/librustc/metadata/tyencode.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ use rbml::io::SeekableMemWriter;
3131

3232
macro_rules! mywrite( ($($arg:tt)*) => ({ write!($($arg)*); }) )
3333

34-
pub struct ctxt<'a> {
34+
pub struct ctxt<'a, 'tcx: 'a> {
3535
pub diag: &'a SpanHandler,
3636
// Def -> str Callback:
3737
pub ds: fn(DefId) -> String,
3838
// The type context.
39-
pub tcx: &'a ty::ctxt,
39+
pub tcx: &'a ty::ctxt<'tcx>,
4040
pub abbrevs: &'a abbrev_map
4141
}
4242

src/librustc/middle/astencode.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ use serialize::{EncoderHelpers};
5454
#[cfg(test)] use syntax::print::pprust;
5555
#[cfg(test)] use std::gc::Gc;
5656

57-
struct DecodeContext<'a> {
57+
struct DecodeContext<'a, 'tcx: 'a> {
5858
cdata: &'a cstore::crate_metadata,
59-
tcx: &'a ty::ctxt,
59+
tcx: &'a ty::ctxt<'tcx>,
6060
}
6161

62-
struct ExtendedDecodeContext<'a> {
63-
dcx: &'a DecodeContext<'a>,
62+
struct ExtendedDecodeContext<'a, 'tcx: 'a> {
63+
dcx: &'a DecodeContext<'a, 'tcx>,
6464
from_id_range: ast_util::IdRange,
6565
to_id_range: ast_util::IdRange
6666
}
@@ -176,7 +176,7 @@ fn reserve_id_range(sess: &Session,
176176
ast_util::IdRange { min: to_id_min, max: to_id_max }
177177
}
178178

179-
impl<'a> ExtendedDecodeContext<'a> {
179+
impl<'a, 'tcx> ExtendedDecodeContext<'a, 'tcx> {
180180
pub fn tr_id(&self, id: ast::NodeId) -> ast::NodeId {
181181
/*!
182182
* Translates an internal id, meaning a node id that is known
@@ -382,11 +382,11 @@ fn decode_ast(par_doc: rbml::Doc) -> ast::InlinedItem {
382382
Decodable::decode(&mut d).unwrap()
383383
}
384384

385-
struct AstRenumberer<'a> {
386-
xcx: &'a ExtendedDecodeContext<'a>,
385+
struct AstRenumberer<'a, 'tcx: 'a> {
386+
xcx: &'a ExtendedDecodeContext<'a, 'tcx>,
387387
}
388388

389-
impl<'a> ast_map::FoldOps for AstRenumberer<'a> {
389+
impl<'a, 'tcx> ast_map::FoldOps for AstRenumberer<'a, 'tcx> {
390390
fn new_id(&self, id: ast::NodeId) -> ast::NodeId {
391391
if id == ast::DUMMY_NODE_ID {
392392
// Used by ast_map to map the NodeInlinedParent.
@@ -914,12 +914,12 @@ fn encode_vec_per_param_space<T>(rbml_w: &mut Encoder,
914914
// ______________________________________________________________________
915915
// Encoding and decoding the side tables
916916

917-
trait get_ty_str_ctxt {
918-
fn ty_str_ctxt<'a>(&'a self) -> tyencode::ctxt<'a>;
917+
trait get_ty_str_ctxt<'tcx> {
918+
fn ty_str_ctxt<'a>(&'a self) -> tyencode::ctxt<'a, 'tcx>;
919919
}
920920

921-
impl<'a> get_ty_str_ctxt for e::EncodeContext<'a> {
922-
fn ty_str_ctxt<'a>(&'a self) -> tyencode::ctxt<'a> {
921+
impl<'a, 'tcx> get_ty_str_ctxt<'tcx> for e::EncodeContext<'a, 'tcx> {
922+
fn ty_str_ctxt<'a>(&'a self) -> tyencode::ctxt<'a, 'tcx> {
923923
tyencode::ctxt {
924924
diag: self.tcx.sess.diagnostic(),
925925
ds: e::def_to_string,

0 commit comments

Comments
 (0)