Skip to content

Commit 5912445

Browse files
committed
Use &C::Key less in queries.
Currently we use a mix of `C::Key` and `&C::Key` parameters. The former is more common and a bit nicer, so convert some of the latter. This results in less converting between the two types, and fewer sigils.
1 parent e370b60 commit 5912445

6 files changed

Lines changed: 28 additions & 29 deletions

File tree

compiler/rustc_macros/src/query.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,10 @@ fn make_helpers_for_query(query: &Query, streams: &mut HelperTokenStreams) {
342342

343343
// Generate a function to check whether we should cache the query to disk, for some key.
344344
if let Some(CacheOnDiskIf { block, .. }) = modifiers.cache_on_disk_if.as_ref() {
345-
// `disallowed_pass_by_ref` is needed because some keys are `rustc_pass_by_value`.
346345
streams.cache_on_disk_if_fns_stream.extend(quote! {
347-
#[allow(unused_variables, rustc::disallowed_pass_by_ref)]
346+
#[allow(unused_variables)]
348347
#[inline]
349-
pub fn #erased_name<'tcx>(tcx: TyCtxt<'tcx>, #key_pat: &#key_ty) -> bool
348+
pub fn #erased_name<'tcx>(tcx: TyCtxt<'tcx>, #key_pat: #key_ty) -> bool
350349
#block
351350
});
352351
}

compiler/rustc_middle/src/queries.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,7 +2407,7 @@ rustc_queries! {
24072407
/// sets of different crates do not intersect.
24082408
query exported_non_generic_symbols(cnum: CrateNum) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
24092409
desc { "collecting exported non-generic symbols for crate `{}`", cnum}
2410-
cache_on_disk_if { *cnum == LOCAL_CRATE }
2410+
cache_on_disk_if { cnum == LOCAL_CRATE }
24112411
separate_provide_extern
24122412
}
24132413

@@ -2420,7 +2420,7 @@ rustc_queries! {
24202420
/// sets of different crates do not intersect.
24212421
query exported_generic_symbols(cnum: CrateNum) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
24222422
desc { "collecting exported generic symbols for crate `{}`", cnum}
2423-
cache_on_disk_if { *cnum == LOCAL_CRATE }
2423+
cache_on_disk_if { cnum == LOCAL_CRATE }
24242424
separate_provide_extern
24252425
}
24262426

@@ -2788,7 +2788,7 @@ rustc_queries! {
27882788
query externally_implementable_items(cnum: CrateNum) -> &'tcx FxIndexMap<DefId, (EiiDecl, FxIndexMap<DefId, EiiImpl>)> {
27892789
arena_cache
27902790
desc { "looking up the externally implementable items of a crate" }
2791-
cache_on_disk_if { *cnum == LOCAL_CRATE }
2791+
cache_on_disk_if { cnum == LOCAL_CRATE }
27922792
separate_provide_extern
27932793
}
27942794

compiler/rustc_middle/src/query/inner.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use crate::ty::TyCtxt;
1616
///
1717
/// (Also performs some associated bookkeeping, if a value was found.)
1818
#[inline(always)]
19-
fn try_get_cached<'tcx, C>(tcx: TyCtxt<'tcx>, cache: &C, key: &C::Key) -> Option<C::Value>
19+
fn try_get_cached<'tcx, C>(tcx: TyCtxt<'tcx>, cache: &C, key: C::Key) -> Option<C::Value>
2020
where
2121
C: QueryCache,
2222
{
23-
match cache.lookup(key) {
23+
match cache.lookup(&key) {
2424
Some((value, index)) => {
2525
tcx.prof.query_cache_hit(index.into());
2626
tcx.dep_graph.read_index(index);
@@ -42,7 +42,7 @@ pub(crate) fn query_get_at<'tcx, C>(
4242
where
4343
C: QueryCache,
4444
{
45-
match try_get_cached(tcx, &query.cache, &key) {
45+
match try_get_cached(tcx, &query.cache, key) {
4646
Some(value) => value,
4747
None => (query.execute_query_fn)(tcx, span, key, QueryMode::Get).unwrap(),
4848
}
@@ -59,7 +59,7 @@ pub(crate) fn query_ensure<'tcx, C>(
5959
) where
6060
C: QueryCache,
6161
{
62-
if try_get_cached(tcx, &query.cache, &key).is_none() {
62+
if try_get_cached(tcx, &query.cache, key).is_none() {
6363
(query.execute_query_fn)(tcx, DUMMY_SP, key, QueryMode::Ensure { ensure_mode });
6464
}
6565
}
@@ -81,7 +81,7 @@ where
8181
{
8282
assert_matches!(ensure_mode, EnsureMode::Ok);
8383

84-
if let Some(res) = try_get_cached(tcx, &query.cache, &key) {
84+
if let Some(res) = try_get_cached(tcx, &query.cache, key) {
8585
erase::restore_val(res).map(drop)
8686
} else {
8787
(query.execute_query_fn)(tcx, DUMMY_SP, key, QueryMode::Ensure { ensure_mode })
@@ -111,7 +111,7 @@ pub(crate) fn query_feed<'tcx, C>(
111111
let format_value = query_vtable.format_value;
112112

113113
// Check whether the in-memory cache already has a value for this key.
114-
match try_get_cached(tcx, &query_vtable.cache, &key) {
114+
match try_get_cached(tcx, &query_vtable.cache, key) {
115115
Some(old) => {
116116
// The query already has a cached value for this key.
117117
// That's OK if both values are the same, i.e. they have the same hash,

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,17 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
121121
/// This should be the only code that calls the provider function.
122122
pub invoke_provider_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
123123

124-
pub will_cache_on_disk_for_key_fn: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
124+
pub will_cache_on_disk_for_key_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> bool,
125125

126126
pub try_load_from_disk_fn: fn(
127127
tcx: TyCtxt<'tcx>,
128-
key: &C::Key,
128+
key: C::Key,
129129
prev_index: SerializedDepNodeIndex,
130130
index: DepNodeIndex,
131131
) -> Option<C::Value>,
132132

133133
pub is_loadable_from_disk_fn:
134-
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
134+
fn(tcx: TyCtxt<'tcx>, key: C::Key, index: SerializedDepNodeIndex) -> bool,
135135

136136
/// Function pointer that hashes this query's result values.
137137
///

compiler/rustc_query_impl/src/execution.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::job::{QueryJobInfo, QueryJobMap, find_cycle_in_stack, report_cycle};
2222
use crate::plumbing::{current_query_job, next_job_id, start_query};
2323

2424
#[inline]
25-
fn equivalent_key<K: Eq, V>(k: &K) -> impl Fn(&(K, V)) -> bool + '_ {
26-
move |x| x.0 == *k
25+
fn equivalent_key<K: Eq, V>(k: K) -> impl Fn(&(K, V)) -> bool {
26+
move |x| x.0 == k
2727
}
2828

2929
/// Obtains the enclosed [`QueryJob`], or panics if this query evaluation
@@ -173,7 +173,7 @@ where
173173
// since unwinding also wants to look at this map, this can also prevent a double
174174
// panic.
175175
let mut shard = state.active.lock_shard_by_hash(key_hash);
176-
match shard.find_entry(key_hash, equivalent_key(&key)) {
176+
match shard.find_entry(key_hash, equivalent_key(key)) {
177177
Err(_) => None,
178178
Ok(occupied) => Some(occupied.remove().0.1),
179179
}
@@ -195,7 +195,7 @@ where
195195
let Self { state, key, key_hash } = *self;
196196
let job = {
197197
let mut shard = state.active.lock_shard_by_hash(key_hash);
198-
match shard.find_entry(key_hash, equivalent_key(&key)) {
198+
match shard.find_entry(key_hash, equivalent_key(key)) {
199199
Err(_) => panic!(),
200200
Ok(occupied) => {
201201
let ((key, value), vacant) = occupied.remove();
@@ -254,7 +254,7 @@ fn wait_for_query<'tcx, C: QueryCache>(
254254
// poisoned due to a panic instead.
255255
let key_hash = sharded::make_hash(&key);
256256
let shard = query.state.active.lock_shard_by_hash(key_hash);
257-
match shard.find(key_hash, equivalent_key(&key)) {
257+
match shard.find(key_hash, equivalent_key(key)) {
258258
// The query we waited on panicked. Continue unwinding here.
259259
Some((_, ActiveKeyStatus::Poisoned)) => FatalError.raise(),
260260
_ => panic!(
@@ -303,7 +303,7 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
303303

304304
let current_job_id = current_query_job();
305305

306-
match state_lock.entry(key_hash, equivalent_key(&key), |(k, _)| sharded::make_hash(k)) {
306+
match state_lock.entry(key_hash, equivalent_key(key), |(k, _)| sharded::make_hash(k)) {
307307
Entry::Vacant(entry) => {
308308
// Nothing has computed or is computing the query, so we start a new job and insert it in the
309309
// state map.
@@ -459,7 +459,7 @@ fn execute_job_incr<'tcx, C: QueryCache>(
459459
tcx,
460460
dep_graph_data,
461461
query,
462-
&key,
462+
key,
463463
dep_node,
464464
prev_index,
465465
dep_node_index,
@@ -507,7 +507,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
507507
tcx: TyCtxt<'tcx>,
508508
dep_graph_data: &DepGraphData,
509509
query: &'tcx QueryVTable<'tcx, C>,
510-
key: &C::Key,
510+
key: C::Key,
511511
dep_node: &DepNode,
512512
prev_index: SerializedDepNodeIndex,
513513
dep_node_index: DepNodeIndex,
@@ -570,7 +570,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
570570

571571
// The dep-graph for this computation is already in-place.
572572
// Call the query provider.
573-
let value = tcx.dep_graph.with_ignore(|| (query.invoke_provider_fn)(tcx, *key));
573+
let value = tcx.dep_graph.with_ignore(|| (query.invoke_provider_fn)(tcx, key));
574574

575575
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
576576

@@ -615,7 +615,7 @@ struct EnsureCanSkip {
615615
fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
616616
query: &'tcx QueryVTable<'tcx, C>,
617617
tcx: TyCtxt<'tcx>,
618-
key: &C::Key,
618+
key: C::Key,
619619
ensure_mode: EnsureMode,
620620
) -> EnsureCanSkip {
621621
// Queries with `eval_always` should never skip execution.
@@ -626,7 +626,7 @@ fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
626626
// Ensuring an anonymous query makes no sense
627627
assert!(!query.anon);
628628

629-
let dep_node = DepNode::construct(tcx, query.dep_kind, key);
629+
let dep_node = DepNode::construct(tcx, query.dep_kind, &key);
630630

631631
let dep_graph = &tcx.dep_graph;
632632
let serialized_dep_node_index = match dep_graph.try_mark_green(tcx, &dep_node) {
@@ -695,7 +695,7 @@ pub(super) fn execute_query_incr_inner<'tcx, C: QueryCache>(
695695
let dep_node: Option<DepNode> = match mode {
696696
QueryMode::Ensure { ensure_mode } => {
697697
let EnsureCanSkip { skip_execution, dep_node } =
698-
check_if_ensure_can_skip_execution(query, tcx, &key, ensure_mode);
698+
check_if_ensure_can_skip_execution(query, tcx, key, ensure_mode);
699699
if skip_execution {
700700
// Return early to skip execution.
701701
return None;

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub(crate) fn encode_query_results<'a, 'tcx, C, V>(
163163

164164
assert!(all_inactive(&query.state));
165165
query.cache.for_each(&mut |key, value, dep_node| {
166-
if (query.will_cache_on_disk_for_key_fn)(tcx, key) {
166+
if (query.will_cache_on_disk_for_key_fn)(tcx, *key) {
167167
let dep_node = SerializedDepNodeIndex::new(dep_node.index());
168168

169169
// Record position of the cache entry.
@@ -219,7 +219,7 @@ pub(crate) fn promote_from_disk_inner<'tcx, Q: GetQueryVTable<'tcx>>(
219219

220220
// If the recovered key isn't eligible for cache-on-disk, then there's no
221221
// value on disk to promote.
222-
if !(query.will_cache_on_disk_for_key_fn)(tcx, &key) {
222+
if !(query.will_cache_on_disk_for_key_fn)(tcx, key) {
223223
return;
224224
}
225225

0 commit comments

Comments
 (0)