Skip to content

Reland debug impl for fieldless enums with concatenated string without using discriminant_value - #162535

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
panstromek:reland-debug-opt
Oct 22, 2026
Merged

rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
panstromek:reland-debug-opt

Conversation

@panstromek

@panstromek panstromek commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

relands part of #155452
partially reverts #162164
fixes only a part of #133945

This implementation avoids the problem in the original PR by using simple match instead of indexing by discriminant_value. This avoids the cost of many small strings, generates less MIR and reduces borrowck cost, but it doesn't avoid the match checking costs (obviously).

It uses slightly different strategy to avoid more MIR costs - it doesn't generate array of indexes. It maps the enum variant directly to the start index. Names are separated by space, so the outlined function figures out the end index based on that.

Some more measurements and reasoning behind this implementation is in this comment

This PR history contains various experiments, #162535 (comment) is the perf run for the final version, which also includes results for the enum-stress, which is a benchmark that contains a stress test for this case.

I also noticed there's not really a test testing the output of some of these derives (especially big ones), so I added a case to an existing test that covers this.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 9, 2026
@panstromek

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 Sep 9, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 9, 2026
Reland debug impl for fieldless enums with concatenated string without using discriminant_value
@rust-bors

rust-bors Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6ff0c29 (6ff0c293700f750741ca037a07d54262de940377)
Base parent: eca445e (eca445e5ae4a6679cc27d3a09106ce245e13a5a6)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (6ff0c29): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@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.1% [0.1%, 0.1%] 1
Regressions ❌
(secondary)
0.3% [0.2%, 0.5%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [0.1%, 0.1%] 1

Max RSS (memory usage)

Results (primary 0.8%, secondary -0.0%)

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

mean range count
Regressions ❌
(primary)
2.2% [0.5%, 5.2%] 4
Regressions ❌
(secondary)
3.8% [3.8%, 3.8%] 1
Improvements ✅
(primary)
-2.2% [-2.4%, -2.0%] 2
Improvements ✅
(secondary)
-1.9% [-2.4%, -1.5%] 2
All ❌✅ (primary) 0.8% [-2.4%, 5.2%] 6

Cycles

Results (primary 1.0%, secondary 5.9%)

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

mean range count
Regressions ❌
(primary)
1.5% [0.4%, 3.5%] 7
Regressions ❌
(secondary)
5.9% [5.9%, 5.9%] 1
Improvements ✅
(primary)
-0.7% [-0.7%, -0.6%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.0% [-0.7%, 3.5%] 9

Binary size

Results (primary 0.1%, secondary 4.2%)

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

mean range count
Regressions ❌
(primary)
0.2% [0.0%, 1.7%] 39
Regressions ❌
(secondary)
4.2% [0.0%, 13.7%] 21
Improvements ✅
(primary)
-0.4% [-0.7%, -0.0%] 16
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [-0.7%, 1.7%] 55

Bootstrap: 481.923s -> 481.101s (-0.17%)
Artifact size: 403.28 MiB -> 403.16 MiB (-0.03%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 9, 2026
@panstromek

Copy link
Copy Markdown
Contributor Author

Replacing discriminant_value with match => variant_index sadly doesn't help much. The generated MIR looks somewhat better than the default match => &str variant (there's less code in each arms basic block), but without the O(n) reduction in basic blocks, it doesn't seem to be worth it.

@rust-bors

This comment has been minimized.

@panstromek

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 Oct 8, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 8, 2026
Reland debug impl for fieldless enums with concatenated string without using discriminant_value
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 499edf9 failed: CI. Failed jobs:

@rust-log-analyzer

This comment has been minimized.

@panstromek

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 Oct 8, 2026
Reland debug impl for fieldless enums with concatenated string without using discriminant_value
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 3b82c9e (3b82c9e09a3707bda85dc9eb54201ae70eb921c8)
Base parent: 65efa19 (65efa194201c67ba29b7384255a534d61c2aa3cb)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (3b82c9e): 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.6% [0.5%, 0.7%] 2
Improvements ✅
(primary)
-0.3% [-0.6%, -0.1%] 14
Improvements ✅
(secondary)
-0.4% [-0.8%, -0.1%] 59
All ❌✅ (primary) -0.3% [-0.6%, -0.1%] 14

Max RSS (memory usage)

Results (primary 0.6%, secondary -1.5%)

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

mean range count
Regressions ❌
(primary)
1.9% [0.7%, 4.3%] 5
Regressions ❌
(secondary)
2.1% [2.1%, 2.1%] 1
Improvements ✅
(primary)
-2.4% [-3.5%, -1.3%] 2
Improvements ✅
(secondary)
-2.4% [-3.6%, -1.1%] 4
All ❌✅ (primary) 0.6% [-3.5%, 4.3%] 7

Cycles

Results (secondary -2.5%)

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)
-2.5% [-2.9%, -2.2%] 2
All ❌✅ (primary) - - 0

Binary size

Results (primary -0.1%, secondary 3.9%)

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

mean range count
Regressions ❌
(primary)
1.2% [1.1%, 1.2%] 4
Regressions ❌
(secondary)
3.9% [0.6%, 8.0%] 13
Improvements ✅
(primary)
-0.4% [-0.7%, -0.0%] 18
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.7%, 1.2%] 22

Bootstrap: 485.904s -> 487.919s (0.41%)
Artifact size: 406.44 MiB -> 406.38 MiB (-0.02%)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 21, 2026
@nnethercote

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 11200f7 has been tentatively approved by nnethercote

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

🌲 The tree is currently closed for pull requests below priority 5. This pull request will be tested once the tree is reopened.

Reason for tree closure: github outage

@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. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 21, 2026
@rust-bors

This comment has been minimized.

panstromek and others added 4 commits October 21, 2026 21:57
This reduces compile time for large enums.

Co-authored-by: Makai <m4kai410@gmail.com>
This derive is only used for large nums, so it doesn't make sense to use small enum as an example
@rustbot

rustbot commented Oct 21, 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.

@panstromek

Copy link
Copy Markdown
Contributor Author

rebased

@rustbot ready

@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 Oct 21, 2026
@nnethercote

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 0076e09 has been tentatively approved by nnethercote

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

🌲 The tree is currently closed for pull requests below priority 5. This pull request will be tested once the tree is reopened.

Reason for tree closure: github outage

@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-review Status: Awaiting review from the assignee but also interested parties. labels Oct 21, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 21, 2026
Rollup of 6 pull requests

Successful merges:

 - #162535 (Reland debug impl for fieldless enums with concatenated string without using discriminant_value)
 - #164060 (Add hightlight to acceptable characters in macro rules)
 - #164583 (Run -Cpasses during OptimizerLastEP)
 - #164293 (Tweak missing fields error wording)
 - #164563 (Removes unwanted backticks from slice::as_ascii documentation.)
 - #164683 (Add -Zasync-panic flag)
@rust-bors
rust-bors Bot merged commit e3620d0 into rust-lang:main Oct 22, 2026
14 checks passed
rust-bors Bot pushed a commit that referenced this pull request Oct 22, 2026
Rollup merge of #162535 - panstromek:reland-debug-opt, r=nnethercote

Reland debug impl for fieldless enums with concatenated string without using discriminant_value

relands part of #155452
partially reverts #162164
fixes only a part of #133945

This implementation avoids the problem in the original PR by using simple match instead of indexing by `discriminant_value`. This avoids the cost of many small strings, generates less MIR and reduces borrowck cost, but it doesn't avoid the match checking costs (obviously).

It uses slightly different strategy to avoid more MIR costs - it doesn't generate array of indexes. It maps the enum variant directly to the start index. Names are separated by space, so the outlined function figures out the end index based on that.

Some more measurements and reasoning behind this implementation is in [this comment](#162535 (comment))

This PR history contains various experiments, #162535 (comment) is the perf run for the final version, which also includes results for the `enum-stress`, which is a benchmark that contains a stress test for this case.

I also noticed there's not really a test testing the output of some of these derives (especially big ones), so I added a case to an existing test that covers this.
@rustbot rustbot added this to the 1.101.0 milestone Oct 22, 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 (fd1f60f): 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.3% [0.3%, 0.3%] 1
Improvements ✅
(primary)
-0.3% [-0.4%, -0.1%] 3
Improvements ✅
(secondary)
-4.7% [-10.4%, -0.0%] 14
All ❌✅ (primary) -0.3% [-0.4%, -0.1%] 3

Max RSS (memory usage)

Results (secondary -60.7%)

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.4% [0.4%, 0.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-68.4% [-91.1%, -1.8%] 8
All ❌✅ (primary) - - 0

Cycles

Results (secondary -14.0%)

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)
2.3% [1.6%, 3.5%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-20.1% [-28.5%, -1.6%] 8
All ❌✅ (primary) - - 0

Binary size

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

Artifact size: 406.53 MiB -> 406.31 MiB (-0.05%)

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. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants