Skip to content

[perf] GenericArgs-related: Change asserts to debug asserts & use more slice interning over iterable interning #142289

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

Merged
merged 2 commits into from
Jun 14, 2025
Merged
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
3 changes: 1 addition & 2 deletions compiler/rustc_hir_analysis/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ fn create_generic_args<'tcx>(
tcx.impl_trait_header(parent).unwrap().trait_ref.instantiate_identity().args;

let trait_args = ty::GenericArgs::identity_for_item(tcx, sig_id);
let method_args =
tcx.mk_args_from_iter(trait_args.iter().skip(callee_generics.parent_count));
Comment on lines -279 to -280
Copy link
Member Author

Choose a reason for hiding this comment

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

Just a drive-by change, definitely not relevant or hot.

let method_args = tcx.mk_args(&trait_args[callee_generics.parent_count..]);
let method_args = build_generic_args(tcx, sig_id, def_id, method_args);

tcx.mk_args_from_iter(parent_args.iter().chain(method_args))
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

pub mod tls;

use std::assert_matches::{assert_matches, debug_assert_matches};
use std::assert_matches::debug_assert_matches;
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::env::VarError;
Expand Down Expand Up @@ -268,9 +268,9 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
def_id: DefId,
args: ty::GenericArgsRef<'tcx>,
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
let trait_def_id = self.parent(def_id);
assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
debug_assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
Comment on lines +271 to +273
Copy link
Member Author

Choose a reason for hiding this comment

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

The main and most relevant positive perf impact comes from changing these asserts to debug ones. Results: #142289 (comment).

let trait_generics = self.generics_of(trait_def_id);
(
ty::TraitRef::new_from_args(self, trait_def_id, args.truncate_to(self, trait_generics)),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/generic_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ impl<'tcx> GenericArgs<'tcx> {
}

pub fn truncate_to(&self, tcx: TyCtxt<'tcx>, generics: &ty::Generics) -> GenericArgsRef<'tcx> {
tcx.mk_args_from_iter(self.iter().take(generics.count()))
tcx.mk_args(&self[..generics.count()])
Copy link
Member Author

@fmease fmease Jun 12, 2025

Choose a reason for hiding this comment

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

This one wasn't tested in isolation (three perf runs are already a lot (I had to narrow down what is and what isn't perf relevant)) but I presume it to be helping since slicing & slice interning should generally be faster than iterating & "iterable interning" and this function is semi hot / lukewarm I'm pretty sure.

The first approach (which contains this change) yielded more improvements (results: #142289 (comment) (e.g., for image)) than the 1st commit (the assert changes) (results: #142289 (comment)), so this one might be the cause?

Copy link
Member Author

Choose a reason for hiding this comment

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

I could rerun perf for this change / commit only (which seems a bit overkill) or just drop it entirely? WDYT?

}

pub fn print_as_list(&self) -> String {
Expand Down
Loading