Skip to content

Use a lookup table in #[derive(Debug)] for fieldless enums - #114190

Closed
DaniPopes wants to merge 1 commit into
rust-lang:masterfrom
DaniPopes:const-enum-debug
Closed

Use a lookup table in #[derive(Debug)] for fieldless enums#114190
DaniPopes wants to merge 1 commit into
rust-lang:masterfrom
DaniPopes:const-enum-debug

Conversation

@DaniPopes

Copy link
Copy Markdown
Contributor

Index into a constant array containing the Debug strings instead of matching directly.

Opening as draft since I don't know the performance implications of this, and would be nice to have a perf run done on this PR.

Closes #114106
Refs #114106 (comment), #106884

@rustbot

rustbot commented Jul 28, 2023

Copy link
Copy Markdown
Collaborator

r? @davidtwco

(rustbot has picked a reviewer for you, use r? to override)

@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 Jul 28, 2023
@compiler-errors

Copy link
Copy Markdown
Contributor

@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 Jul 28, 2023
@bors

bors commented Jul 28, 2023

Copy link
Copy Markdown
Collaborator

⌛ Trying commit 4d76b33 with merge fdbb5360b46d689c4d8ca16144ce40a45cb83099...

@bors

bors commented Jul 29, 2023

Copy link
Copy Markdown
Collaborator

☀️ Try build successful - checks-actions
Build commit: fdbb5360b46d689c4d8ca16144ce40a45cb83099 (fdbb5360b46d689c4d8ca16144ce40a45cb83099)

@rust-timer

This comment has been minimized.

@clubby789

Copy link
Copy Markdown
Contributor

I haven't looked at the implementation yet but my previous attempt at this (#109615) might be of interest

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (fdbb5360b46d689c4d8ca16144ce40a45cb83099): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.8% [0.3%, 2.6%] 37
Regressions ❌
(secondary)
1.5% [0.3%, 7.1%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.6% [-1.6%, -1.6%] 1
All ❌✅ (primary) 0.8% [0.3%, 2.6%] 37

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
3.9% [0.5%, 11.7%] 5
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-4.0% [-4.0%, -4.0%] 1
Improvements ✅
(secondary)
-2.5% [-2.5%, -2.5%] 1
All ❌✅ (primary) 2.6% [-4.0%, 11.7%] 6

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.8% [0.9%, 2.7%] 7
Regressions ❌
(secondary)
2.0% [1.3%, 2.9%] 5
Improvements ✅
(primary)
-3.1% [-5.1%, -1.3%] 6
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.5% [-5.1%, 2.7%] 13

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.3% [0.0%, 1.9%] 67
Regressions ❌
(secondary)
1.5% [0.1%, 3.8%] 16
Improvements ✅
(primary)
-0.2% [-0.4%, -0.1%] 5
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.3% [-0.4%, 1.9%] 72

Bootstrap: 649.861s -> 651.137s (0.20%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jul 29, 2023
Comment on lines +206 to +210
/// const { ["A", "B", "C"] }[match self {
/// Fieldless::A => 0usize,
/// Fieldless::B => 1usize,
/// Fieldless::C => 2usize,
/// }],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have been looking into a similar case recently as noted in #114106 (comment), and part of the problem might be the match statement itself.

For data-less enums with dense/contiguous discriminants, you should be able to just use something like *self as u32 to use the discriminant directly as an index instead of via the match roundtrip.

@davidtwco

Copy link
Copy Markdown
Member

I don't think the perf results warrant landing this as-is, if you have other ideas that you'd like to try then we can re-run the benchmarking with those :)

@DaniPopes

Copy link
Copy Markdown
Contributor Author

Closing given negative perf results and I don't really have any idea on how to improve this.

@DaniPopes DaniPopes closed this Aug 2, 2023
rust-bors Bot pushed a commit that referenced this pull request Aug 2, 2026
Implement `Debug` for C-like enums with a concatenated string



Fixes: #133945 

Related to: #88793 #114106

Continuation of: #109615 #114190 

This optimizes the expansion of `derive(Debug)` for C-like enums with 10 or more variants to:
```rust
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
    const __NAMES: &str =
        "AAAAABBBBCCDDDDDDDDEFFFFFFFFFFFFFGGGGGGHatsuneIIIIIIIJJJJJJJJJ";
    const __OFFSET: [usize; 11] =
        [0usize, 5usize, 9usize, 11usize, 19usize, 20usize, 33usize,
                39usize, 46usize, 53usize, 62usize];
    let __d = ::core::intrinsics::discriminant_value(self) as usize;
    ::core::fmt::Formatter::debug_c_like_enum_write_str(f, __NAMES,
        &__OFFSET, __d)
}
```

The number 10 was decided based on both [the rustc perf result](#155452 (comment)) and the results from the dedicated perf tool for this scenario:

<img width="1920" height="1280" alt="chart-link" src="https://github.com/user-attachments/assets/1327ee63-ce05-482b-a1fd-c56e919250ea" />
<img width="1920" height="1280" alt="chart-obj" src="https://github.com/user-attachments/assets/1cb831d3-c9dc-469f-b87d-03cf633f8335" />


See https://codeberg.org/makai410/debug-fmt-perf for more details.

r? @ghost (I want to see the perf first)
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Aug 3, 2026
Implement `Debug` for C-like enums with a concatenated string



Fixes: rust-lang/rust#133945 

Related to: rust-lang/rust#88793 rust-lang/rust#114106

Continuation of: rust-lang/rust#109615 rust-lang/rust#114190 

This optimizes the expansion of `derive(Debug)` for C-like enums with 10 or more variants to:
```rust
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
    const __NAMES: &str =
        "AAAAABBBBCCDDDDDDDDEFFFFFFFFFFFFFGGGGGGHatsuneIIIIIIIJJJJJJJJJ";
    const __OFFSET: [usize; 11] =
        [0usize, 5usize, 9usize, 11usize, 19usize, 20usize, 33usize,
                39usize, 46usize, 53usize, 62usize];
    let __d = ::core::intrinsics::discriminant_value(self) as usize;
    ::core::fmt::Formatter::debug_c_like_enum_write_str(f, __NAMES,
        &__OFFSET, __d)
}
```

The number 10 was decided based on both [the rustc perf result](rust-lang/rust#155452 (comment)) and the results from the dedicated perf tool for this scenario:

<img width="1920" height="1280" alt="chart-link" src="https://github.com/user-attachments/assets/1327ee63-ce05-482b-a1fd-c56e919250ea" />
<img width="1920" height="1280" alt="chart-obj" src="https://github.com/user-attachments/assets/1cb831d3-c9dc-469f-b87d-03cf633f8335" />


See https://codeberg.org/makai410/debug-fmt-perf for more details.

r? @ghost (I want to see the perf first)
WhySoBad pushed a commit to WhySoBad/miri that referenced this pull request Aug 5, 2026
Implement `Debug` for C-like enums with a concatenated string



Fixes: rust-lang/rust#133945 

Related to: rust-lang/rust#88793 rust-lang/rust#114106

Continuation of: rust-lang/rust#109615 rust-lang/rust#114190 

This optimizes the expansion of `derive(Debug)` for C-like enums with 10 or more variants to:
```rust
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
    const __NAMES: &str =
        "AAAAABBBBCCDDDDDDDDEFFFFFFFFFFFFFGGGGGGHatsuneIIIIIIIJJJJJJJJJ";
    const __OFFSET: [usize; 11] =
        [0usize, 5usize, 9usize, 11usize, 19usize, 20usize, 33usize,
                39usize, 46usize, 53usize, 62usize];
    let __d = ::core::intrinsics::discriminant_value(self) as usize;
    ::core::fmt::Formatter::debug_c_like_enum_write_str(f, __NAMES,
        &__OFFSET, __d)
}
```

The number 10 was decided based on both [the rustc perf result](rust-lang/rust#155452 (comment)) and the results from the dedicated perf tool for this scenario:

<img width="1920" height="1280" alt="chart-link" src="https://github.com/user-attachments/assets/1327ee63-ce05-482b-a1fd-c56e919250ea" />
<img width="1920" height="1280" alt="chart-obj" src="https://github.com/user-attachments/assets/1cb831d3-c9dc-469f-b87d-03cf633f8335" />


See https://codeberg.org/makai410/debug-fmt-perf for more details.

r? @ghost (I want to see the perf first)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. 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.

Poor codegen for Debug impls of C-like enums with many variants

8 participants