Skip to content

Remove sharding for VecCache #123556

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

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions compiler/rustc_query_system/src/query/caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where
}

pub struct VecCache<K: Idx, V> {
cache: Sharded<IndexVec<K, Option<(V, DepNodeIndex)>>>,
cache: Lock<IndexVec<K, Option<(V, DepNodeIndex)>>>,
}

impl<K: Idx, V> Default for VecCache<K, V> {
Expand All @@ -120,24 +120,20 @@ where

#[inline(always)]
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
// FIXME: lock_shard_by_hash will use high bits which are usually zero in the index() passed
// here. This makes sharding essentially useless, always selecting the zero'th shard.
let lock = self.cache.lock_shard_by_hash(key.index() as u64);
let lock = self.cache.lock();
if let Some(Some(value)) = lock.get(*key) { Some(*value) } else { None }
Comment on lines -125 to 124
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IndexVec is contiguous, right? Did the old implementation make much sense? From a quick glance, it seems to me that if the indices were in fact high enough to shard we'd end up with largely empty IndexVecs in each shard

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sounds plausible. In any case IIRC the sharding uses the high bits and we'd almost certainly run out of memory first.

}

#[inline]
fn complete(&self, key: K, value: V, index: DepNodeIndex) {
let mut lock = self.cache.lock_shard_by_hash(key.index() as u64);
let mut lock = self.cache.lock();
lock.insert(key, (value, index));
}

fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
for shard in self.cache.lock_shards() {
for (k, v) in shard.iter_enumerated() {
if let Some(v) = v {
f(&k, &v.0, v.1);
}
for (k, v) in self.cache.lock().iter_enumerated() {
if let Some(v) = v {
f(&k, &v.0, v.1);
}
}
}
Expand All @@ -149,9 +145,6 @@ pub struct DefIdCache<V> {
///
/// The second element of the tuple is the set of keys actually present in the IndexVec, used
/// for faster iteration in `iter()`.
// FIXME: This may want to be sharded, like VecCache. However *how* to shard an IndexVec isn't
// super clear; VecCache is effectively not sharded today (see FIXME there). For now just omit
// that complexity here.
local: Lock<(IndexVec<DefIndex, Option<(V, DepNodeIndex)>>, Vec<DefIndex>)>,
foreign: DefaultCache<DefId, V>,
}
Expand Down