Skip to content

Auto trait finder: Avoid recomputing ParamEnv if nothing changed - #165774

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
jdonszelmann:better-auto-traits
Nov 18, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
jdonszelmann:better-auto-traits

Conversation

@jdonszelmann

@jdonszelmann jdonszelmann commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

@rustbot rustbot added 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. labels Nov 10, 2026
@jdonszelmann

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 10, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 10, 2026
@rust-log-analyzer

This comment has been minimized.

@jdonszelmann

Copy link
Copy Markdown
Contributor Author

@bors try cancel

@rust-bors

rust-bors Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Try build cancelled. Cancelled workflows:

Hint: if you want to run another try build, you do not need to manually cancel the previous one. Just run @bors try and bors will cancel the previous build automatically.

@jdonszelmann

Copy link
Copy Markdown
Contributor Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 10, 2026
@jdonszelmann

Copy link
Copy Markdown
Contributor Author

@bors try cancel

@rust-bors

rust-bors Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Try build cancelled. Cancelled workflows:

Hint: if you want to run another try build, you do not need to manually cancel the previous one. Just run @bors try and bors will cancel the previous build automatically.

@jdonszelmann

Copy link
Copy Markdown
Contributor Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 10, 2026
@Kobzol

Kobzol commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

(You don't need to cancel try builds manually, if you do @bors try, it will auto-cancel the currently running try build for you, before starting a new one :) The hint hints at that.)

@rust-bors

rust-bors Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 20a62ed (20a62edc5f4be076a2718d931dbb605fa812e6c4)
Base parent: b6c79e8 (b6c79e88af6825838aed7c6a159e7e0b00259443)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (20a62ed): 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)
-1.7% [-15.6%, -0.2%] 33
Improvements ✅
(secondary)
-1.2% [-3.4%, -0.2%] 10
All ❌✅ (primary) -1.7% [-15.6%, -0.2%] 33

Max RSS (memory usage)

Results (primary 1.1%, secondary 1.2%)

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

mean range count
Regressions ❌
(primary)
3.3% [3.3%, 3.3%] 1
Regressions ❌
(secondary)
2.7% [2.6%, 2.8%] 3
Improvements ✅
(primary)
-1.1% [-1.1%, -1.1%] 1
Improvements ✅
(secondary)
-3.5% [-3.5%, -3.5%] 1
All ❌✅ (primary) 1.1% [-1.1%, 3.3%] 2

Cycles

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

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

mean range count
Regressions ❌
(primary)
2.7% [2.7%, 2.7%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-9.1% [-9.5%, -8.6%] 2
Improvements ✅
(secondary)
-2.0% [-2.0%, -2.0%] 1
All ❌✅ (primary) -5.2% [-9.5%, 2.7%] 3

Binary size

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

Bootstrap: 482.793s -> 484.101s (0.27%)
Artifact size: 406.28 MiB -> 406.33 MiB (0.01%)

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

Copy link
Copy Markdown
Contributor Author

well, that's pretty worth it!

@rust-bors rust-bors Bot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 16, 2026
@rust-bors

rust-bors Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6110103 (6110103ac19e4c6ab71812951c3c226b88418cd4)
Base parent: 5fb8a16 (5fb8a16f5f8244f9fbfe696487a9da223168a6c7)

@jdonszelmann

Copy link
Copy Markdown
Contributor Author

@bors r=fmease

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

rustbot commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-bors rust-bors Bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Nov 16, 2026
@rust-bors

rust-bors Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

⚠️ A new commit 2a137d3b632dd85045abe4205dbffca74c2e5868 was pushed.

This pull request was unapproved.

@rust-bors

rust-bors Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 2a137d3 has been tentatively approved by fmease

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 16, 2026
@rust-bors

rust-bors Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

⌛ Testing commit 2a137d3 with merge 3933833...

Workflow: https://github.com/rust-lang/rust/actions/runs/44259539524

rust-bors Bot pushed a commit that referenced this pull request Nov 18, 2026
Auto trait finder: Avoid recomputing `ParamEnv` if nothing changed



r? @fmease

> [!NOTE] 
> <table>
> <tr><td>
> 
> ![ai:0](https://github.com/jdonszelmann/0rwx.com/blob/main/static/50x50/AI-0-50x50.png?raw=true)
> 
> </td><td>
> 
> [I've not used an LLM for any part of this PR, or any other PR I make. This includes any related work like research.](https://0rwx.com/.)
> 
> </td></tr>
> </table>
@JonathanBrouwer

Copy link
Copy Markdown
Member

test-x86_64-mingw-1 seems to be slow, doesn't seem to be stuck tho so lets see if it finishes?

@JonathanBrouwer

JonathanBrouwer commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

This might or might not still finish, not sure, but let's just kill it.
@bors cancel

Creating a perf-sensitive rollup
@bors rollup=iffy

@rust-bors

rust-bors Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #166141.

@jdonszelmann

Copy link
Copy Markdown
Contributor Author

fair enough, sad :/

rust-bors Bot pushed a commit that referenced this pull request Nov 18, 2026
…uwer

Rollup of 4 perf-sensitive pull requests

Successful merges:

 - #165396 (Perf - Separate flag computation & intern functions per `TyKind`)
 - #165774 (Auto trait finder: Avoid recomputing `ParamEnv` if nothing changed)
 - #165863 (Avoid instantiating `FnSig` when not necessary)
 - #166084 (Avoid unnecessary LLVM passes when thin LTO buffer is null)
@rust-bors
rust-bors Bot merged commit e2c96e4 into rust-lang:main Nov 18, 2026
14 of 15 checks passed
rust-bors Bot pushed a commit that referenced this pull request Nov 18, 2026
Rollup merge of #165774 - jdonszelmann:better-auto-traits, r=fmease

Auto trait finder: Avoid recomputing `ParamEnv` if nothing changed

r? @fmease

> [!NOTE]
> <table>
> <tr><td>
>
> ![ai:0](https://github.com/jdonszelmann/0rwx.com/blob/main/static/50x50/AI-0-50x50.png?raw=true)
>
> </td><td>
>
> [I've not used an LLM for any part of this PR, or any other PR I make. This includes any related work like research.](https://0rwx.com/.)
>
> </td></tr>
> </table>
@rustbot rustbot added this to the 1.102.0 milestone Nov 18, 2026
@rust-timer

Copy link
Copy Markdown
Collaborator

Note

This PR was benchmarked as part of triage of its containing rollup: triage URL.

Finished benchmarking commit (89b1c43): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -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)
-1.9% [-15.3%, -0.2%] 29
Improvements ✅
(secondary)
-1.4% [-3.3%, -0.3%] 8
All ❌✅ (primary) -1.9% [-15.3%, -0.2%] 29

Max RSS (memory usage)

Results (secondary -1.1%)

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)
- - 0
Improvements ✅
(secondary)
-1.1% [-1.3%, -0.9%] 3
All ❌✅ (primary) - - 0

Cycles

Results (primary -4.7%, secondary -1.1%)

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)
-4.7% [-8.7%, -2.2%] 5
Improvements ✅
(secondary)
-1.1% [-1.7%, -0.5%] 2
All ❌✅ (primary) -4.7% [-8.7%, -2.2%] 5

Binary size

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

Artifact size: 405.23 MiB -> 405.27 MiB (0.01%)

rust-bors Bot pushed a commit that referenced this pull request Nov 19, 2026
…illa

Compressed `ParamEnv`s for common marker traits



r? project-trait-system-refactor

This implements "compressed" `ParamEnv`s. It uses the fact that a lot of paramenvs contain many similar bounds on each generic. For example:

```rust
fn foo<A: Clone, B: Clone>(a: A, b: B) {}
```

Contains the bounds `A: Clone, A: Sized, A: MetaSized, B: Clone, B: Sized, B: MetaSized`.

The fact that metasized is in there is already enough of a problem that it is in fact not in there, and we have many hacks to avoid it in many cases which I hope to remove soon.

The new representation of this would be two bitsets:

```
A: <bitset containing Clone | Sized | MetaSized>, B: <bitset containing Clone | Sized | MetaSized>
```

The bitsets can contain the following traits:

- Sized
- MetaSized
- PointeeSized (which may be unused right now? May remove this one again in the future since it kinda represents the absence of any bounds)
- Clone
- Copy
- Debug
- Drop
- Unpin
- Eq
- Send
- Sync

Note that in the future this optimization becomes more important given that we're in the process of experimenting with more implicit trait bounds. In the future, `A: Clone` may expand to `A: Clone, A: Sized, A: MetaSized, A: Forget, A: Move, A: Destruct, A: Leak`, and with this PR those will all become just `A: <bitset containing Sized | MetaSized | Forget | Move | Destruct | Leak>`

On its own this optimization isn't necessarily worth a lot, because by storing these common predicates in a bitset, we actually stop interning them upfront. On access (and they're commonly accessed) we have to re-intern them which is terrible. The real speedup comes from knowing better *in advance* what bounds we're looking for. Using the new paramenv iterators, caller bounds iterators can say that they're looking for a predicate referencing a specific trait and for a specific self type. If we know that, we and the trait isn't any of the compressed traits, or the self type isn't the one of a specific compressed bitset, we can skip the entire set in one step. This optimization needs to offset the cost of re-interning for this optimization to be worth anything.

Note: this PR may be slightly perf-negative on rustdoc. It looks positive in the latest perf runs but that's because it's based on #165774 which improves rustdoc perf a lot. It improves some benchmark by up to 16% and this PR regresses it back by about 1%. See also #165460 (comment), #165774

> [!NOTE] 
> <table>
> <tr><td>
> 
> ![ai:0](https://github.com/jdonszelmann/0rwx.com/blob/main/static/50x50/AI-0-50x50.png?raw=true)
> 
> </td><td>
> 
> [I've not used an LLM for any part of this PR, or any other PR I make. This includes any related work like research.](https://0rwx.com/)
> 
> </td></tr>
> </table>
rust-bors Bot pushed a commit that referenced this pull request Nov 19, 2026
…illa

Compressed `ParamEnv`s for common marker traits



r? project-trait-system-refactor

This implements "compressed" `ParamEnv`s. It uses the fact that a lot of paramenvs contain many similar bounds on each generic. For example:

```rust
fn foo<A: Clone, B: Clone>(a: A, b: B) {}
```

Contains the bounds `A: Clone, A: Sized, A: MetaSized, B: Clone, B: Sized, B: MetaSized`.

The fact that metasized is in there is already enough of a problem that it is in fact not in there, and we have many hacks to avoid it in many cases which I hope to remove soon.

The new representation of this would be two bitsets:

```
A: <bitset containing Clone | Sized | MetaSized>, B: <bitset containing Clone | Sized | MetaSized>
```

The bitsets can contain the following traits:

- Sized
- MetaSized
- PointeeSized (which may be unused right now? May remove this one again in the future since it kinda represents the absence of any bounds)
- Clone
- Copy
- Debug
- Drop
- Unpin
- Eq
- Send
- Sync

Note that in the future this optimization becomes more important given that we're in the process of experimenting with more implicit trait bounds. In the future, `A: Clone` may expand to `A: Clone, A: Sized, A: MetaSized, A: Forget, A: Move, A: Destruct, A: Leak`, and with this PR those will all become just `A: <bitset containing Sized | MetaSized | Forget | Move | Destruct | Leak>`

On its own this optimization isn't necessarily worth a lot, because by storing these common predicates in a bitset, we actually stop interning them upfront. On access (and they're commonly accessed) we have to re-intern them which is terrible. The real speedup comes from knowing better *in advance* what bounds we're looking for. Using the new paramenv iterators, caller bounds iterators can say that they're looking for a predicate referencing a specific trait and for a specific self type. If we know that, we and the trait isn't any of the compressed traits, or the self type isn't the one of a specific compressed bitset, we can skip the entire set in one step. This optimization needs to offset the cost of re-interning for this optimization to be worth anything.

Note: this PR may be slightly perf-negative on rustdoc. It looks positive in the latest perf runs but that's because it's based on #165774 which improves rustdoc perf a lot. It improves some benchmark by up to 16% and this PR regresses it back by about 1%. See also #165460 (comment), #165774

> [!NOTE] 
> <table>
> <tr><td>
> 
> ![ai:0](https://github.com/jdonszelmann/0rwx.com/blob/main/static/50x50/AI-0-50x50.png?raw=true)
> 
> </td><td>
> 
> [I've not used an LLM for any part of this PR, or any other PR I make. This includes any related work like research.](https://0rwx.com/)
> 
> </td></tr>
> </table>
rust-bors Bot pushed a commit that referenced this pull request Nov 19, 2026
…illa

Compressed `ParamEnv`s for common marker traits



r? project-trait-system-refactor

This implements "compressed" `ParamEnv`s. It uses the fact that a lot of paramenvs contain many similar bounds on each generic. For example:

```rust
fn foo<A: Clone, B: Clone>(a: A, b: B) {}
```

Contains the bounds `A: Clone, A: Sized, A: MetaSized, B: Clone, B: Sized, B: MetaSized`.

The fact that metasized is in there is already enough of a problem that it is in fact not in there, and we have many hacks to avoid it in many cases which I hope to remove soon.

The new representation of this would be two bitsets:

```
A: <bitset containing Clone | Sized | MetaSized>, B: <bitset containing Clone | Sized | MetaSized>
```

The bitsets can contain the following traits:

- Sized
- MetaSized
- PointeeSized (which may be unused right now? May remove this one again in the future since it kinda represents the absence of any bounds)
- Clone
- Copy
- Debug
- Drop
- Unpin
- Eq
- Send
- Sync

Note that in the future this optimization becomes more important given that we're in the process of experimenting with more implicit trait bounds. In the future, `A: Clone` may expand to `A: Clone, A: Sized, A: MetaSized, A: Forget, A: Move, A: Destruct, A: Leak`, and with this PR those will all become just `A: <bitset containing Sized | MetaSized | Forget | Move | Destruct | Leak>`

On its own this optimization isn't necessarily worth a lot, because by storing these common predicates in a bitset, we actually stop interning them upfront. On access (and they're commonly accessed) we have to re-intern them which is terrible. The real speedup comes from knowing better *in advance* what bounds we're looking for. Using the new paramenv iterators, caller bounds iterators can say that they're looking for a predicate referencing a specific trait and for a specific self type. If we know that, we and the trait isn't any of the compressed traits, or the self type isn't the one of a specific compressed bitset, we can skip the entire set in one step. This optimization needs to offset the cost of re-interning for this optimization to be worth anything.

Note: this PR may be slightly perf-negative on rustdoc. It looks positive in the latest perf runs but that's because it's based on #165774 which improves rustdoc perf a lot. It improves some benchmark by up to 16% and this PR regresses it back by about 1%. See also #165460 (comment), #165774

> [!NOTE] 
> <table>
> <tr><td>
> 
> ![ai:0](https://github.com/jdonszelmann/0rwx.com/blob/main/static/50x50/AI-0-50x50.png?raw=true)
> 
> </td><td>
> 
> [I've not used an LLM for any part of this PR, or any other PR I make. This includes any related work like research.](https://0rwx.com/)
> 
> </td></tr>
> </table>
rust-bors Bot pushed a commit that referenced this pull request Nov 19, 2026
…illa

Compressed `ParamEnv`s for common marker traits



r? project-trait-system-refactor

This implements "compressed" `ParamEnv`s. It uses the fact that a lot of paramenvs contain many similar bounds on each generic. For example:

```rust
fn foo<A: Clone, B: Clone>(a: A, b: B) {}
```

Contains the bounds `A: Clone, A: Sized, A: MetaSized, B: Clone, B: Sized, B: MetaSized`.

The fact that metasized is in there is already enough of a problem that it is in fact not in there, and we have many hacks to avoid it in many cases which I hope to remove soon.

The new representation of this would be two bitsets:

```
A: <bitset containing Clone | Sized | MetaSized>, B: <bitset containing Clone | Sized | MetaSized>
```

The bitsets can contain the following traits:

- Sized
- MetaSized
- PointeeSized (which may be unused right now? May remove this one again in the future since it kinda represents the absence of any bounds)
- Clone
- Copy
- Debug
- Drop
- Unpin
- Eq
- Send
- Sync

Note that in the future this optimization becomes more important given that we're in the process of experimenting with more implicit trait bounds. In the future, `A: Clone` may expand to `A: Clone, A: Sized, A: MetaSized, A: Forget, A: Move, A: Destruct, A: Leak`, and with this PR those will all become just `A: <bitset containing Sized | MetaSized | Forget | Move | Destruct | Leak>`

On its own this optimization isn't necessarily worth a lot, because by storing these common predicates in a bitset, we actually stop interning them upfront. On access (and they're commonly accessed) we have to re-intern them which is terrible. The real speedup comes from knowing better *in advance* what bounds we're looking for. Using the new paramenv iterators, caller bounds iterators can say that they're looking for a predicate referencing a specific trait and for a specific self type. If we know that, we and the trait isn't any of the compressed traits, or the self type isn't the one of a specific compressed bitset, we can skip the entire set in one step. This optimization needs to offset the cost of re-interning for this optimization to be worth anything.

Note: this PR may be slightly perf-negative on rustdoc. It looks positive in the latest perf runs but that's because it's based on #165774 which improves rustdoc perf a lot. It improves some benchmark by up to 16% and this PR regresses it back by about 1%. See also #165460 (comment), #165774

> [!NOTE] 
> <table>
> <tr><td>
> 
> ![ai:0](https://github.com/jdonszelmann/0rwx.com/blob/main/static/50x50/AI-0-50x50.png?raw=true)
> 
> </td><td>
> 
> [I've not used an LLM for any part of this PR, or any other PR I make. This includes any related work like research.](https://0rwx.com/)
> 
> </td></tr>
> </table>
bjorn3 pushed a commit to rust-lang/rustc_codegen_cranelift that referenced this pull request Nov 20, 2026
…illa

Compressed `ParamEnv`s for common marker traits



r? project-trait-system-refactor

This implements "compressed" `ParamEnv`s. It uses the fact that a lot of paramenvs contain many similar bounds on each generic. For example:

```rust
fn foo<A: Clone, B: Clone>(a: A, b: B) {}
```

Contains the bounds `A: Clone, A: Sized, A: MetaSized, B: Clone, B: Sized, B: MetaSized`.

The fact that metasized is in there is already enough of a problem that it is in fact not in there, and we have many hacks to avoid it in many cases which I hope to remove soon.

The new representation of this would be two bitsets:

```
A: <bitset containing Clone | Sized | MetaSized>, B: <bitset containing Clone | Sized | MetaSized>
```

The bitsets can contain the following traits:

- Sized
- MetaSized
- PointeeSized (which may be unused right now? May remove this one again in the future since it kinda represents the absence of any bounds)
- Clone
- Copy
- Debug
- Drop
- Unpin
- Eq
- Send
- Sync

Note that in the future this optimization becomes more important given that we're in the process of experimenting with more implicit trait bounds. In the future, `A: Clone` may expand to `A: Clone, A: Sized, A: MetaSized, A: Forget, A: Move, A: Destruct, A: Leak`, and with this PR those will all become just `A: <bitset containing Sized | MetaSized | Forget | Move | Destruct | Leak>`

On its own this optimization isn't necessarily worth a lot, because by storing these common predicates in a bitset, we actually stop interning them upfront. On access (and they're commonly accessed) we have to re-intern them which is terrible. The real speedup comes from knowing better *in advance* what bounds we're looking for. Using the new paramenv iterators, caller bounds iterators can say that they're looking for a predicate referencing a specific trait and for a specific self type. If we know that, we and the trait isn't any of the compressed traits, or the self type isn't the one of a specific compressed bitset, we can skip the entire set in one step. This optimization needs to offset the cost of re-interning for this optimization to be worth anything.

Note: this PR may be slightly perf-negative on rustdoc. It looks positive in the latest perf runs but that's because it's based on rust-lang/rust#165774 which improves rustdoc perf a lot. It improves some benchmark by up to 16% and this PR regresses it back by about 1%. See also rust-lang/rust#165460 (comment), rust-lang/rust#165774

> [!NOTE] 
> <table>
> <tr><td>
> 
> ![ai:0](https://github.com/jdonszelmann/0rwx.com/blob/main/static/50x50/AI-0-50x50.png?raw=true)
> 
> </td><td>
> 
> [I've not used an LLM for any part of this PR, or any other PR I make. This includes any related work like research.](https://0rwx.com/)
> 
> </td></tr>
> </table>
yermakoffivan pushed a commit to yermakoffivan/rust-clippy that referenced this pull request Nov 20, 2026
…illa

Compressed `ParamEnv`s for common marker traits



r? project-trait-system-refactor

This implements "compressed" `ParamEnv`s. It uses the fact that a lot of paramenvs contain many similar bounds on each generic. For example:

```rust
fn foo<A: Clone, B: Clone>(a: A, b: B) {}
```

Contains the bounds `A: Clone, A: Sized, A: MetaSized, B: Clone, B: Sized, B: MetaSized`.

The fact that metasized is in there is already enough of a problem that it is in fact not in there, and we have many hacks to avoid it in many cases which I hope to remove soon.

The new representation of this would be two bitsets:

```
A: <bitset containing Clone | Sized | MetaSized>, B: <bitset containing Clone | Sized | MetaSized>
```

The bitsets can contain the following traits:

- Sized
- MetaSized
- PointeeSized (which may be unused right now? May remove this one again in the future since it kinda represents the absence of any bounds)
- Clone
- Copy
- Debug
- Drop
- Unpin
- Eq
- Send
- Sync

Note that in the future this optimization becomes more important given that we're in the process of experimenting with more implicit trait bounds. In the future, `A: Clone` may expand to `A: Clone, A: Sized, A: MetaSized, A: Forget, A: Move, A: Destruct, A: Leak`, and with this PR those will all become just `A: <bitset containing Sized | MetaSized | Forget | Move | Destruct | Leak>`

On its own this optimization isn't necessarily worth a lot, because by storing these common predicates in a bitset, we actually stop interning them upfront. On access (and they're commonly accessed) we have to re-intern them which is terrible. The real speedup comes from knowing better *in advance* what bounds we're looking for. Using the new paramenv iterators, caller bounds iterators can say that they're looking for a predicate referencing a specific trait and for a specific self type. If we know that, we and the trait isn't any of the compressed traits, or the self type isn't the one of a specific compressed bitset, we can skip the entire set in one step. This optimization needs to offset the cost of re-interning for this optimization to be worth anything.

Note: this PR may be slightly perf-negative on rustdoc. It looks positive in the latest perf runs but that's because it's based on rust-lang/rust#165774 which improves rustdoc perf a lot. It improves some benchmark by up to 16% and this PR regresses it back by about 1%. See also rust-lang/rust#165460 (comment), rust-lang/rust#165774

> [!NOTE] 
> <table>
> <tr><td>
> 
> ![ai:0](https://github.com/jdonszelmann/0rwx.com/blob/main/static/50x50/AI-0-50x50.png?raw=true)
> 
> </td><td>
> 
> [I've not used an LLM for any part of this PR, or any other PR I make. This includes any related work like research.](https://0rwx.com/)
> 
> </td></tr>
> </table>
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Nov 23, 2026
…illa

Compressed `ParamEnv`s for common marker traits



r? project-trait-system-refactor

This implements "compressed" `ParamEnv`s. It uses the fact that a lot of paramenvs contain many similar bounds on each generic. For example:

```rust
fn foo<A: Clone, B: Clone>(a: A, b: B) {}
```

Contains the bounds `A: Clone, A: Sized, A: MetaSized, B: Clone, B: Sized, B: MetaSized`.

The fact that metasized is in there is already enough of a problem that it is in fact not in there, and we have many hacks to avoid it in many cases which I hope to remove soon.

The new representation of this would be two bitsets:

```
A: <bitset containing Clone | Sized | MetaSized>, B: <bitset containing Clone | Sized | MetaSized>
```

The bitsets can contain the following traits:

- Sized
- MetaSized
- PointeeSized (which may be unused right now? May remove this one again in the future since it kinda represents the absence of any bounds)
- Clone
- Copy
- Debug
- Drop
- Unpin
- Eq
- Send
- Sync

Note that in the future this optimization becomes more important given that we're in the process of experimenting with more implicit trait bounds. In the future, `A: Clone` may expand to `A: Clone, A: Sized, A: MetaSized, A: Forget, A: Move, A: Destruct, A: Leak`, and with this PR those will all become just `A: <bitset containing Sized | MetaSized | Forget | Move | Destruct | Leak>`

On its own this optimization isn't necessarily worth a lot, because by storing these common predicates in a bitset, we actually stop interning them upfront. On access (and they're commonly accessed) we have to re-intern them which is terrible. The real speedup comes from knowing better *in advance* what bounds we're looking for. Using the new paramenv iterators, caller bounds iterators can say that they're looking for a predicate referencing a specific trait and for a specific self type. If we know that, we and the trait isn't any of the compressed traits, or the self type isn't the one of a specific compressed bitset, we can skip the entire set in one step. This optimization needs to offset the cost of re-interning for this optimization to be worth anything.

Note: this PR may be slightly perf-negative on rustdoc. It looks positive in the latest perf runs but that's because it's based on rust-lang/rust#165774 which improves rustdoc perf a lot. It improves some benchmark by up to 16% and this PR regresses it back by about 1%. See also rust-lang/rust#165460 (comment), rust-lang/rust#165774

> [!NOTE] 
> <table>
> <tr><td>
> 
> ![ai:0](https://github.com/jdonszelmann/0rwx.com/blob/main/static/50x50/AI-0-50x50.png?raw=true)
> 
> </td><td>
> 
> [I've not used an LLM for any part of this PR, or any other PR I make. This includes any related work like research.](https://0rwx.com/)
> 
> </td></tr>
> </table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

7 participants