Skip to content

Queryify has_attr to improve performance #127185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/query/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,14 @@ impl Key for Option<Symbol> {
}
}

impl Key for (DefId, Symbol) {
type Cache<V> = DefaultCache<Self, V>;

fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}

/// Canonical query goals correspond to abstract trait operations that
/// are not tied to any crate in particular.
impl<'tcx, T: Clone> Key for Canonical<'tcx, T> {
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,12 @@ rustc_queries! {
separate_provide_extern
}

/// This query backs the [`TyCtxt::has_attr`] function to make it faster.
/// You should use that function instead.
query has_attr_query(key: (DefId, Symbol)) -> bool {
desc { |tcx| "checking whether `{}` has attr {}", tcx.def_path_str(key.0), key.1 }
}

/// Determines whether an item is annotated with `doc(hidden)`.
query is_doc_hidden(def_id: DefId) -> bool {
desc { |tcx| "checking whether `{}` is `doc(hidden)`", tcx.def_path_str(def_id) }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ impl<'tcx> TyCtxt<'tcx> {

/// Determines whether an item is annotated with an attribute.
pub fn has_attr(self, did: impl Into<DefId>, attr: Symbol) -> bool {
self.get_attrs(did, attr).next().is_some()
self.has_attr_query((did.into(), attr))
}

/// Determines whether an item is annotated with a multi-segement attribute
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
use rustc_index::bit_set::GrowableBitSet;
use rustc_macros::{extension, HashStable, TyDecodable, TyEncodable};
use rustc_session::Limit;
use rustc_span::sym;
use rustc_span::{sym, Symbol};
use rustc_target::abi::{Float, Integer, IntegerType, Size};
use rustc_target::spec::abi::Abi;
use smallvec::{smallvec, SmallVec};
Expand Down Expand Up @@ -1829,6 +1829,12 @@ pub fn reveal_opaque_types_in_bounds<'tcx>(
val.fold_with(&mut visitor)
}

/// Determines whether an item is annotated with an attribute.
pub fn has_attr_query(tcx: TyCtxt<'_>, key: (DefId, Symbol)) -> bool {
let (did, attr) = key;
Comment on lines +1833 to +1834
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn has_attr_query(tcx: TyCtxt<'_>, key: (DefId, Symbol)) -> bool {
let (did, attr) = key;
pub fn has_attr_query(tcx: TyCtxt<'_>, (did, attr): (DefId, Symbol)) -> bool {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll change that after the perf run. I forgot to change it from the query declaration's signature 😅

tcx.get_attrs(did, attr).next().is_some()
}

/// Determines whether an item is directly annotated with `doc(hidden)`.
fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
tcx.get_attrs(def_id, sym::doc)
Expand Down Expand Up @@ -1865,6 +1871,7 @@ pub fn intrinsic_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::Intrinsi
pub fn provide(providers: &mut Providers) {
*providers = Providers {
reveal_opaque_types_in_bounds,
has_attr_query,
is_doc_hidden,
is_doc_notable_trait,
intrinsic_raw,
Expand Down
Loading