Skip to content

Avoid instantiating FnSig when not necessary - #165863

Open
nnethercote wants to merge 1 commit into
rust-lang:mainfrom
nnethercote:avoid-instantiating-FnSig
Open

nnethercote wants to merge 1 commit into
rust-lang:mainfrom
nnethercote:avoid-instantiating-FnSig

Conversation

@nnethercote

@nnethercote nnethercote commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

In various places we call fn_sig, which is expensive because it instantiates the signature, but then only look at the ABI or the safety, which don't require the instantiation. This commit introduces fn_sig_abi and fn_sig_safety which are alternatives that skip instantiation.

In check_instance_abi the commit also changes instance.ty(...) to tcx.type_of(...) to avoid a second unnecessary instantiation path.

r? @bjorn3

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 12, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 12, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 12, 2026
Avoid instantiating `FnSig` when not necessary
@rust-bors

rust-bors Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 15c8031 (15c8031dc8d8bb8da526299cf9bb3b3a89471372)
Base parent: b4fe65c (b4fe65c19d1bb6e720c843086b785493c453028d)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (15c8031): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.3% [-0.5%, -0.2%] 49
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.2%] 28
All ❌✅ (primary) -0.3% [-0.5%, -0.2%] 49

Max RSS (memory usage)

Results (primary 0.4%, secondary 1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
5.2% [5.2%, 5.2%] 1
Regressions ❌
(secondary)
5.4% [3.8%, 7.1%] 2
Improvements ✅
(primary)
-2.0% [-2.1%, -2.0%] 2
Improvements ✅
(secondary)
-6.9% [-6.9%, -6.9%] 1
All ❌✅ (primary) 0.4% [-2.1%, 5.2%] 3

Cycles

Results (primary -2.0%, secondary -11.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.0% [-2.0%, -2.0%] 1
Improvements ✅
(secondary)
-11.2% [-18.7%, -3.7%] 2
All ❌✅ (primary) -2.0% [-2.0%, -2.0%] 1

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 482.212s -> 481.954s (-0.05%)
Artifact size: 406.08 MiB -> 406.08 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 12, 2026
@rust-bors

This comment has been minimized.

In various places we call `fn_sig`, which is expensive because it
instantiates the signature, but then only look at the ABI or the safety,
which don't require the instantiation. This commit introduces
`fn_sig_abi` and `fn_sig_safety` which are alternatives that skip
instantiation.

In `check_instance_abi` the commit also changes `instance.ty(...)` to
`tcx.type_of(...)` to avoid a second unnecessary instantiation path.
@nnethercote
nnethercote force-pushed the avoid-instantiating-FnSig branch from 4d5b1d2 to 9c20e78 Compare November 12, 2026 22:03
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 12, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

LLM disclosure: this PR involve LLM analysis of profiles. I wrote the code and text myself.

@nnethercote
nnethercote marked this pull request as ready for review November 12, 2026 22:11
@rustbot

rustbot commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 12, 2026
match *autoderef.final_ty().kind() {
ty::FnDef(def_id, _) => {
let abi = self.tcx.fn_sig(def_id).skip_binder().skip_binder().abi();
// njn: move double skip_binder removal to precursor commit

@bjorn3 bjorn3 Nov 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

&& let fn_sig @ ty::FnSig {
..
} = fn_ty.fn_sig(tcx).skip_binder()
// njn: move this cleanup to a separate commit

@bjorn3 bjorn3 Nov 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants