Skip to content

Commit 5b8f284

Browse files
committed
Auto merge of rust-lang#107643 - Zoxc:single-cache, r=cjgillot
Create a single value cache for the () query key Since queries using `()` as the key can only store a single value, specialize for that case. This looks like a minor performance improvement: <table><tr><td rowspan="2">Benchmark</td><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th></tr><tr><td align="right">Time</td><td align="right">Time</td><td align="right">%</th></tr><tr><td>🟣 <b>clap</b>:check</td><td align="right">1.8477s</td><td align="right">1.8415s</td><td align="right"> -0.33%</td></tr><tr><td>🟣 <b>hyper</b>:check</td><td align="right">0.2666s</td><td align="right">0.2655s</td><td align="right"> -0.40%</td></tr><tr><td>🟣 <b>syntex_syntax</b>:check</td><td align="right">6.3943s</td><td align="right">6.3686s</td><td align="right"> -0.40%</td></tr><tr><td>🟣 <b>syn</b>:check</td><td align="right">1.6413s</td><td align="right">1.6345s</td><td align="right"> -0.42%</td></tr><tr><td>🟣 <b>regex</b>:check</td><td align="right">1.0337s</td><td align="right">1.0313s</td><td align="right"> -0.24%</td></tr><tr><td>Total</td><td align="right">11.1836s</td><td align="right">11.1414s</td><td align="right"> -0.38%</td></tr><tr><td>Summary</td><td align="right">1.0000s</td><td align="right">0.9964s</td><td align="right"> -0.36%</td></tr></table>
2 parents 00cf19a + 80d2652 commit 5b8f284

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

compiler/rustc_middle/src/query/keys.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::ty::subst::{GenericArg, SubstsRef};
88
use crate::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
99
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
1010
use rustc_hir::hir_id::{HirId, OwnerId};
11-
use rustc_query_system::query::{DefaultCacheSelector, VecCacheSelector};
11+
use rustc_query_system::query::{DefaultCacheSelector, SingleCacheSelector, VecCacheSelector};
1212
use rustc_span::symbol::{Ident, Symbol};
1313
use rustc_span::{Span, DUMMY_SP};
1414

@@ -45,7 +45,7 @@ pub trait Key: Sized {
4545
}
4646

4747
impl Key for () {
48-
type CacheSelector = DefaultCacheSelector<Self>;
48+
type CacheSelector = SingleCacheSelector;
4949

5050
#[inline(always)]
5151
fn query_crate_is_local(&self) -> bool {

compiler/rustc_query_system/src/query/caches.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_data_structures::fx::FxHashMap;
55
use rustc_data_structures::sharded;
66
#[cfg(parallel_compiler)]
77
use rustc_data_structures::sharded::Sharded;
8-
#[cfg(not(parallel_compiler))]
98
use rustc_data_structures::sync::Lock;
109
use rustc_data_structures::sync::WorkerLocal;
1110
use rustc_index::vec::{Idx, IndexVec};
@@ -117,6 +116,52 @@ where
117116
}
118117
}
119118

119+
pub struct SingleCacheSelector;
120+
121+
impl<'tcx, V: 'tcx> CacheSelector<'tcx, V> for SingleCacheSelector {
122+
type Cache = SingleCache<V>
123+
where
124+
V: Copy;
125+
type ArenaCache = ArenaCache<'tcx, (), V>;
126+
}
127+
128+
pub struct SingleCache<V> {
129+
cache: Lock<Option<(V, DepNodeIndex)>>,
130+
}
131+
132+
impl<V> Default for SingleCache<V> {
133+
fn default() -> Self {
134+
SingleCache { cache: Lock::new(None) }
135+
}
136+
}
137+
138+
impl<V: Copy + Debug> QueryStorage for SingleCache<V> {
139+
type Value = V;
140+
type Stored = V;
141+
}
142+
143+
impl<V> QueryCache for SingleCache<V>
144+
where
145+
V: Copy + Debug,
146+
{
147+
type Key = ();
148+
149+
#[inline(always)]
150+
fn lookup(&self, _key: &()) -> Option<(V, DepNodeIndex)> {
151+
*self.cache.lock()
152+
}
153+
154+
#[inline]
155+
fn complete(&self, _key: (), value: V, index: DepNodeIndex) -> Self::Stored {
156+
*self.cache.lock() = Some((value.clone(), index));
157+
value
158+
}
159+
160+
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
161+
self.cache.lock().as_ref().map(|value| f(&(), &value.0, value.1));
162+
}
163+
}
164+
120165
pub struct ArenaCache<'tcx, K, V> {
121166
arena: WorkerLocal<TypedArena<(V, DepNodeIndex)>>,
122167
#[cfg(parallel_compiler)]

compiler/rustc_query_system/src/query/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ pub use self::job::{print_query_stack, QueryInfo, QueryJob, QueryJobId, QueryJob
88

99
mod caches;
1010
pub use self::caches::{
11-
CacheSelector, DefaultCacheSelector, QueryCache, QueryStorage, VecCacheSelector,
11+
CacheSelector, DefaultCacheSelector, QueryCache, QueryStorage, SingleCacheSelector,
12+
VecCacheSelector,
1213
};
1314

1415
mod config;

0 commit comments

Comments
 (0)