Skip to content

Commit fd318a2

Browse files
committed
Reduce amount of function pointers.
1 parent f60a670 commit fd318a2

File tree

3 files changed

+58
-39
lines changed

3 files changed

+58
-39
lines changed

compiler/rustc_query_impl/src/plumbing.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,15 @@ macro_rules! define_queries {
386386
}
387387

388388
#[inline]
389-
fn compute(tcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
389+
fn compute_fn(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
390+
fn(TyCtxt<'tcx>, Self::Key) -> Self::Value
391+
{
390392
let is_local = key.query_crate() == LOCAL_CRATE;
391-
let provider = if is_local {
393+
if is_local {
392394
tcx.queries.local_providers.$name
393395
} else {
394396
tcx.queries.extern_providers.$name
395-
};
396-
provider(*tcx, key)
397+
}
397398
}
398399

399400
fn hash_result(

compiler/rustc_query_system/src/query/config.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
2323
pub dep_kind: CTX::DepKind,
2424
pub eval_always: bool,
2525

26-
// Don't use this method to compute query results, instead use the methods on TyCtxt
27-
pub compute: fn(CTX, K) -> V,
28-
2926
pub hash_result: fn(&mut CTX::StableHashingContext, &V) -> Option<Fingerprint>,
3027
pub handle_cycle_error: fn(CTX, DiagnosticBuilder<'_>) -> V,
3128
pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
@@ -40,10 +37,6 @@ impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
4037
DepNode::construct(tcx, self.dep_kind, key)
4138
}
4239

43-
pub(crate) fn compute(&self, tcx: CTX, key: K) -> V {
44-
(self.compute)(tcx, key)
45-
}
46-
4740
pub(crate) fn hash_result(
4841
&self,
4942
hcx: &mut CTX::StableHashingContext,
@@ -79,7 +72,7 @@ pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
7972
CTX: 'a;
8073

8174
// Don't use this method to compute query results, instead use the methods on TyCtxt
82-
fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
75+
fn compute_fn(tcx: CTX, key: &Self::Key) -> fn(CTX::DepContext, Self::Key) -> Self::Value;
8376

8477
fn hash_result(
8578
hcx: &mut CTX::StableHashingContext,
@@ -115,7 +108,6 @@ where
115108
anon: Q::ANON,
116109
dep_kind: Q::DEP_KIND,
117110
eval_always: Q::EVAL_ALWAYS,
118-
compute: Q::compute,
119111
hash_result: Q::hash_result,
120112
handle_cycle_error: Q::handle_cycle_error,
121113
cache_on_disk: Q::cache_on_disk,

compiler/rustc_query_system/src/query/plumbing.rs

+52-26
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ fn try_execute_query<CTX, C>(
428428
key: C::Key,
429429
lookup: QueryLookup,
430430
query: &QueryVtable<CTX, C::Key, C::Value>,
431+
compute: fn(CTX::DepContext, C::Key) -> C::Value,
431432
) -> C::Stored
432433
where
433434
C: QueryCache,
@@ -457,7 +458,7 @@ where
457458
// Fast path for when incr. comp. is off.
458459
if !dep_graph.is_fully_enabled() {
459460
let prof_timer = tcx.dep_context().profiler().query_provider();
460-
let result = tcx.start_query(job.id, None, || query.compute(tcx, key));
461+
let result = tcx.start_query(job.id, None, || compute(*tcx.dep_context(), key));
461462
let dep_node_index = dep_graph.next_virtual_depnode_index();
462463
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
463464
return job.complete(result, dep_node_index);
@@ -468,8 +469,9 @@ where
468469

469470
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
470471
tcx.start_query(job.id, diagnostics, || {
471-
dep_graph
472-
.with_anon_task(*tcx.dep_context(), query.dep_kind, || query.compute(tcx, key))
472+
dep_graph.with_anon_task(*tcx.dep_context(), query.dep_kind, || {
473+
compute(*tcx.dep_context(), key)
474+
})
473475
})
474476
});
475477

@@ -501,6 +503,7 @@ where
501503
dep_node_index,
502504
&dep_node,
503505
query,
506+
compute,
504507
),
505508
dep_node_index,
506509
)
@@ -511,7 +514,7 @@ where
511514
}
512515
}
513516

514-
let (result, dep_node_index) = force_query_with_job(tcx, key, job, dep_node, query);
517+
let (result, dep_node_index) = force_query_with_job(tcx, key, job, dep_node, query, compute);
515518
dep_graph.read_index(dep_node_index);
516519
result
517520
}
@@ -523,6 +526,7 @@ fn load_from_disk_and_cache_in_memory<CTX, K, V: Debug>(
523526
dep_node_index: DepNodeIndex,
524527
dep_node: &DepNode<CTX::DepKind>,
525528
query: &QueryVtable<CTX, K, V>,
529+
compute: fn(CTX::DepContext, K) -> V,
526530
) -> V
527531
where
528532
CTX: QueryContext,
@@ -565,7 +569,7 @@ where
565569
let prof_timer = tcx.dep_context().profiler().query_provider();
566570

567571
// The dep-graph for this computation is already in-place.
568-
let result = tcx.dep_context().dep_graph().with_ignore(|| query.compute(tcx, key));
572+
let result = tcx.dep_context().dep_graph().with_ignore(|| compute(*tcx.dep_context(), key));
569573

570574
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
571575

@@ -627,6 +631,7 @@ fn force_query_with_job<C, CTX>(
627631
job: JobOwner<'_, CTX::DepKind, C>,
628632
dep_node: DepNode<CTX::DepKind>,
629633
query: &QueryVtable<CTX, C::Key, C::Value>,
634+
compute: fn(CTX::DepContext, C::Key) -> C::Value,
630635
) -> (C::Stored, DepNodeIndex)
631636
where
632637
C: QueryCache,
@@ -653,17 +658,17 @@ where
653658
if query.eval_always {
654659
tcx.dep_context().dep_graph().with_eval_always_task(
655660
dep_node,
656-
tcx,
661+
*tcx.dep_context(),
657662
key,
658-
query.compute,
663+
compute,
659664
query.hash_result,
660665
)
661666
} else {
662667
tcx.dep_context().dep_graph().with_task(
663668
dep_node,
664-
tcx,
669+
*tcx.dep_context(),
665670
key,
666-
query.compute,
671+
compute,
667672
query.hash_result,
668673
)
669674
}
@@ -690,13 +695,14 @@ fn get_query_impl<CTX, C>(
690695
key: C::Key,
691696
lookup: QueryLookup,
692697
query: &QueryVtable<CTX, C::Key, C::Value>,
698+
compute: fn(CTX::DepContext, C::Key) -> C::Value,
693699
) -> C::Stored
694700
where
695701
CTX: QueryContext,
696702
C: QueryCache,
697703
C::Key: DepNodeParams<CTX::DepContext>,
698704
{
699-
try_execute_query(tcx, state, cache, span, key, lookup, query)
705+
try_execute_query(tcx, state, cache, span, key, lookup, query, compute)
700706
}
701707

702708
/// Ensure that either this query has all green inputs or been executed.
@@ -744,8 +750,10 @@ fn force_query_impl<CTX, C>(
744750
tcx: CTX,
745751
state: &QueryState<CTX::DepKind, C::Key>,
746752
cache: &QueryCacheStore<C>,
753+
key: C::Key,
747754
dep_node: DepNode<CTX::DepKind>,
748755
query: &QueryVtable<CTX, C::Key, C::Value>,
756+
compute: fn(CTX::DepContext, C::Key) -> C::Value,
749757
) -> bool
750758
where
751759
C: QueryCache,
@@ -754,18 +762,6 @@ where
754762
{
755763
debug_assert!(!query.anon);
756764

757-
if !<C::Key as DepNodeParams<CTX::DepContext>>::can_reconstruct_query_key() {
758-
return false;
759-
}
760-
761-
let key = if let Some(key) =
762-
<C::Key as DepNodeParams<CTX::DepContext>>::recover(*tcx.dep_context(), &dep_node)
763-
{
764-
key
765-
} else {
766-
return false;
767-
};
768-
769765
// We may be concurrently trying both execute and force a query.
770766
// Ensure that only one of them runs the query.
771767
let cached = cache.cache.lookup(cache, &key, |_, index| {
@@ -798,7 +794,7 @@ where
798794
TryGetJob::JobCompleted(_) => return true,
799795
};
800796

801-
force_query_with_job(tcx, key, job, dep_node, query);
797+
force_query_with_job(tcx, key, job, dep_node, query, compute);
802798

803799
true
804800
}
@@ -828,8 +824,17 @@ where
828824
}
829825

830826
debug!("ty::query::get_query<{}>(key={:?}, span={:?})", Q::NAME, key, span);
831-
let value =
832-
get_query_impl(tcx, Q::query_state(tcx), Q::query_cache(tcx), span, key, lookup, query);
827+
let compute = Q::compute_fn(tcx, &key);
828+
let value = get_query_impl(
829+
tcx,
830+
Q::query_state(tcx),
831+
Q::query_cache(tcx),
832+
span,
833+
key,
834+
lookup,
835+
query,
836+
compute,
837+
);
833838
Some(value)
834839
}
835840

@@ -843,5 +848,26 @@ where
843848
return false;
844849
}
845850

846-
force_query_impl(tcx, Q::query_state(tcx), Q::query_cache(tcx), *dep_node, &Q::VTABLE)
851+
if !<Q::Key as DepNodeParams<CTX::DepContext>>::can_reconstruct_query_key() {
852+
return false;
853+
}
854+
855+
let key = if let Some(key) =
856+
<Q::Key as DepNodeParams<CTX::DepContext>>::recover(*tcx.dep_context(), &dep_node)
857+
{
858+
key
859+
} else {
860+
return false;
861+
};
862+
863+
let compute = Q::compute_fn(tcx, &key);
864+
force_query_impl(
865+
tcx,
866+
Q::query_state(tcx),
867+
Q::query_cache(tcx),
868+
key,
869+
*dep_node,
870+
&Q::VTABLE,
871+
compute,
872+
)
847873
}

0 commit comments

Comments
 (0)