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

Use unchecked_sub in str indexing #123561

Merged
merged 1 commit into from
Apr 7, 2024

Conversation

saethlin
Copy link
Member

@saethlin saethlin commented Apr 6, 2024

#108763 applied this logic to indexing for slices, but of course str has its own separate impl.

Found this by skimming over the codegen for https://github.com/oxidecomputer/hubris/; their dist builds enable overflow checks so the lack of unchecked_sub was producing an impossible-to-hit overflow check and also inhibiting some inlining.

r? scottmcm

@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 Apr 6, 2024
let len = self.end - self.start;
ptr::slice_from_raw_parts(ptr, len) as *const str
unsafe {
let new_len = unchecked_sub(self.end, self.start);
Copy link
Member

Choose a reason for hiding this comment

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

Does this get different codegen if you just use the public stdlib unchecked_sub?

Copy link
Member

Choose a reason for hiding this comment

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

It gets a boatload more MIR, or at least it will once #121571 lands.

Copy link
Member

Choose a reason for hiding this comment

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

(We really need a "look, nobody cares about the parameter debug info from this method" for things like that and i32:PartialOrd and …)

Copy link
Member

Choose a reason for hiding this comment

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

Ah okay! Cool then.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, but it doubles up on the precondition check.

@scottmcm
Copy link
Member

scottmcm commented Apr 6, 2024

Ah, good catch!
@bors r+ rollup=iffy (codegen tests aren't tested in CI, AFAIK)

@bors
Copy link
Contributor

bors commented Apr 6, 2024

📌 Commit 712aab7 has been approved by scottmcm

It is now in the queue for this repository.

@bors bors 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 Apr 6, 2024
@bors
Copy link
Contributor

bors commented Apr 7, 2024

⌛ Testing commit 712aab7 with merge 4e431fa...

@bors
Copy link
Contributor

bors commented Apr 7, 2024

☀️ Test successful - checks-actions
Approved by: scottmcm
Pushing 4e431fa to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 7, 2024
@bors bors merged commit 4e431fa into rust-lang:master Apr 7, 2024
12 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 7, 2024
@saethlin saethlin deleted the str-unchecked-sub-index branch April 7, 2024 15:04
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4e431fa): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

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.9% [0.9%, 0.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.5% [-0.9%, -0.4%] 7
All ❌✅ (primary) 0.9% [0.9%, 0.9%] 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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.3% [-1.3%, -1.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.3% [-1.3%, -1.3%] 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)
2.9% [2.9%, 2.9%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

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

Bootstrap: 667.899s -> 667.449s (-0.07%)
Artifact size: 318.21 MiB -> 318.47 MiB (0.08%)

@rustbot rustbot added the perf-regression Performance regression. label Apr 7, 2024
@pnkfelix
Copy link
Member

pnkfelix commented Apr 9, 2024

  • this is an improvement to the code for str::get_unchecked when overflow checks are enabled; its calling a compiler-intrinsic directly now.
  • it really doesn't make any sense that it caused any regression at all. (Perhaps this change is causing a change to inlining decisions, at least for cargo?)
  • marking as triaged.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

7 participants