Skip to content
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

Optimize EscapeIterInner #125317

Closed
wants to merge 1 commit into from
Closed

Conversation

joboet
Copy link
Member

@joboet joboet commented May 20, 2024

This optimizes EscapeIterInner by using MaybeUninit for unused array elements instead of initializing them with ascii::Char::Null.

Follow up to #124307, CC @reitermarkus

@rustbot
Copy link
Collaborator

rustbot commented May 20, 2024

r? @Nilstrieb

rustbot has assigned @Nilstrieb.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 20, 2024
@Kobzol
Copy link
Contributor

Kobzol commented May 20, 2024

@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 May 20, 2024
@bors
Copy link
Contributor

bors commented May 20, 2024

⌛ Trying commit cc56a48 with merge a2f180d...

bors added a commit to rust-lang-ci/rust that referenced this pull request May 20, 2024
Optimize `EscapeIterInner`

This optimizes `EscapeIterInner` by using `MaybeUninit` for unused array elements instead of initializing them with `ascii::Char::Null`.

Follow up to rust-lang#124307, CC `@reitermarkus`
@bors
Copy link
Contributor

bors commented May 20, 2024

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a2f180d): comparison URL.

Overall result: ❌ regressions - 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 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
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 3.0%)

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

Cycles

Results (secondary -2.2%)

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

Binary size

Results (primary -0.1%, secondary -0.0%)

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.0% [0.0%, 0.0%] 3
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 3
Improvements ✅
(primary)
-0.2% [-0.4%, -0.0%] 6
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 35
All ❌✅ (primary) -0.1% [-0.4%, 0.0%] 9

Bootstrap: 670.321s -> 670.058s (-0.04%)
Artifact size: 316.18 MiB -> 316.18 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 20, 2024
Comment on lines -122 to +103
self.data.get_unchecked(usize::from(self.alive.start)..usize::from(self.alive.end))
let data = self.data.get_unchecked(self.alive.start as usize..self.alive.end as usize);
Copy link
Member

Choose a reason for hiding this comment

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

Why did you change usize::fromto as here? I prefer the from conversions

@Noratrieb
Copy link
Member

Do you have any benchmarks or at least assembly examples to show that this makes the code faster? If you do, r=me after addressing my other question (and either changing it back or not). The unsafe here is not very complex but I don't want to merge it without any proof of at least some improvements.

// The element type ensures this is always ASCII, and thus also valid UTF-8.
data: [ascii::Char; N],
// Invariant: all elements inside the range indexed by `alive` are initialized
data: [MaybeUninit<ascii::Char>; N],
Copy link
Member

Choose a reason for hiding this comment

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

Note that, for small N, it's not obvious at all to me that this is necessarily a win. Zero-initializing a [ascii::Char; 8] for example is just a single 0_u64, and having it be MaybeUninit instead comes with the cost of it not getting noundef when it's passed around, reducing optimization possibilities for what LLVM can do with it.

For large, especially heap-allocated buffers it can help to avoid initializing them, but making all this code safety-critical (EscapeIterInner::ascii now has to be considered for understanding if it's UB, for example) is not at all obvious that it's a good tradeoff.

(We really need unsafe structs so that potential UB stops hiding in struct literals.)

Copy link

Choose a reason for hiding this comment

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

The relevant N is 10, for \u{NNNNNN}.

@Noratrieb
Copy link
Member

@rustbot author

@rustbot rustbot added 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. labels Jun 4, 2024
@clarfonthey
Copy link
Contributor

What exactly is the status of this? Is it actually worth it to make these changes?

@Dylan-DPC
Copy link
Member

@joboet any updates on this? thanks

@joboet
Copy link
Member Author

joboet commented Oct 1, 2024

On second thought I agree with Scott's assessment and am closing this.

@joboet joboet closed this Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

10 participants