Skip to content

Implement Debug for C-like enums with an array #109615

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

Closed
wants to merge 2 commits into from

Conversation

clubby789
Copy link
Contributor

@clubby789 clubby789 commented Mar 25, 2023

Implement Debug for C-like, automatic discriminant, Copy enums with ["A", "B", "C"][*self as usize]
Points to improve:

  • Possibly using unsafe to avoid a needless bounds check in MIR (should be eliminated, but seems a shame to add a redundant branch)
  • Handling enums with an explicit repr (currently, only Copy enums work
  • Handle enums with discriminants like Enum Foo { A = 0, B = 1, C = 2 } (might not be worth it for the extra complexity)

cc @nnethercote

@rustbot
Copy link
Collaborator

rustbot commented Mar 25, 2023

r? @oli-obk

(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 Mar 25, 2023
@Noratrieb
Copy link
Member

@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 Mar 25, 2023
@bors
Copy link
Collaborator

bors commented Mar 25, 2023

⌛ Trying commit f1021ed with merge 2d011dde8c273334d2eb4442d82a866ca18a0bf3...

@nnethercote
Copy link
Contributor

What's the motivation here? Is the generated code expected to be faster and/or more compact? It's possible that LLVM will do some kind of conversion like this anyway, I'd be interested to see the generated code.

@clubby789
Copy link
Contributor Author

clubby789 commented Mar 26, 2023

#106884 previously improved perf surprisingly, so I'm interested to see if this has a similar effect, as it shrinks the generated MIR more.
The generated code is slightly worse (at least for Enum A { A, B, C}) as it constructs the array on the stack, but using an inline const block for the array results in equivalent code (a regular const item works too but makes the code significantly more complex). I can push that version once the perf is done

@bors
Copy link
Collaborator

bors commented Mar 26, 2023

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (2d011dde8c273334d2eb4442d82a866ca18a0bf3): comparison URL.

Overall result: ❌✅ regressions and improvements - 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.7% [0.7%, 0.7%] 1
Regressions ❌
(secondary)
1.0% [1.0%, 1.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.5% [-0.6%, -0.3%] 7
All ❌✅ (primary) 0.7% [0.7%, 0.7%] 1

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)
1.2% [0.8%, 1.5%] 2
Regressions ❌
(secondary)
2.0% [0.6%, 9.3%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.8% [-1.0%, -0.7%] 3
All ❌✅ (primary) 1.2% [0.8%, 1.5%] 2

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.1% [1.0%, 1.3%] 2
Regressions ❌
(secondary)
0.5% [0.5%, 0.6%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.7% [-0.7%, -0.7%] 1
All ❌✅ (primary) 1.1% [1.0%, 1.3%] 2

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Mar 26, 2023
@clubby789
Copy link
Contributor Author

Changed to emit

         ::core::fmt::Formatter::write_str(f, {
             const __NAMES: [&str; 3] = ["A", "B", "C"];
             __NAMES[::core::intrinsics::discriminant_value(self) as usize]
         })

This now works on non-Copy enums, and brings codegen in line with the current behaviour.

@Noratrieb
Copy link
Member

@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 Mar 26, 2023
@bors
Copy link
Collaborator

bors commented Mar 26, 2023

⌛ Trying commit 41140f2 with merge cacf0a7b7eccd3ec43e77c76de0b6c250a24de73...

@bors
Copy link
Collaborator

bors commented Mar 26, 2023

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (cacf0a7b7eccd3ec43e77c76de0b6c250a24de73): comparison URL.

Overall result: no relevant changes - no 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.

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

Instruction count

This benchmark run did not return any relevant results for this metric.

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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.0% [-1.0%, -1.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.0% [-1.0%, -1.0%] 1

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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.3% [-2.3%, -2.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.3% [-2.3%, -2.3%] 1

@rustbot rustbot removed S-waiting-on-perf Status: Waiting on a perf run to be completed. perf-regression Performance regression. labels Mar 26, 2023
@clubby789
Copy link
Contributor Author

Since there seems not to be much improvement in perf now, and the generated code is roughly equivalent, this probably isn't worth keeping open :/

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.

7 participants