Skip to content

Commit f8de392

Browse files
authored
Rollup merge of #108169 - Zoxc:query-key-copy, r=cjgillot
Make query keys `Copy` This regressed compiler performance locally, so I'm curious what perf will say about it. <table><tr><td rowspan="2">Benchmark</td><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th></tr><tr><td align="right">Time</td><td align="right">Time</td><td align="right">%</th></tr><tr><td>🟣 <b>clap</b>:check</td><td align="right">1.7566s</td><td align="right">1.7657s</td><td align="right"> 0.52%</td></tr><tr><td>🟣 <b>hyper</b>:check</td><td align="right">0.2572s</td><td align="right">0.2578s</td><td align="right"> 0.20%</td></tr><tr><td>🟣 <b>regex</b>:check</td><td align="right">0.9863s</td><td align="right">0.9900s</td><td align="right"> 0.37%</td></tr><tr><td>🟣 <b>syn</b>:check</td><td align="right">1.6018s</td><td align="right">1.6073s</td><td align="right"> 0.34%</td></tr><tr><td>🟣 <b>syntex_syntax</b>:check</td><td align="right">6.2493s</td><td align="right">6.2920s</td><td align="right"> 0.68%</td></tr><tr><td>Total</td><td align="right">10.8512s</td><td align="right">10.9127s</td><td align="right"> 0.57%</td></tr><tr><td>Summary</td><td align="right">1.0000s</td><td align="right">1.0042s</td><td align="right"> 0.42%</td></tr></table>
2 parents 58baee1 + 056c5b3 commit f8de392

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

compiler/rustc_query_system/src/query/caches.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub trait QueryStorage {
2121
}
2222

2323
pub trait QueryCache: QueryStorage + Sized {
24-
type Key: Hash + Eq + Clone + Debug;
24+
type Key: Hash + Eq + Copy + Debug;
2525

2626
/// Checks if the query is already computed and in the cache.
2727
/// It returns the shard index and a lock guard to the shard,
@@ -61,7 +61,7 @@ impl<K: Eq + Hash, V: Copy + Debug> QueryStorage for DefaultCache<K, V> {
6161

6262
impl<K, V> QueryCache for DefaultCache<K, V>
6363
where
64-
K: Eq + Hash + Clone + Debug,
64+
K: Eq + Hash + Copy + Debug,
6565
V: Copy + Debug,
6666
{
6767
type Key = K;
@@ -179,7 +179,7 @@ impl<K: Eq + Idx, V: Copy + Debug> QueryStorage for VecCache<K, V> {
179179

180180
impl<K, V> QueryCache for VecCache<K, V>
181181
where
182-
K: Eq + Idx + Clone + Debug,
182+
K: Eq + Idx + Copy + Debug,
183183
V: Copy + Debug,
184184
{
185185
type Key = K;

compiler/rustc_query_system/src/query/config.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ pub type TryLoadFromDisk<Qcx, Q> =
1919
pub trait QueryConfig<Qcx: QueryContext> {
2020
const NAME: &'static str;
2121

22-
type Key: DepNodeParams<Qcx::DepContext> + Eq + Hash + Clone + Debug;
22+
// `Key` and `Value` are `Copy` instead of `Clone` to ensure copying them stays cheap,
23+
// but it isn't necessary.
24+
type Key: DepNodeParams<Qcx::DepContext> + Eq + Hash + Copy + Debug;
2325
type Value: Debug + Copy;
2426

2527
type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;

compiler/rustc_query_system/src/query/plumbing.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ enum QueryResult<D: DepKind> {
4848

4949
impl<K, D> QueryState<K, D>
5050
where
51-
K: Eq + Hash + Clone + Debug,
51+
K: Eq + Hash + Copy + Debug,
5252
D: DepKind,
5353
{
5454
pub fn all_inactive(&self) -> bool {
@@ -77,7 +77,7 @@ where
7777
for shard in shards.iter() {
7878
for (k, v) in shard.iter() {
7979
if let QueryResult::Started(ref job) = *v {
80-
let query = make_query(qcx, k.clone());
80+
let query = make_query(qcx, *k);
8181
jobs.insert(job.id, QueryJobInfo { query, job: job.clone() });
8282
}
8383
}
@@ -91,7 +91,7 @@ where
9191
// really hurt much.)
9292
for (k, v) in self.active.try_lock()?.iter() {
9393
if let QueryResult::Started(ref job) = *v {
94-
let query = make_query(qcx, k.clone());
94+
let query = make_query(qcx, *k);
9595
jobs.insert(job.id, QueryJobInfo { query, job: job.clone() });
9696
}
9797
}
@@ -111,7 +111,7 @@ impl<K, D: DepKind> Default for QueryState<K, D> {
111111
/// This will poison the relevant query if dropped.
112112
struct JobOwner<'tcx, K, D: DepKind>
113113
where
114-
K: Eq + Hash + Clone,
114+
K: Eq + Hash + Copy,
115115
{
116116
state: &'tcx QueryState<K, D>,
117117
key: K,
@@ -163,7 +163,7 @@ where
163163

164164
impl<'tcx, K, D: DepKind> JobOwner<'tcx, K, D>
165165
where
166-
K: Eq + Hash + Clone,
166+
K: Eq + Hash + Copy,
167167
{
168168
/// Either gets a `JobOwner` corresponding the query, allowing us to
169169
/// start executing the query, or returns with the result of the query.
@@ -195,7 +195,7 @@ where
195195
let job = qcx.current_query_job();
196196
let job = QueryJob::new(id, span, job);
197197

198-
let key = entry.key().clone();
198+
let key = *entry.key();
199199
entry.insert(QueryResult::Started(job));
200200

201201
let owner = JobOwner { state, id, key };
@@ -274,7 +274,7 @@ where
274274

275275
impl<'tcx, K, D> Drop for JobOwner<'tcx, K, D>
276276
where
277-
K: Eq + Hash + Clone,
277+
K: Eq + Hash + Copy,
278278
D: DepKind,
279279
{
280280
#[inline(never)]
@@ -291,7 +291,7 @@ where
291291
QueryResult::Started(job) => job,
292292
QueryResult::Poisoned => panic!(),
293293
};
294-
shard.insert(self.key.clone(), QueryResult::Poisoned);
294+
shard.insert(self.key, QueryResult::Poisoned);
295295
job
296296
};
297297
// Also signal the completion of the job, so waiters
@@ -310,7 +310,7 @@ pub(crate) struct CycleError<D: DepKind> {
310310
/// The result of `try_start`.
311311
enum TryGetJob<'tcx, K, D>
312312
where
313-
K: Eq + Hash + Clone,
313+
K: Eq + Hash + Copy,
314314
D: DepKind,
315315
{
316316
/// The query is not yet started. Contains a guard to the cache eventually used to start it.
@@ -358,10 +358,9 @@ where
358358
Q: QueryConfig<Qcx>,
359359
Qcx: QueryContext,
360360
{
361-
match JobOwner::<'_, Q::Key, Qcx::DepKind>::try_start(&qcx, state, span, key.clone()) {
361+
match JobOwner::<'_, Q::Key, Qcx::DepKind>::try_start(&qcx, state, span, key) {
362362
TryGetJob::NotYetStarted(job) => {
363-
let (result, dep_node_index) =
364-
execute_job::<Q, Qcx>(qcx, key.clone(), dep_node, job.id);
363+
let (result, dep_node_index) = execute_job::<Q, Qcx>(qcx, key, dep_node, job.id);
365364
if Q::FEEDABLE {
366365
// We should not compute queries that also got a value via feeding.
367366
// This can't happen, as query feeding adds the very dependencies to the fed query
@@ -551,7 +550,7 @@ where
551550
let prof_timer = qcx.dep_context().profiler().query_provider();
552551

553552
// The dep-graph for this computation is already in-place.
554-
let result = dep_graph.with_ignore(|| Q::compute(qcx, key.clone()));
553+
let result = dep_graph.with_ignore(|| Q::compute(qcx, *key));
555554

556555
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
557556

0 commit comments

Comments
 (0)