Skip to content

Commit d00ed01

Browse files
committed
Only create OnDiskCache in incremental compilation mode
This lets us skip doing useless work when we're not in incremental compilation mode.
1 parent fe98231 commit d00ed01

File tree

6 files changed

+33
-17
lines changed

6 files changed

+33
-17
lines changed

Diff for: compiler/rustc_incremental/src/persist/load.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,14 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
199199
}))
200200
}
201201

202-
pub fn load_query_result_cache(sess: &Session) -> OnDiskCache<'_> {
202+
/// Attempts to load the query result cache from disk
203+
///
204+
/// If we are not in incremental compilation mode, returns `None`.
205+
/// Otherwise, tries to load the query result cache from disk,
206+
/// creating an empty cache if it could not be loaded.
207+
pub fn load_query_result_cache(sess: &Session) -> Option<OnDiskCache<'_>> {
203208
if sess.opts.incremental.is_none() {
204-
return OnDiskCache::new_empty(sess.source_map());
209+
return None;
205210
}
206211

207212
let _prof_timer = sess.prof.generic_activity("incr_comp_load_query_result_cache");
@@ -211,7 +216,9 @@ pub fn load_query_result_cache(sess: &Session) -> OnDiskCache<'_> {
211216
&query_cache_path(sess),
212217
sess.is_nightly_build(),
213218
) {
214-
LoadResult::Ok { data: (bytes, start_pos) } => OnDiskCache::new(sess, bytes, start_pos),
215-
_ => OnDiskCache::new_empty(sess.source_map()),
219+
LoadResult::Ok { data: (bytes, start_pos) } => {
220+
Some(OnDiskCache::new(sess, bytes, start_pos))
221+
}
222+
_ => Some(OnDiskCache::new_empty(sess.source_map())),
216223
}
217224
}

Diff for: compiler/rustc_macros/src/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ fn add_query_description_impl(
353353
tcx: TyCtxt<'tcx>,
354354
id: SerializedDepNodeIndex
355355
) -> Option<Self::Value> {
356-
tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
356+
tcx.queries.on_disk_cache.as_ref().and_then(|c| c.try_load_query_result(tcx, id))
357357
}
358358
}
359359
};

Diff for: compiler/rustc_middle/src/dep_graph/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,27 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
164164
}
165165

166166
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
167-
self.queries.on_disk_cache.load_diagnostics(*self, prev_dep_node_index)
167+
self.queries
168+
.on_disk_cache
169+
.as_ref()
170+
.map(|c| c.load_diagnostics(*self, prev_dep_node_index))
171+
.unwrap_or_default()
168172
}
169173

170174
fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
171-
self.queries.on_disk_cache.store_diagnostics(dep_node_index, diagnostics)
175+
if let Some(c) = self.queries.on_disk_cache.as_ref() {
176+
c.store_diagnostics(dep_node_index, diagnostics)
177+
}
172178
}
173179

174180
fn store_diagnostics_for_anon_node(
175181
&self,
176182
dep_node_index: DepNodeIndex,
177183
diagnostics: ThinVec<Diagnostic>,
178184
) {
179-
self.queries.on_disk_cache.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
185+
if let Some(c) = self.queries.on_disk_cache.as_ref() {
186+
c.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
187+
}
180188
}
181189

182190
fn profiler(&self) -> &SelfProfilerRef {

Diff for: compiler/rustc_middle/src/query/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ rustc_queries! {
130130
storage(ArenaCacheSelector<'tcx>)
131131
cache_on_disk_if { key.is_local() }
132132
load_cached(tcx, id) {
133-
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache
134-
.try_load_query_result(tcx, id);
133+
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache.as_ref()
134+
.and_then(|c| c.try_load_query_result(tcx, id));
135135
generics
136136
}
137137
}
@@ -688,8 +688,8 @@ rustc_queries! {
688688
cache_on_disk_if { true }
689689
load_cached(tcx, id) {
690690
let typeck_results: Option<ty::TypeckResults<'tcx>> = tcx
691-
.queries.on_disk_cache
692-
.try_load_query_result(tcx, id);
691+
.queries.on_disk_cache.as_ref()
692+
.and_then(|c| c.try_load_query_result(tcx, id));
693693

694694
typeck_results.map(|x| &*tcx.arena.alloc(x))
695695
}

Diff for: compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ impl<'tcx> TyCtxt<'tcx> {
10941094
krate: &'tcx hir::Crate<'tcx>,
10951095
definitions: &'tcx Definitions,
10961096
dep_graph: DepGraph,
1097-
on_disk_query_result_cache: query::OnDiskCache<'tcx>,
1097+
on_disk_query_result_cache: Option<query::OnDiskCache<'tcx>>,
10981098
crate_name: &str,
10991099
output_filenames: &OutputFilenames,
11001100
) -> GlobalCtxt<'tcx> {
@@ -1343,7 +1343,7 @@ impl<'tcx> TyCtxt<'tcx> {
13431343
where
13441344
E: ty::codec::OpaqueEncoder,
13451345
{
1346-
self.queries.on_disk_cache.serialize(self, encoder)
1346+
self.queries.on_disk_cache.as_ref().map(|c| c.serialize(self, encoder)).unwrap_or(Ok(()))
13471347
}
13481348

13491349
/// If `true`, we should use the MIR-based borrowck, but also

Diff for: compiler/rustc_middle/src/ty/query/plumbing.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,11 @@ macro_rules! define_queries_struct {
507507
(tcx: $tcx:tt,
508508
input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
509509
pub struct Queries<$tcx> {
510-
/// This provides access to the incrimental comilation on-disk cache for query results.
510+
/// This provides access to the incremental comilation on-disk cache for query results.
511511
/// Do not access this directly. It is only meant to be used by
512512
/// `DepGraph::try_mark_green()` and the query infrastructure.
513-
pub(crate) on_disk_cache: OnDiskCache<'tcx>,
513+
/// This is `None` if we are not incremental compilation mode
514+
pub(crate) on_disk_cache: Option<OnDiskCache<'tcx>>,
514515

515516
providers: IndexVec<CrateNum, Providers>,
516517
fallback_extern_providers: Box<Providers>,
@@ -526,7 +527,7 @@ macro_rules! define_queries_struct {
526527
pub(crate) fn new(
527528
providers: IndexVec<CrateNum, Providers>,
528529
fallback_extern_providers: Providers,
529-
on_disk_cache: OnDiskCache<'tcx>,
530+
on_disk_cache: Option<OnDiskCache<'tcx>>,
530531
) -> Self {
531532
Queries {
532533
providers,

0 commit comments

Comments
 (0)