Skip to content

Commit 075560a

Browse files
committed
Free intermediate translation results as soon as possible
This fixes the recently introduced peak memory usage regression by freeing the intermediate results as soon as they're not required anymore instead of keeping them around for the whole compilation process. Refs rust-lang#8077
1 parent 293ec2c commit 075560a

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Diff for: src/librustc/driver/driver.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,19 @@ pub fn stop_after_phase_5(sess: Session) -> bool {
409409
pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &input,
410410
outdir: &Option<Path>, output: &Option<Path>) {
411411
let outputs = build_output_filenames(input, outdir, output, [], sess);
412-
let crate = phase_1_parse_input(sess, cfg.clone(), input);
413-
if stop_after_phase_1(sess) { return; }
414-
let expanded_crate = phase_2_configure_and_expand(sess, cfg, crate);
415-
let analysis = phase_3_run_analysis_passes(sess, expanded_crate);
416-
if stop_after_phase_3(sess) { return; }
417-
let trans = phase_4_translate_to_llvm(sess, expanded_crate, &analysis, outputs);
412+
// We need nested scopes here, because the intermediate results can keep
413+
// large chunks of memory alive and we want to free them as soon as
414+
// possible to keep the peak memory usage low
415+
let trans = {
416+
let expanded_crate = {
417+
let crate = phase_1_parse_input(sess, cfg.clone(), input);
418+
if stop_after_phase_1(sess) { return; }
419+
phase_2_configure_and_expand(sess, cfg, crate)
420+
};
421+
let analysis = phase_3_run_analysis_passes(sess, expanded_crate);
422+
if stop_after_phase_3(sess) { return; }
423+
phase_4_translate_to_llvm(sess, expanded_crate, &analysis, outputs)
424+
};
418425
phase_5_run_llvm_passes(sess, &trans, outputs);
419426
if stop_after_phase_5(sess) { return; }
420427
phase_6_link_output(sess, &trans, outputs);

0 commit comments

Comments
 (0)