Skip to content

Optimize indexing slices and strs with inclusive ranges #145024

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Kmeakin
Copy link
Contributor

@Kmeakin Kmeakin commented Aug 6, 2025

Instead of separately checking for end == usize::MAX and end + 1 > slice.len(), we can check for end >= slice.len(). Also consolidate all the slice indexing related panic functions into a single function which reports the correct error depending on the arguments, as the str indexing code already does.

The downside of all this is that the panic message is slightly less specific when trying to index with [..=usize::MAX]: instead of saying "attempted to index slice up to maximum usize" it just says "range end index {end} out of range for slice of length {len}". But this is a rare enough case that I think it is acceptable

@rustbot
Copy link
Collaborator

rustbot commented Aug 6, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
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 Aug 6, 2025
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@scottmcm scottmcm left a comment

Choose a reason for hiding this comment

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

As you saw from the PR build failure, if you're touching things like slice_index_order_fail you'll need to look through every mention of those in the repo -- there are a bunch of codegen tests that look for their absence, and you need to make sure that you don't make those tests useless by renaming what they're looking for.

(You probably also need to re-bless some MIR tests that include indexing.)

@jieyouxu jieyouxu 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 Aug 7, 2025
@Kmeakin Kmeakin force-pushed the km/optimize-slice-index/v3 branch from eab010d to 61bcd82 Compare August 8, 2025 00:40
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Kmeakin Kmeakin force-pushed the km/optimize-slice-index/v3 branch from 2546221 to a61fde0 Compare August 9, 2025 00:09
@rustbot
Copy link
Collaborator

rustbot commented Aug 9, 2025

The Miri subtree was changed

cc @rust-lang/miri

@rustbot

This comment has been minimized.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Aug 9, 2025
@Kmeakin Kmeakin force-pushed the km/optimize-slice-index/v3 branch from 48b325f to 87a3889 Compare August 9, 2025 00:16
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 9, 2025
@rust-log-analyzer

This comment has been minimized.

@Kmeakin Kmeakin force-pushed the km/optimize-slice-index/v3 branch from 87a3889 to bd63d02 Compare August 9, 2025 19:16
@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Aug 9, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Aug 9, 2025
Optimize indexing slices and strs with inclusive ranges
@rust-bors
Copy link

rust-bors bot commented Aug 9, 2025

⌛ Trying commit bd63d02 with merge 4249615

To cancel the try build, run the command @bors try cancel.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 9, 2025
The check for `self.end() == usize::MAX` can be combined with the `self.end() +
1 > slice.len()` check into `self.en() >= slice.len()`, since `self.end() <
slice.len()` implies both `self.end() <= slice.len()` and `self.end() <
usize::MAX`.

The tradeoff is slightly worse error reporting: previously there would be a
special panic message in the `range.end() == usize::MAX` case.
@Kmeakin Kmeakin force-pushed the km/optimize-slice-index/v3 branch from bd63d02 to 194cfc3 Compare August 9, 2025 22:13
@rust-bors
Copy link

rust-bors bot commented Aug 9, 2025

☀️ Try build successful (CI)
Build commit: 4249615 (4249615ebb47450f368b974f339d722beb35f600, parent: ca77504943887037504c7fc0b9bf06dab3910373)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4249615): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@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.6% [0.6%, 0.6%] 1
Regressions ❌
(secondary)
0.1% [0.1%, 0.2%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.8% [-2.9%, -0.0%] 9
All ❌✅ (primary) 0.6% [0.6%, 0.6%] 1

Max RSS (memory usage)

Results (primary -4.0%, secondary 2.6%)

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)
4.6% [2.0%, 5.4%] 5
Improvements ✅
(primary)
-4.0% [-4.0%, -4.0%] 1
Improvements ✅
(secondary)
-2.5% [-2.9%, -2.1%] 2
All ❌✅ (primary) -4.0% [-4.0%, -4.0%] 1

Cycles

Results (primary 2.5%, secondary -1.8%)

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

mean range count
Regressions ❌
(primary)
2.5% [2.3%, 2.7%] 2
Regressions ❌
(secondary)
2.8% [2.7%, 2.9%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.2% [-7.2%, -1.4%] 4
All ❌✅ (primary) 2.5% [2.3%, 2.7%] 2

Binary size

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

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

mean range count
Regressions ❌
(primary)
0.5% [0.5%, 0.5%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.1%, -0.0%] 18
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 35
All ❌✅ (primary) -0.0% [-0.1%, 0.5%] 19

Bootstrap: 463.988s -> 464.596s (0.13%)
Artifact size: 377.65 MiB -> 377.38 MiB (-0.07%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Aug 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. 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.

9 participants