Skip to content

Commit

Permalink
auto merge of #9598 : alexcrichton/rust/rc-crate, r=huonw
Browse files Browse the repository at this point in the history
This patch exposes actual ownership of an `ast::Crate` structure so it's not implicitly copied and reference counted via `@`.

The main purpose for this patch was to get rid of the massive spike in memory during the start of the compiler (this can be seen on isrustfastyet). The reason that this spike exists is that during `phase_2` we're creating many copies of the crate by folding. Because these are reference counted, all instances of the old crates aren't dropped until the end of the function, which is why so much memory is accumulated.

This patch exposes true ownership of the crate, meaning that it will be destroyed ASAP when requested. There are no code changes except for dealing with actual ownership of the crate. The large spike is then avoided: http://i.imgur.com/IO3NENy.png

This shouldn't help our overall memory usage (that still is the highest at the end), but if we ever manage to bring that down it should help us not have a 1GB spike at the beginning of compilation.
  • Loading branch information
bors committed Sep 29, 2013
2 parents 9883a62 + a86e490 commit a7cfbfb
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 219 deletions.
84 changes: 42 additions & 42 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ pub enum input {
}

pub fn phase_1_parse_input(sess: Session, cfg: ast::CrateConfig, input: &input)
-> @ast::Crate {
time(sess.time_passes(), ~"parsing", || {
-> ast::Crate {
time(sess.time_passes(), ~"parsing", (), |_| {
match *input {
file_input(ref file) => {
parse::parse_crate_from_file(&(*file), cfg.clone(), sess.parse_sess)
Expand All @@ -153,11 +153,11 @@ pub fn phase_1_parse_input(sess: Session, cfg: ast::CrateConfig, input: &input)
/// standard library and prelude.
pub fn phase_2_configure_and_expand(sess: Session,
cfg: ast::CrateConfig,
mut crate: @ast::Crate) -> @ast::Crate {
mut crate: ast::Crate) -> ast::Crate {
let time_passes = sess.time_passes();

*sess.building_library = session::building_library(sess.opts.crate_type,
crate, sess.opts.test);
&crate, sess.opts.test);


// strip before expansion to allow macros to depend on
Expand All @@ -167,29 +167,29 @@ pub fn phase_2_configure_and_expand(sess: Session,
// mod bar { macro_rules! baz!(() => {{}}) }
//
// baz! should not use this definition unless foo is enabled.
crate = time(time_passes, ~"std macros injection", ||
crate = time(time_passes, ~"std macros injection", crate, |crate|
syntax::ext::expand::inject_std_macros(sess.parse_sess,
cfg.clone(),
crate));

crate = time(time_passes, ~"configuration 1", ||
crate = time(time_passes, ~"configuration 1", crate, |crate|
front::config::strip_unconfigured_items(crate));

crate = time(time_passes, ~"expansion", ||
crate = time(time_passes, ~"expansion", crate, |crate|
syntax::ext::expand::expand_crate(sess.parse_sess, cfg.clone(),
crate));

// strip again, in case expansion added anything with a #[cfg].
crate = time(time_passes, ~"configuration 2", ||
crate = time(time_passes, ~"configuration 2", crate, |crate|
front::config::strip_unconfigured_items(crate));

crate = time(time_passes, ~"maybe building test harness", ||
crate = time(time_passes, ~"maybe building test harness", crate, |crate|
front::test::modify_for_testing(sess, crate));

crate = time(time_passes, ~"std injection", ||
crate = time(time_passes, ~"std injection", crate, |crate|
front::std_inject::maybe_inject_libstd_ref(sess, crate));

crate = time(time_passes, ~"assigning node ids", ||
crate = time(time_passes, ~"assigning node ids", crate, |crate|
front::assign_node_ids::assign_node_ids(sess, crate));

return crate;
Expand All @@ -207,41 +207,41 @@ pub struct CrateAnalysis {
/// miscellaneous analysis passes on the crate. Return various
/// structures carrying the results of the analysis.
pub fn phase_3_run_analysis_passes(sess: Session,
crate: @ast::Crate) -> CrateAnalysis {
crate: &ast::Crate) -> CrateAnalysis {

let time_passes = sess.time_passes();

let ast_map = time(time_passes, ~"ast indexing", ||
let ast_map = time(time_passes, ~"ast indexing", (), |_|
syntax::ast_map::map_crate(sess.diagnostic(), crate));

time(time_passes, ~"external crate/lib resolution", ||
time(time_passes, ~"external crate/lib resolution", (), |_|
creader::read_crates(sess.diagnostic(), crate, sess.cstore,
sess.filesearch,
session::sess_os_to_meta_os(sess.targ_cfg.os),
sess.opts.is_static,
token::get_ident_interner()));

let lang_items = time(time_passes, ~"language item collection", ||
let lang_items = time(time_passes, ~"language item collection", (), |_|
middle::lang_items::collect_language_items(crate, sess));

let middle::resolve::CrateMap {
def_map: def_map,
exp_map2: exp_map2,
trait_map: trait_map
} =
time(time_passes, ~"resolution", ||
time(time_passes, ~"resolution", (), |_|
middle::resolve::resolve_crate(sess, lang_items, crate));

time(time_passes, ~"looking for entry point",
|| middle::entry::find_entry_point(sess, crate, ast_map));
time(time_passes, ~"looking for entry point", (),
|_| middle::entry::find_entry_point(sess, crate, ast_map));

let freevars = time(time_passes, ~"freevar finding", ||
let freevars = time(time_passes, ~"freevar finding", (), |_|
freevars::annotate_freevars(def_map, crate));

let region_map = time(time_passes, ~"region resolution", ||
let region_map = time(time_passes, ~"region resolution", (), |_|
middle::region::resolve_crate(sess, def_map, crate));

let rp_set = time(time_passes, ~"region parameterization inference", ||
let rp_set = time(time_passes, ~"region parameterization inference", (), |_|
middle::region::determine_rp_in_crate(sess, ast_map, def_map, crate));

let ty_cx = ty::mk_ctxt(sess, def_map, ast_map, freevars,
Expand All @@ -252,53 +252,53 @@ pub fn phase_3_run_analysis_passes(sess: Session,
ty_cx, trait_map, crate);

// These next two const passes can probably be merged
time(time_passes, ~"const marking", ||
time(time_passes, ~"const marking", (), |_|
middle::const_eval::process_crate(crate, ty_cx));

time(time_passes, ~"const checking", ||
time(time_passes, ~"const checking", (), |_|
middle::check_const::check_crate(sess, crate, ast_map, def_map,
method_map, ty_cx));

let exported_items =
time(time_passes, ~"privacy checking", ||
time(time_passes, ~"privacy checking", (), |_|
middle::privacy::check_crate(ty_cx, &method_map, &exp_map2, crate));

time(time_passes, ~"effect checking", ||
time(time_passes, ~"effect checking", (), |_|
middle::effect::check_crate(ty_cx, method_map, crate));

time(time_passes, ~"loop checking", ||
time(time_passes, ~"loop checking", (), |_|
middle::check_loop::check_crate(ty_cx, crate));

time(time_passes, ~"stack checking", ||
time(time_passes, ~"stack checking", (), |_|
middle::stack_check::stack_check_crate(ty_cx, crate));

let middle::moves::MoveMaps {moves_map, moved_variables_set,
capture_map} =
time(time_passes, ~"compute moves", ||
time(time_passes, ~"compute moves", (), |_|
middle::moves::compute_moves(ty_cx, method_map, crate));

time(time_passes, ~"match checking", ||
time(time_passes, ~"match checking", (), |_|
middle::check_match::check_crate(ty_cx, method_map,
moves_map, crate));

time(time_passes, ~"liveness checking", ||
time(time_passes, ~"liveness checking", (), |_|
middle::liveness::check_crate(ty_cx, method_map,
capture_map, crate));

let (root_map, write_guard_map) =
time(time_passes, ~"borrow checking", ||
time(time_passes, ~"borrow checking", (), |_|
middle::borrowck::check_crate(ty_cx, method_map,
moves_map, moved_variables_set,
capture_map, crate));

time(time_passes, ~"kind checking", ||
time(time_passes, ~"kind checking", (), |_|
kind::check_crate(ty_cx, method_map, crate));

let reachable_map =
time(time_passes, ~"reachability checking", ||
time(time_passes, ~"reachability checking", (), |_|
reachable::find_reachable(ty_cx, method_map, crate));

time(time_passes, ~"lint checking", ||
time(time_passes, ~"lint checking", (), |_|
lint::check_crate(ty_cx, crate));

CrateAnalysis {
Expand All @@ -325,10 +325,10 @@ pub struct CrateTranslation {
/// Run the translation phase to LLVM, after which the AST and analysis can
/// be discarded.
pub fn phase_4_translate_to_llvm(sess: Session,
crate: @ast::Crate,
crate: ast::Crate,
analysis: &CrateAnalysis,
outputs: &OutputFilenames) -> CrateTranslation {
time(sess.time_passes(), ~"translation", ||
time(sess.time_passes(), ~"translation", crate, |crate|
trans::base::trans_crate(sess, crate, analysis,
&outputs.obj_filename))
}
Expand All @@ -349,7 +349,7 @@ pub fn phase_5_run_llvm_passes(sess: Session,
let output_type = link::output_type_assembly;
let asm_filename = outputs.obj_filename.with_filetype("s");

time(sess.time_passes(), ~"LLVM passes", ||
time(sess.time_passes(), ~"LLVM passes", (), |_|
link::write::run_passes(sess,
trans.context,
trans.module,
Expand All @@ -363,7 +363,7 @@ pub fn phase_5_run_llvm_passes(sess: Session,
os::remove_file(&asm_filename);
}
} else {
time(sess.time_passes(), ~"LLVM passes", ||
time(sess.time_passes(), ~"LLVM passes", (), |_|
link::write::run_passes(sess,
trans.context,
trans.module,
Expand All @@ -377,7 +377,7 @@ pub fn phase_5_run_llvm_passes(sess: Session,
pub fn phase_6_link_output(sess: Session,
trans: &CrateTranslation,
outputs: &OutputFilenames) {
time(sess.time_passes(), ~"linking", ||
time(sess.time_passes(), ~"linking", (), |_|
link::link_binary(sess,
&outputs.obj_filename,
&outputs.out_filename,
Expand Down Expand Up @@ -430,7 +430,7 @@ pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &input,
if stop_after_phase_1(sess) { return; }
phase_2_configure_and_expand(sess, cfg, crate)
};
let analysis = phase_3_run_analysis_passes(sess, expanded_crate);
let analysis = phase_3_run_analysis_passes(sess, &expanded_crate);
if stop_after_phase_3(sess) { return; }
let outputs = build_output_filenames(input, outdir, output, [], sess);
let trans = phase_4_translate_to_llvm(sess, expanded_crate,
Expand Down Expand Up @@ -535,7 +535,7 @@ pub fn pretty_print_input(sess: Session,
} as @pprust::pp_ann
}
PpmTyped => {
let analysis = phase_3_run_analysis_passes(sess, crate);
let analysis = phase_3_run_analysis_passes(sess, &crate);
@TypedAnnotation {
analysis: analysis
} as @pprust::pp_ann
Expand All @@ -548,7 +548,7 @@ pub fn pretty_print_input(sess: Session,
pprust::print_crate(sess.codemap,
token::get_ident_interner(),
sess.span_diagnostic,
crate,
&crate,
source_name(input),
rdr,
io::stdout(),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/front/assign_node_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ impl ast_fold for NodeIdAssigner {
}
}

pub fn assign_node_ids(sess: Session, crate: @ast::Crate) -> @ast::Crate {
pub fn assign_node_ids(sess: Session, crate: ast::Crate) -> ast::Crate {
let fold = NodeIdAssigner {
sess: sess,
};
@fold.fold_crate(crate)
fold.fold_crate(crate)
}
11 changes: 6 additions & 5 deletions src/librustc/front/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ struct Context<'self> {

// Support conditional compilation by transforming the AST, stripping out
// any items that do not belong in the current configuration
pub fn strip_unconfigured_items(crate: @ast::Crate) -> @ast::Crate {
pub fn strip_unconfigured_items(crate: ast::Crate) -> ast::Crate {
let config = crate.config.clone();
do strip_items(crate) |attrs| {
in_cfg(crate.config, attrs)
in_cfg(config, attrs)
}
}

Expand All @@ -40,13 +41,13 @@ impl<'self> fold::ast_fold for Context<'self> {
}
}

pub fn strip_items(crate: &ast::Crate,
pub fn strip_items(crate: ast::Crate,
in_cfg: &fn(attrs: &[ast::Attribute]) -> bool)
-> @ast::Crate {
-> ast::Crate {
let ctxt = Context {
in_cfg: in_cfg,
};
@ctxt.fold_crate(crate)
ctxt.fold_crate(crate)
}

fn filter_item(cx: &Context, item: @ast::item) -> Option<@ast::item> {
Expand Down
15 changes: 7 additions & 8 deletions src/librustc/front/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use syntax::opt_vec;

static STD_VERSION: &'static str = "0.9-pre";

pub fn maybe_inject_libstd_ref(sess: Session, crate: @ast::Crate)
-> @ast::Crate {
if use_std(crate) {
pub fn maybe_inject_libstd_ref(sess: Session, crate: ast::Crate)
-> ast::Crate {
if use_std(&crate) {
inject_libstd_ref(sess, crate)
} else {
crate
Expand All @@ -51,7 +51,7 @@ struct StandardLibraryInjector {
}

impl fold::ast_fold for StandardLibraryInjector {
fn fold_crate(&self, crate: &ast::Crate) -> ast::Crate {
fn fold_crate(&self, crate: ast::Crate) -> ast::Crate {
let version = STD_VERSION.to_managed();
let vi1 = ast::view_item {
node: ast::view_item_extern_mod(self.sess.ident_of("std"),
Expand All @@ -77,10 +77,9 @@ impl fold::ast_fold for StandardLibraryInjector {
new_module = self.fold_mod(&new_module);
}

// FIXME #2543: Bad copy.
ast::Crate {
module: new_module,
..(*crate).clone()
..crate
}
}

Expand Down Expand Up @@ -133,9 +132,9 @@ impl fold::ast_fold for StandardLibraryInjector {
}
}

fn inject_libstd_ref(sess: Session, crate: &ast::Crate) -> @ast::Crate {
fn inject_libstd_ref(sess: Session, crate: ast::Crate) -> ast::Crate {
let fold = StandardLibraryInjector {
sess: sess,
};
@fold.fold_crate(crate)
fold.fold_crate(crate)
}
Loading

0 comments on commit a7cfbfb

Please sign in to comment.