Skip to content

Commit bd42ef6

Browse files
committed
Introduce ArenaStorage.
1 parent e8ef41e commit bd42ef6

File tree

7 files changed

+97
-5
lines changed

7 files changed

+97
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4125,6 +4125,7 @@ dependencies = [
41254125
name = "rustc_query_system"
41264126
version = "0.0.0"
41274127
dependencies = [
4128+
"arena",
41284129
"log",
41294130
"parking_lot 0.10.2",
41304131
"rustc-rayon-core",

src/librustc_middle/query/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ rustc_queries! {
156156
/// Set of all the `DefId`s in this crate that have MIR associated with
157157
/// them. This includes all the body owners, but also things like struct
158158
/// constructors.
159-
query mir_keys(_: CrateNum) -> &'tcx FxHashSet<LocalDefId> {
159+
query mir_keys(_: CrateNum) -> FxHashSet<LocalDefId> {
160+
storage(ArenaCacheSelector<'tcx>)
160161
desc { "getting a list of all mir_keys" }
161162
}
162163

src/librustc_middle/ty/query/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ macro_rules! query_storage {
213213
<<$K as Key>::CacheSelector as CacheSelector<$K, $V>>::Cache
214214
};
215215
([storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
216-
$ty
216+
<$ty as CacheSelector<$K, $V>>::Cache
217217
};
218218
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
219219
query_storage!([$($($modifiers)*)*][$($args)*])

src/librustc_mir/transform/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn is_mir_available(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
6060

6161
/// Finds the full set of `DefId`s within the current crate that have
6262
/// MIR associated with them.
63-
fn mir_keys(tcx: TyCtxt<'_>, krate: CrateNum) -> &FxHashSet<LocalDefId> {
63+
fn mir_keys(tcx: TyCtxt<'_>, krate: CrateNum) -> FxHashSet<LocalDefId> {
6464
assert_eq!(krate, LOCAL_CRATE);
6565

6666
let mut set = FxHashSet::default();
@@ -97,7 +97,7 @@ fn mir_keys(tcx: TyCtxt<'_>, krate: CrateNum) -> &FxHashSet<LocalDefId> {
9797
.krate()
9898
.visit_all_item_likes(&mut GatherCtors { tcx, set: &mut set }.as_deep_visitor());
9999

100-
tcx.arena.alloc(set)
100+
set
101101
}
102102

103103
/// Where a specific `mir::Body` comes from.

src/librustc_query_system/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ path = "lib.rs"
1010
doctest = false
1111

1212
[dependencies]
13+
arena = { path = "../libarena" }
1314
log = { version = "0.4", features = ["release_max_level_info", "std"] }
1415
rustc-rayon-core = "0.3.0"
1516
rustc_data_structures = { path = "../librustc_data_structures" }

src/librustc_query_system/query/caches.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use crate::dep_graph::DepNodeIndex;
22
use crate::query::plumbing::{QueryLookup, QueryState};
33
use crate::query::QueryContext;
44

5+
use arena::TypedArena;
56
use rustc_data_structures::fx::FxHashMap;
67
use rustc_data_structures::sharded::Sharded;
8+
use rustc_data_structures::sync::WorkerLocal;
79
use std::default::Default;
810
use std::hash::Hash;
911
use std::marker::PhantomData;
@@ -133,3 +135,88 @@ impl<K: Eq + Hash, V: Clone> QueryCache for DefaultCache<K, V> {
133135
f(Box::new(results))
134136
}
135137
}
138+
139+
pub struct ArenaCacheSelector<'tcx>(PhantomData<&'tcx ()>);
140+
141+
impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<K, V> for ArenaCacheSelector<'tcx> {
142+
type Cache = ArenaCache<'tcx, K, V>;
143+
}
144+
145+
pub struct ArenaCache<'tcx, K, V> {
146+
arena: WorkerLocal<&'tcx TypedArena<(V, DepNodeIndex)>>,
147+
phantom: PhantomData<K>,
148+
}
149+
150+
impl<'tcx, K, V> Default for ArenaCache<'tcx, K, V> {
151+
fn default() -> Self {
152+
ArenaCache {
153+
arena: WorkerLocal::new(|_| &*Box::leak(Box::new(TypedArena::default()))),
154+
phantom: PhantomData,
155+
}
156+
}
157+
}
158+
159+
impl<'tcx, K: Eq + Hash, V: 'tcx> QueryStorage for ArenaCache<'tcx, K, V> {
160+
type Value = V;
161+
type Stored = &'tcx V;
162+
163+
fn store_nocache(&self, value: Self::Value) -> Self::Stored {
164+
let value = self.arena.alloc((value, DepNodeIndex::INVALID));
165+
&value.0
166+
}
167+
}
168+
169+
impl<'tcx, K: Eq + Hash, V: 'tcx> QueryCache for ArenaCache<'tcx, K, V> {
170+
type Key = K;
171+
type Sharded = FxHashMap<K, &'tcx (V, DepNodeIndex)>;
172+
173+
#[inline(always)]
174+
fn lookup<CTX: QueryContext, R, OnHit, OnMiss>(
175+
&self,
176+
state: &QueryState<CTX, Self>,
177+
key: K,
178+
on_hit: OnHit,
179+
on_miss: OnMiss,
180+
) -> R
181+
where
182+
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
183+
OnMiss: FnOnce(K, QueryLookup<'_, CTX, K, Self::Sharded>) -> R,
184+
{
185+
let mut lookup = state.get_lookup(&key);
186+
let lock = &mut *lookup.lock;
187+
188+
let result = lock.cache.raw_entry().from_key_hashed_nocheck(lookup.key_hash, &key);
189+
190+
if let Some((_, value)) = result {
191+
on_hit(&&value.0, value.1)
192+
} else {
193+
on_miss(key, lookup)
194+
}
195+
}
196+
197+
#[inline]
198+
fn complete<CTX: QueryContext>(
199+
&self,
200+
_: CTX,
201+
lock_sharded_storage: &mut Self::Sharded,
202+
key: K,
203+
value: V,
204+
index: DepNodeIndex,
205+
) -> Self::Stored {
206+
let value = self.arena.alloc((value, index));
207+
lock_sharded_storage.insert(key, value);
208+
&value.0
209+
}
210+
211+
fn iter<R, L>(
212+
&self,
213+
shards: &Sharded<L>,
214+
get_shard: impl Fn(&mut L) -> &mut Self::Sharded,
215+
f: impl for<'a> FnOnce(Box<dyn Iterator<Item = (&'a K, &'a V, DepNodeIndex)> + 'a>) -> R,
216+
) -> R {
217+
let mut shards = shards.lock_shards();
218+
let mut shards: Vec<_> = shards.iter_mut().map(|shard| get_shard(shard)).collect();
219+
let results = shards.iter_mut().flat_map(|shard| shard.iter()).map(|(k, v)| (k, &v.0, v.1));
220+
f(Box::new(results))
221+
}
222+
}

src/librustc_query_system/query/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ pub use self::job::deadlock;
77
pub use self::job::{QueryInfo, QueryJob, QueryJobId, QueryJobInfo};
88

99
mod caches;
10-
pub use self::caches::{CacheSelector, DefaultCacheSelector, QueryCache, QueryStorage};
10+
pub use self::caches::{
11+
ArenaCacheSelector, CacheSelector, DefaultCacheSelector, QueryCache, QueryStorage,
12+
};
1113

1214
mod config;
1315
pub use self::config::{QueryAccessors, QueryConfig, QueryDescription};

0 commit comments

Comments
 (0)