-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a query cache for dense local DefIds #69303
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,12 @@ use crate::ty::TyCtxt; | |
|
||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_data_structures::sharded::Sharded; | ||
use rustc_hir::def_id::{DefId, DefIndex, LOCAL_CRATE}; | ||
use rustc_index::vec::IndexVec; | ||
use std::cell::RefCell; | ||
use std::default::Default; | ||
use std::hash::Hash; | ||
use std::marker::PhantomData; | ||
|
||
pub(crate) trait CacheSelector<K, V> { | ||
type Cache: QueryCache<K, V>; | ||
|
@@ -54,13 +58,18 @@ pub(crate) trait QueryCache<K, V>: Default { | |
pub struct DefaultCacheSelector; | ||
|
||
impl<K: Eq + Hash, V: Clone> CacheSelector<K, V> for DefaultCacheSelector { | ||
type Cache = DefaultCache; | ||
type Cache = DefaultCache<()>; | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct DefaultCache; | ||
pub struct DefaultCache<D>(PhantomData<D>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this generic parameter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoids compiler errors for the |
||
|
||
impl<K: Eq + Hash, V: Clone> QueryCache<K, V> for DefaultCache { | ||
impl<D> Default for DefaultCache<D> { | ||
fn default() -> Self { | ||
DefaultCache(PhantomData) | ||
} | ||
} | ||
|
||
impl<D, K: Eq + Hash, V: Clone> QueryCache<K, V> for DefaultCache<D> { | ||
type Sharded = FxHashMap<K, (V, DepNodeIndex)>; | ||
|
||
#[inline(always)] | ||
|
@@ -110,3 +119,88 @@ impl<K: Eq + Hash, V: Clone> QueryCache<K, V> for DefaultCache { | |
f(Box::new(results)) | ||
} | ||
} | ||
|
||
#[cfg(parallel_compiler)] | ||
pub type LocalDenseDefIdCacheSelector<V> = DefaultCache<V>; | ||
#[cfg(not(parallel_compiler))] | ||
pub type LocalDenseDefIdCacheSelector<V> = LocalDenseDefIdCache<V>; | ||
|
||
pub struct LocalDenseDefIdCache<V> { | ||
local: RefCell<IndexVec<DefIndex, Option<(V, DepNodeIndex)>>>, | ||
other: DefaultCache<()>, | ||
} | ||
|
||
impl<V> Default for LocalDenseDefIdCache<V> { | ||
fn default() -> Self { | ||
LocalDenseDefIdCache { local: RefCell::new(IndexVec::new()), other: Default::default() } | ||
} | ||
} | ||
|
||
impl<V: Clone> QueryCache<DefId, V> for LocalDenseDefIdCache<V> { | ||
type Sharded = <DefaultCache<()> as QueryCache<DefId, V>>::Sharded; | ||
|
||
#[inline(always)] | ||
fn lookup<'tcx, R, GetCache, OnHit, OnMiss, Q>( | ||
&self, | ||
state: &'tcx QueryState<'tcx, Q>, | ||
get_cache: GetCache, | ||
key: DefId, | ||
on_hit: OnHit, | ||
on_miss: OnMiss, | ||
) -> R | ||
where | ||
Q: QueryAccessors<'tcx>, | ||
GetCache: for<'a> Fn(&'a mut QueryStateShard<'tcx, Q>) -> &'a mut Self::Sharded, | ||
OnHit: FnOnce(&V, DepNodeIndex) -> R, | ||
OnMiss: FnOnce(DefId, QueryLookup<'tcx, Q>) -> R, | ||
{ | ||
if key.krate == LOCAL_CRATE { | ||
let local = self.local.borrow(); | ||
if let Some(result) = local.get(key.index).and_then(|v| v.as_ref()) { | ||
on_hit(&result.0, result.1) | ||
} else { | ||
drop(local); | ||
let lookup = state.get_lookup(&key); | ||
on_miss(key, lookup) | ||
} | ||
} else { | ||
self.other.lookup(state, get_cache, key, on_hit, on_miss) | ||
} | ||
} | ||
|
||
#[inline] | ||
fn complete( | ||
&self, | ||
tcx: TyCtxt<'tcx>, | ||
lock_sharded_storage: &mut Self::Sharded, | ||
key: DefId, | ||
value: V, | ||
index: DepNodeIndex, | ||
) { | ||
if key.krate == LOCAL_CRATE { | ||
let mut local = self.local.borrow_mut(); | ||
if local.raw.capacity() == 0 { | ||
*local = IndexVec::from_elem_n(None, tcx.hir().definitions().def_index_count()); | ||
} | ||
local[key.index] = Some((value, index)); | ||
} else { | ||
self.other.complete(tcx, lock_sharded_storage, key, value, index); | ||
} | ||
} | ||
|
||
fn iter<R, L>( | ||
&self, | ||
shards: &Sharded<L>, | ||
get_shard: impl Fn(&mut L) -> &mut Self::Sharded, | ||
f: impl for<'a> FnOnce(Box<dyn Iterator<Item = (&'a DefId, &'a V, DepNodeIndex)> + 'a>) -> R, | ||
) -> R { | ||
let local = self.local.borrow(); | ||
let local: Vec<(DefId, &V, DepNodeIndex)> = local | ||
.iter_enumerated() | ||
.filter_map(|(i, e)| e.as_ref().map(|e| (DefId::local(i), &e.0, e.1))) | ||
.collect(); | ||
self.other.iter(shards, get_shard, |results| { | ||
f(Box::new(results.chain(local.iter().map(|(id, v, i)| (id, *v, *i))))) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change the query keys to
LocalDefId
, then you won't have anyis_local
checks, maybe that helps?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are
LocalDefId
inmaster
now. I don't expect removing a single branch to help much though.