Skip to content

Commit face186

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 c7b206b commit face186

File tree

6 files changed

+28
-29
lines changed

6 files changed

+28
-29
lines changed

compiler/rustc_macros/src/query.rs

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

356356
// Generate a function to check whether we should cache the query to disk, for some key.
357357
if let Some(CacheOnDiskIf { block, .. }) = modifiers.cache_on_disk_if.as_ref() {
358-
// `disallowed_pass_by_ref` is needed because some keys are `rustc_pass_by_value`.
359358
streams.cache_on_disk_if_fns_stream.extend(quote! {
360-
#[allow(unused_variables, rustc::disallowed_pass_by_ref)]
359+
#[allow(unused_variables)]
361360
#[inline]
362-
pub fn #erased_name<'tcx>(tcx: TyCtxt<'tcx>, #key_pat: &#key_ty) -> bool
361+
pub fn #erased_name<'tcx>(tcx: TyCtxt<'tcx>, #key_pat: #key_ty) -> bool
363362
#block
364363
});
365364
}

compiler/rustc_middle/src/queries.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ rustc_queries! {
23912391
/// sets of different crates do not intersect.
23922392
query exported_non_generic_symbols(cnum: CrateNum) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
23932393
desc { "collecting exported non-generic symbols for crate `{}`", cnum}
2394-
cache_on_disk_if { *cnum == LOCAL_CRATE }
2394+
cache_on_disk_if { cnum == LOCAL_CRATE }
23952395
separate_provide_extern
23962396
}
23972397

@@ -2404,7 +2404,7 @@ rustc_queries! {
24042404
/// sets of different crates do not intersect.
24052405
query exported_generic_symbols(cnum: CrateNum) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
24062406
desc { "collecting exported generic symbols for crate `{}`", cnum}
2407-
cache_on_disk_if { *cnum == LOCAL_CRATE }
2407+
cache_on_disk_if { cnum == LOCAL_CRATE }
24082408
separate_provide_extern
24092409
}
24102410

@@ -2771,7 +2771,7 @@ rustc_queries! {
27712771
query externally_implementable_items(cnum: CrateNum) -> &'tcx FxIndexMap<DefId, (EiiDecl, FxIndexMap<DefId, EiiImpl>)> {
27722772
arena_cache
27732773
desc { "looking up the externally implementable items of a crate" }
2774-
cache_on_disk_if { *cnum == LOCAL_CRATE }
2774+
cache_on_disk_if { cnum == LOCAL_CRATE }
27752775
separate_provide_extern
27762776
}
27772777

compiler/rustc_middle/src/query/inner.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use crate::ty::TyCtxt;
1515
///
1616
/// (Also performs some associated bookkeeping, if a value was found.)
1717
#[inline(always)]
18-
fn try_get_cached<'tcx, C>(tcx: TyCtxt<'tcx>, cache: &C, key: &C::Key) -> Option<C::Value>
18+
fn try_get_cached<'tcx, C>(tcx: TyCtxt<'tcx>, cache: &C, key: C::Key) -> Option<C::Value>
1919
where
2020
C: QueryCache,
2121
{
22-
match cache.lookup(key) {
22+
match cache.lookup(&key) {
2323
Some((value, index)) => {
2424
tcx.prof.query_cache_hit(index.into());
2525
tcx.dep_graph.read_index(index);
@@ -41,7 +41,7 @@ pub(crate) fn query_get_at<'tcx, C>(
4141
where
4242
C: QueryCache,
4343
{
44-
match try_get_cached(tcx, &query.cache, &key) {
44+
match try_get_cached(tcx, &query.cache, key) {
4545
Some(value) => value,
4646
None => (query.execute_query_fn)(tcx, span, key, QueryMode::Get).unwrap(),
4747
}
@@ -58,7 +58,7 @@ pub(crate) fn query_ensure_ok_or_done<'tcx, C>(
5858
) where
5959
C: QueryCache,
6060
{
61-
match try_get_cached(tcx, &query.cache, &key) {
61+
match try_get_cached(tcx, &query.cache, key) {
6262
Some(_value) => {}
6363
None => {
6464
(query.execute_query_fn)(tcx, DUMMY_SP, key, QueryMode::Ensure { ensure_mode });
@@ -78,7 +78,7 @@ where
7878
C: QueryCache<Value = Erased<Result<T, ErrorGuaranteed>>>,
7979
Result<T, ErrorGuaranteed>: Erasable,
8080
{
81-
match try_get_cached(tcx, &query.cache, &key) {
81+
match try_get_cached(tcx, &query.cache, key) {
8282
Some(value) => erase::restore_val(value).map(drop),
8383
None => (query.execute_query_fn)(
8484
tcx,
@@ -112,7 +112,7 @@ pub(crate) fn query_feed<'tcx, C>(
112112
let format_value = query_vtable.format_value;
113113

114114
// Check whether the in-memory cache already has a value for this key.
115-
match try_get_cached(tcx, &query_vtable.cache, &key) {
115+
match try_get_cached(tcx, &query_vtable.cache, key) {
116116
Some(old) => {
117117
// The query already has a cached value for this key.
118118
// 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)