Skip to content

Commit f778c52

Browse files
committed
Drop AST on a separate thread and prefetch hir_crate
1 parent ae9173d commit f778c52

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use rustc_attr_parsing::{AttributeParser, OmitDoc};
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::spawn;
5354
use rustc_data_structures::tagged_ptr::TaggedRef;
5455
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5556
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
@@ -455,9 +456,14 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
455456
.lower_node(def_id);
456457
}
457458

458-
// Drop AST to free memory
459459
drop(ast_index);
460-
sess.time("drop_ast", || drop(krate));
460+
461+
// Drop AST to free memory. It can be expensive so try to drop it on a separate thread.
462+
let prof = sess.prof.clone();
463+
spawn(move || {
464+
let _timer = prof.verbose_generic_activity("drop_ast");
465+
drop(krate);
466+
});
461467

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

compiler/rustc_data_structures/src/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub use self::freeze::{FreezeLock, FreezeReadGuard, FreezeWriteGuard};
4343
pub use self::lock::{Lock, LockGuard, Mode};
4444
pub use self::mode::{is_dyn_thread_safe, set_dyn_thread_safe_mode};
4545
pub use self::parallel::{
46-
join, par_for_each_in, par_map, parallel_guard, scope, try_par_for_each_in,
46+
join, par_for_each_in, par_map, parallel_guard, scope, spawn, try_par_for_each_in,
4747
};
4848
pub use self::vec::{AppendOnlyIndexVec, AppendOnlyVec};
4949
pub use self::worker_local::{Registry, WorkerLocal};

compiler/rustc_data_structures/src/sync/parallel.rs

+11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ macro_rules! parallel {
9494
};
9595
}
9696

97+
pub fn spawn(func: impl FnOnce() + DynSend + 'static) {
98+
if mode::is_dyn_thread_safe() {
99+
let func = FromDyn::from(func);
100+
rayon::spawn(|| {
101+
(func.into_inner())();
102+
});
103+
} else {
104+
func()
105+
}
106+
}
107+
97108
// This function only works when `mode::is_dyn_thread_safe()`.
98109
pub fn scope<'scope, OP, R>(op: OP) -> R
99110
where

compiler/rustc_interface/src/passes.rs

+6
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,12 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
899899
// is not defined. So we need to cfg it out.
900900
#[cfg(all(not(doc), debug_assertions))]
901901
rustc_passes::hir_id_validator::check_crate(tcx);
902+
903+
// Prefetch this to prevent multiple threads from blocking on it later.
904+
// This is needed since the `hir_id_validator::check_crate` call above is not guaranteed
905+
// to use `hir_crate`.
906+
tcx.ensure_done().hir_crate(());
907+
902908
let sess = tcx.sess;
903909
sess.time("misc_checking_1", || {
904910
parallel!(

0 commit comments

Comments
 (0)