Skip to content

Commit 9a9f303

Browse files
Rollup merge of #147387 - azhogin:azhogin/hir_owner_parent_opt, r=petrochenkov
hir_owner_parent optimized to inlined call for non-incremental build Continuation of #146880 and #147232. 'hir_owner_parent' query renamed 'hir_owner_parent_q'. hir_owner_parent inlined function added to optimize performance in case of non-incremental build. 'hir_owner_parent' query has low normalized average execution time (163ns) and good cache_hits (5773) according Daria's processed statistics. 'source_span', for comparison, has avg_ns_norm = 66ns and cache_hits = 11361. Optimization may be profitable for queries with low normalized average execution time (to replace cache lookup into inlined call) and be significant with good cache_hits. | Query | cache_hits | min_ns | max_ns | avg_ns_norm | | ------------- | ------------- | ------------- | ------------- | ------------- | source_span | 11361 | 18 | 2991 | 66 hir_owner_parent | 5773 | 52 | 1773 | 163 is_doc_hidden | 3134 | 47 | 1111 | 285 lookup_deprecation_entry | 13905 | 36 | 6208 | 287 object_lifetime_default | 5840 | 63 | 4688 | 290 upvars_mentioned | 2575 | 75 | 7722 | 322 intrinsic_raw | 21235 | 73 | 3453 | 367 Draft PR to measure performance changes.
2 parents 370143f + a5052a0 commit 9a9f303

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,33 @@ impl<'tcx> TyCtxt<'tcx> {
365365
}
366366
}
367367
}
368+
369+
#[inline]
370+
fn hir_owner_parent_impl(self, owner_id: OwnerId) -> HirId {
371+
self.opt_local_parent(owner_id.def_id).map_or(CRATE_HIR_ID, |parent_def_id| {
372+
let parent_owner_id = self.local_def_id_to_hir_id(parent_def_id).owner;
373+
HirId {
374+
owner: parent_owner_id,
375+
local_id: self.hir_crate(()).owners[parent_owner_id.def_id]
376+
.unwrap()
377+
.parenting
378+
.get(&owner_id.def_id)
379+
.copied()
380+
.unwrap_or(ItemLocalId::ZERO),
381+
}
382+
})
383+
}
384+
385+
/// Optimization of `hir_owner_parent` query as an inlined function
386+
/// in case of non-incremental build. The query itself renamed to `hir_owner_parent_q`.
387+
#[inline]
388+
pub fn hir_owner_parent(self, owner_id: OwnerId) -> HirId {
389+
if self.dep_graph.is_fully_enabled() {
390+
self.hir_owner_parent_q(owner_id)
391+
} else {
392+
self.hir_owner_parent_impl(owner_id)
393+
}
394+
}
368395
}
369396

370397
/// Hashes computed by [`TyCtxt::hash_owner_nodes`] if necessary.
@@ -386,20 +413,7 @@ pub fn provide(providers: &mut Providers) {
386413
};
387414
providers.opt_hir_owner_nodes =
388415
|tcx, id| tcx.hir_crate(()).owners.get(id)?.as_owner().map(|i| &i.nodes);
389-
providers.hir_owner_parent = |tcx, owner_id| {
390-
tcx.opt_local_parent(owner_id.def_id).map_or(CRATE_HIR_ID, |parent_def_id| {
391-
let parent_owner_id = tcx.local_def_id_to_hir_id(parent_def_id).owner;
392-
HirId {
393-
owner: parent_owner_id,
394-
local_id: tcx.hir_crate(()).owners[parent_owner_id.def_id]
395-
.unwrap()
396-
.parenting
397-
.get(&owner_id.def_id)
398-
.copied()
399-
.unwrap_or(ItemLocalId::ZERO),
400-
}
401-
})
402-
};
416+
providers.hir_owner_parent_q = |tcx, owner_id| tcx.hir_owner_parent_impl(owner_id);
403417
providers.hir_attr_map = |tcx, id| {
404418
tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
405419
};

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ rustc_queries! {
266266
///
267267
/// This can be conveniently accessed by `tcx.hir_*` methods.
268268
/// Avoid calling this query directly.
269-
query hir_owner_parent(key: hir::OwnerId) -> hir::HirId {
269+
query hir_owner_parent_q(key: hir::OwnerId) -> hir::HirId {
270270
desc { |tcx| "getting HIR parent of `{}`", tcx.def_path_str(key) }
271271
}
272272

0 commit comments

Comments
 (0)