Skip to content

Optimize indexing slices and strs with inclusive ranges#145024

Open
Kmeakin wants to merge 5 commits intorust-lang:mainfrom
Kmeakin:km/optimize-slice-index/v3
Open

Optimize indexing slices and strs with inclusive ranges#145024
Kmeakin wants to merge 5 commits intorust-lang:mainfrom
Kmeakin:km/optimize-slice-index/v3

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 str indexing related panic functions into a single function which reports the correct error depending on the arguments, as the slice 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 str up to maximum usize" it just says "end byte index {end} out of bounds". 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
Contributor

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
@Kmeakin Kmeakin force-pushed the km/optimize-slice-index/v3 branch from bd63d02 to 194cfc3 Compare August 9, 2025 22:13
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 27, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 27, 2026

☀️ Try build successful (CI)
Build commit: ddd9a12 (ddd9a12e711d4b329a39072cd3ed4cf138a43c4c, parent: b3cda168c8afd5c4240a9477f6a7f54e70e2589a)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ddd9a12): comparison URL.

Overall result: ❌ regressions - no action needed

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.

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

Max RSS (memory usage)

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

Cycles

Results (primary 9.4%, secondary 5.2%)

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

mean range count
Regressions ❌
(primary)
9.4% [2.4%, 16.9%] 12
Regressions ❌
(secondary)
5.2% [4.3%, 6.1%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 9.4% [2.4%, 16.9%] 12

Binary size

Results (secondary 0.0%)

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

Bootstrap: 472.506s -> 472.489s (-0.00%)
Artifact size: 383.60 MiB -> 383.59 MiB (-0.00%)

@rustbot rustbot removed S-waiting-on-perf Status: Waiting on a perf run to be completed. perf-regression Performance regression. labels Jan 27, 2026
@rust-bors

This comment has been minimized.

@scottmcm

This comment was marked as resolved.

@rustbot rustbot assigned Mark-Simulacrum and unassigned scottmcm Feb 1, 2026
@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 Feb 1, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 1, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Kmeakin Kmeakin force-pushed the km/optimize-slice-index/v3 branch from ab33748 to 0f259ac Compare February 2, 2026 21:15
@rustbot

This comment has been minimized.

@Kmeakin Kmeakin force-pushed the km/optimize-slice-index/v3 branch 2 times, most recently from 0b26613 to e128bf3 Compare February 5, 2026 02:24
@Kmeakin
Copy link
Contributor Author

Kmeakin commented Feb 5, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 5, 2026
Add a `codegen-llvm` test to check the number of `icmp` instrucitons
generated for each `SliceIndex` method on the various range types. This
will be updated in the next commit when `SliceIndex::get` is optimized
for `RangeInclusive`.
The checks for `self.end() == usize::MAX` and `self.end() + 1 > slice.len()`
can be replaced with `self.end() >= slice.len()`, since
`self.end() < slice.len()` implies both
`self.end() <= slice.len()` and
`self.end() < usize::MAX`.
@rust-log-analyzer

This comment has been minimized.

Replace `self.end() == usize::MAX` and `self.end() + 1 > slice.len()`
with `self.end() >= slice.len()`. Same reasoning as previous commit.

Also consolidate the str panicking functions into function.
The panic message when slicing a string with a negative length range (eg
`"abcdef"[4..3]`) is confusing: it gives the condition that failed to hold,
whilst all the other panic messages give the condition that did hold.

Before:
begin <= end (4 <= 3) when slicing `abcdef`

After:
begin > end (4 > 3) when slicing `abcdef`
Rename `into_range` to `try_into_slice_range`:
- Prepend `try_` to show that it returns `None` on error, like `try_range`
- add `_slice` to make it consistent with `into_slice_range`
@Kmeakin Kmeakin force-pushed the km/optimize-slice-index/v3 branch from e128bf3 to 75740e8 Compare February 5, 2026 02:34
@rustbot
Copy link
Collaborator

rustbot commented Feb 5, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
fmt: checked 6702 files
tidy check
tidy [rustdoc_json (src)]: `rustdoc-json-types` modified, checking format version
tidy: Skipping binary file check, read-only filesystem
tidy [style (tests)]: /checkout/tests/codegen-llvm/str-range-indexing.rs:24: line longer than 100 chars
tidy [style (tests)]: /checkout/tests/codegen-llvm/str-range-indexing.rs:25: line longer than 100 chars
tidy [style (tests)]: /checkout/tests/codegen-llvm/str-range-indexing.rs:55: line longer than 100 chars
tidy [style (tests)]: /checkout/tests/codegen-llvm/str-range-indexing.rs:83: line longer than 100 chars
tidy [style (tests)]: FAIL
tidy: The following check failed: style (tests)
Bootstrap failed while executing `--stage 2 test --skip tests --skip coverage-map --skip coverage-run --skip library --skip tidyselftest`
Command `/checkout/obj/build/aarch64-unknown-linux-gnu/stage1-tools-bin/rust-tidy --root-path=/checkout --cargo-path=/checkout/obj/build/aarch64-unknown-linux-gnu/stage0/bin/cargo --output-dir=/checkout/obj/build --concurrency=4 --npm-path=yarn` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:1612:23
Executed at: src/bootstrap/src/core/build_steps/test.rs:1365:29

Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:00:49
  local time: Thu Feb  5 02:40:36 UTC 2026
  network time: Thu, 05 Feb 2026 02:40:36 GMT
##[error]Process completed with exit code 1.
##[group]Run echo "disk usage:"

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-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.