Skip to content

Commit

Permalink
Auto merge of #107643 - Zoxc:single-cache, r=cjgillot
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors committed Feb 12, 2023
2 parents 00cf19a + 80d2652 commit 5b8f284
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/query/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::ty::subst::{GenericArg, SubstsRef};
use crate::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::hir_id::{HirId, OwnerId};
use rustc_query_system::query::{DefaultCacheSelector, VecCacheSelector};
use rustc_query_system::query::{DefaultCacheSelector, SingleCacheSelector, VecCacheSelector};
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};

Expand Down Expand Up @@ -45,7 +45,7 @@ pub trait Key: Sized {
}

impl Key for () {
type CacheSelector = DefaultCacheSelector<Self>;
type CacheSelector = SingleCacheSelector;

#[inline(always)]
fn query_crate_is_local(&self) -> bool {
Expand Down
47 changes: 46 additions & 1 deletion compiler/rustc_query_system/src/query/caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sharded;
#[cfg(parallel_compiler)]
use rustc_data_structures::sharded::Sharded;
#[cfg(not(parallel_compiler))]
use rustc_data_structures::sync::Lock;
use rustc_data_structures::sync::WorkerLocal;
use rustc_index::vec::{Idx, IndexVec};
Expand Down Expand Up @@ -117,6 +116,52 @@ where
}
}

pub struct SingleCacheSelector;

impl<'tcx, V: 'tcx> CacheSelector<'tcx, V> for SingleCacheSelector {
type Cache = SingleCache<V>
where
V: Copy;
type ArenaCache = ArenaCache<'tcx, (), V>;
}

pub struct SingleCache<V> {
cache: Lock<Option<(V, DepNodeIndex)>>,
}

impl<V> Default for SingleCache<V> {
fn default() -> Self {
SingleCache { cache: Lock::new(None) }
}
}

impl<V: Copy + Debug> QueryStorage for SingleCache<V> {
type Value = V;
type Stored = V;
}

impl<V> QueryCache for SingleCache<V>
where
V: Copy + Debug,
{
type Key = ();

#[inline(always)]
fn lookup(&self, _key: &()) -> Option<(V, DepNodeIndex)> {
*self.cache.lock()
}

#[inline]
fn complete(&self, _key: (), value: V, index: DepNodeIndex) -> Self::Stored {
*self.cache.lock() = Some((value.clone(), index));
value
}

fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
self.cache.lock().as_ref().map(|value| f(&(), &value.0, value.1));
}
}

pub struct ArenaCache<'tcx, K, V> {
arena: WorkerLocal<TypedArena<(V, DepNodeIndex)>>,
#[cfg(parallel_compiler)]
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_query_system/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub use self::job::{print_query_stack, QueryInfo, QueryJob, QueryJobId, QueryJob

mod caches;
pub use self::caches::{
CacheSelector, DefaultCacheSelector, QueryCache, QueryStorage, VecCacheSelector,
CacheSelector, DefaultCacheSelector, QueryCache, QueryStorage, SingleCacheSelector,
VecCacheSelector,
};

mod config;
Expand Down

0 comments on commit 5b8f284

Please sign in to comment.