Skip to content

Commit 4f3baa4

Browse files
committed
Drop AST on a separate thread and prefetch hir_crate
1 parent 6db96de commit 4f3baa4

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use rustc_data_structures::captures::Captures;
5050
use rustc_data_structures::fingerprint::Fingerprint;
5151
use rustc_data_structures::sorted_map::SortedMap;
5252
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
53-
use rustc_data_structures::sync::Lrc;
53+
use rustc_data_structures::sync::{spawn, Lrc};
5454
use rustc_errors::{DiagArgFromDisplay, DiagCtxt, StashKey};
5555
use rustc_hir as hir;
5656
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
@@ -445,9 +445,14 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
445445
.lower_node(def_id);
446446
}
447447

448-
// Drop AST to free memory
449448
drop(ast_index);
450-
sess.time("drop_ast", || drop(krate));
449+
450+
// Drop AST to free memory. It can be expensive so try to drop it on a separate thread.
451+
let prof = sess.prof.clone();
452+
spawn(move || {
453+
let _timer = prof.verbose_generic_activity("drop_ast");
454+
drop(krate);
455+
});
451456

452457
// Don't hash unless necessary, because it's expensive.
453458
let opt_hir_hash =

compiler/rustc_data_structures/src/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub use worker_local::{Registry, WorkerLocal};
5353
mod parallel;
5454
#[cfg(parallel_compiler)]
5555
pub use parallel::scope;
56-
pub use parallel::{join, par_for_each_in, par_map, parallel_guard, try_par_for_each_in};
56+
pub use parallel::{join, par_for_each_in, par_map, parallel_guard, spawn, try_par_for_each_in};
5757

5858
pub use vec::{AppendOnlyIndexVec, AppendOnlyVec};
5959

compiler/rustc_data_structures/src/sync/parallel.rs

+15
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ mod disabled {
6161
}}
6262
}
6363

64+
pub fn spawn(func: impl FnOnce() + 'static) {
65+
func()
66+
}
67+
6468
pub fn join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
6569
where
6670
A: FnOnce() -> RA,
@@ -138,6 +142,17 @@ mod enabled {
138142
};
139143
}
140144

145+
pub fn spawn(func: impl FnOnce() + DynSend + 'static) {
146+
if mode::is_dyn_thread_safe() {
147+
let func = FromDyn::from(func);
148+
rayon::spawn(|| {
149+
(func.into_inner())();
150+
});
151+
} else {
152+
func()
153+
}
154+
}
155+
141156
// This function only works when `mode::is_dyn_thread_safe()`.
142157
pub fn scope<'scope, OP, R>(op: OP) -> R
143158
where

compiler/rustc_interface/src/passes.rs

+3
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ pub fn create_global_ctxt<'tcx>(
688688
fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
689689
rustc_passes::hir_id_validator::check_crate(tcx);
690690

691+
// Prefetch this to prevent multiple threads from blocking on it later.
692+
tcx.ensure_with_value().hir_crate(());
693+
691694
let sess = tcx.sess;
692695

693696
sess.time("misc_checking_1", || {

0 commit comments

Comments
 (0)