Skip to content

Further optimize SliceIndex<str> impl for Range<usize>#156119

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Kmeakin:km/optimize-str-index
Jun 8, 2026
Merged

Further optimize SliceIndex<str> impl for Range<usize>#156119
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Kmeakin:km/optimize-str-index

Conversation

@Kmeakin

@Kmeakin Kmeakin commented May 3, 2026

Copy link
Copy Markdown
Contributor

We can shave a further 2 icmps by inlining is_char_boundary and rearranging the checks.

Follow up to #145024

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 3, 2026
@rustbot

rustbot commented May 3, 2026

Copy link
Copy Markdown
Collaborator

r? @scottmcm

rustbot has assigned @scottmcm.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, jhpratt, nia-e, scottmcm

@Kmeakin Kmeakin force-pushed the km/optimize-str-index branch from 492232c to 3693b41 Compare May 3, 2026 21:00
@rust-log-analyzer

This comment has been minimized.

@Kmeakin Kmeakin force-pushed the km/optimize-str-index branch from 3693b41 to 04d9b91 Compare May 3, 2026 21:11
@rust-log-analyzer

This comment has been minimized.

We can shave a further 2 `icmp`s by inlining `is_char_boundary` and rearranging
the checks.
@Kmeakin Kmeakin force-pushed the km/optimize-str-index branch from 04d9b91 to ef9eff9 Compare May 3, 2026 23:12
@scottmcm

Copy link
Copy Markdown
Member

I'm leaving libs reviews.
@rustbot reroll

@rustbot rustbot assigned Mark-Simulacrum and unassigned scottmcm May 18, 2026
@Mark-Simulacrum

Copy link
Copy Markdown
Member

@bors r+ rollup=never

@rust-bors

rust-bors Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

📌 Commit ef9eff9 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 Jun 6, 2026
@jhpratt

jhpratt commented Jun 7, 2026

Copy link
Copy Markdown
Member

Moving ahead of prioritized but rollup-able PRs

@bors p=2

@jhpratt

jhpratt commented Jun 7, 2026

Copy link
Copy Markdown
Member

rolling back priority

@bors p=0

@JonathanBrouwer

JonathanBrouwer commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

@Mark-Simulacrum Is the reason this is not rollupable perf? Let's measure that
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 7, 2026
rust-bors Bot pushed a commit that referenced this pull request Jun 7, 2026
Further optimize `SliceIndex<str>` impl for `Range<usize>`
@rust-bors

rust-bors Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 4e4cf03 (4e4cf031367bb469ae04066e79966dc71acd2123, parent: 61d7280f3c4c63fa24c56bdaa9a446151b5a30dc)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (4e4cf03): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing 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
Improvements ✅
(primary)
-0.6% [-0.6%, -0.6%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.6% [-0.6%, -0.6%] 1

Max RSS (memory usage)

This perf run didn't have relevant results for this metric.

Cycles

Results (secondary -2.8%)

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

Binary size

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

Bootstrap: 514.705s -> 515.168s (0.09%)
Artifact size: 401.25 MiB -> 401.31 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 7, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Contributor

The perf improvement looks real to me, but is relatively small

The queue is a little large atm so let's do
@bors rollup=iffy

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 7, 2026
…ark-Simulacrum

Further optimize `SliceIndex<str>` impl for `Range<usize>`

We can shave a further 2 `icmp`s by inlining `is_char_boundary` and rearranging the checks.

Follow up to rust-lang#145024
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 7, 2026
…ark-Simulacrum

Further optimize `SliceIndex<str>` impl for `Range<usize>`

We can shave a further 2 `icmp`s by inlining `is_char_boundary` and rearranging the checks.

Follow up to rust-lang#145024
rust-bors Bot pushed a commit that referenced this pull request Jun 7, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #157447 (Move cross crate tests into the appropriate folder)
 - #145108 (Resolver: Batched Import Resolution)
 - #156119 (Further optimize `SliceIndex<str>` impl for `Range<usize>`)
 - #157289 (Add infallible primitive type lookups to template arg resolver)
 - #157540 (Cleanup and optimize `render_impls`)
 - #157543 (Reorganize `tests/ui/issues` [5/N])
 - #156188 (riscv: promote d, e, and f target_features to CfgStableToggleUnstable)
 - #157323 (Document Repeat::last panic behavior)
 - #157545 (Suggest using comma to separate valid attribute list items)
 - #157559 (chore: Update annotate-snippets to 0.12.16)
rust-bors Bot pushed a commit that referenced this pull request Jun 7, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #157447 (Move cross crate tests into the appropriate folder)
 - #145108 (Resolver: Batched Import Resolution)
 - #156119 (Further optimize `SliceIndex<str>` impl for `Range<usize>`)
 - #157289 (Add infallible primitive type lookups to template arg resolver)
 - #157540 (Cleanup and optimize `render_impls`)
 - #157543 (Reorganize `tests/ui/issues` [5/N])
 - #156188 (riscv: promote d, e, and f target_features to CfgStableToggleUnstable)
 - #157323 (Document Repeat::last panic behavior)
 - #157545 (Suggest using comma to separate valid attribute list items)
 - #157559 (chore: Update annotate-snippets to 0.12.16)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 7, 2026
…ark-Simulacrum

Further optimize `SliceIndex<str>` impl for `Range<usize>`

We can shave a further 2 `icmp`s by inlining `is_char_boundary` and rearranging the checks.

Follow up to rust-lang#145024
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 7, 2026
…ark-Simulacrum

Further optimize `SliceIndex<str>` impl for `Range<usize>`

We can shave a further 2 `icmp`s by inlining `is_char_boundary` and rearranging the checks.

Follow up to rust-lang#145024
rust-bors Bot pushed a commit that referenced this pull request Jun 7, 2026
Rollup of 25 pull requests

Successful merges:

 - #157447 (Move cross crate tests into the appropriate folder)
 - #145108 (Resolver: Batched Import Resolution)
 - #156119 (Further optimize `SliceIndex<str>` impl for `Range<usize>`)
 - #157224 (Manually unroll loop in `str::floor_char_boundary`)
 - #157289 (Add infallible primitive type lookups to template arg resolver)
 - #157540 (Cleanup and optimize `render_impls`)
 - #157444 (Couple of work product cleanups)
 - #157543 (Reorganize `tests/ui/issues` [5/N])
 - #153513 (Syntactically reject equality predicates)
 - #155797 (LineWriter: cap write_vectored newline scan to avoid quadratic write_all_vectored)
 - #156155 (macros: report unbound metavariables directly)
 - #156188 (riscv: promote d, e, and f target_features to CfgStableToggleUnstable)
 - #156666 (Clarify meaning of ranges in pointer offset docs)
 - #157078 (Document equivalence of `highest_one` and `ilog2` methods on integers)
 - #157129 (ci: update download-artifact action to v8)
 - #157169 (triagebot: Update messages to direct changes to appropriate repositories)
 - #157323 (Document Repeat::last panic behavior)
 - #157370 (Clarify MaybeUninit::zeroed padding docs)
 - #157399 (Silence llbc's output by default to prevent rustc's linker output warning)
 - #157500 (Improve documentation of `align_of` and `Alignment`.)
 - #157545 (Suggest using comma to separate valid attribute list items)
 - #157559 (chore: Update annotate-snippets to 0.12.16)
 - #157560 (In `copy_nonoverlapping`, use `mul nuw nsw` to compute the byte size)
 - #157580 (Importing suggestion reported twice when reporting privacy error)
 - #157581 (Test fixup)
rust-bors Bot pushed a commit that referenced this pull request Jun 7, 2026
Rollup of 25 pull requests

Successful merges:

 - #157447 (Move cross crate tests into the appropriate folder)
 - #145108 (Resolver: Batched Import Resolution)
 - #156119 (Further optimize `SliceIndex<str>` impl for `Range<usize>`)
 - #157224 (Manually unroll loop in `str::floor_char_boundary`)
 - #157289 (Add infallible primitive type lookups to template arg resolver)
 - #157540 (Cleanup and optimize `render_impls`)
 - #157444 (Couple of work product cleanups)
 - #157543 (Reorganize `tests/ui/issues` [5/N])
 - #153513 (Syntactically reject equality predicates)
 - #155797 (LineWriter: cap write_vectored newline scan to avoid quadratic write_all_vectored)
 - #156155 (macros: report unbound metavariables directly)
 - #156188 (riscv: promote d, e, and f target_features to CfgStableToggleUnstable)
 - #156666 (Clarify meaning of ranges in pointer offset docs)
 - #157078 (Document equivalence of `highest_one` and `ilog2` methods on integers)
 - #157129 (ci: update download-artifact action to v8)
 - #157169 (triagebot: Update messages to direct changes to appropriate repositories)
 - #157323 (Document Repeat::last panic behavior)
 - #157370 (Clarify MaybeUninit::zeroed padding docs)
 - #157399 (Silence llbc's output by default to prevent rustc's linker output warning)
 - #157500 (Improve documentation of `align_of` and `Alignment`.)
 - #157545 (Suggest using comma to separate valid attribute list items)
 - #157559 (chore: Update annotate-snippets to 0.12.16)
 - #157560 (In `copy_nonoverlapping`, use `mul nuw nsw` to compute the byte size)
 - #157580 (Importing suggestion reported twice when reporting privacy error)
 - #157581 (Test fixup)
@rust-bors rust-bors Bot merged commit 0efcd77 into rust-lang:main Jun 8, 2026
12 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 8, 2026
github-actions Bot pushed a commit to rust-lang/stdarch that referenced this pull request Jun 8, 2026
Rollup of 25 pull requests

Successful merges:

 - rust-lang/rust#157447 (Move cross crate tests into the appropriate folder)
 - rust-lang/rust#145108 (Resolver: Batched Import Resolution)
 - rust-lang/rust#156119 (Further optimize `SliceIndex<str>` impl for `Range<usize>`)
 - rust-lang/rust#157224 (Manually unroll loop in `str::floor_char_boundary`)
 - rust-lang/rust#157289 (Add infallible primitive type lookups to template arg resolver)
 - rust-lang/rust#157540 (Cleanup and optimize `render_impls`)
 - rust-lang/rust#157444 (Couple of work product cleanups)
 - rust-lang/rust#157543 (Reorganize `tests/ui/issues` [5/N])
 - rust-lang/rust#153513 (Syntactically reject equality predicates)
 - rust-lang/rust#155797 (LineWriter: cap write_vectored newline scan to avoid quadratic write_all_vectored)
 - rust-lang/rust#156155 (macros: report unbound metavariables directly)
 - rust-lang/rust#156188 (riscv: promote d, e, and f target_features to CfgStableToggleUnstable)
 - rust-lang/rust#156666 (Clarify meaning of ranges in pointer offset docs)
 - rust-lang/rust#157078 (Document equivalence of `highest_one` and `ilog2` methods on integers)
 - rust-lang/rust#157129 (ci: update download-artifact action to v8)
 - rust-lang/rust#157169 (triagebot: Update messages to direct changes to appropriate repositories)
 - rust-lang/rust#157323 (Document Repeat::last panic behavior)
 - rust-lang/rust#157370 (Clarify MaybeUninit::zeroed padding docs)
 - rust-lang/rust#157399 (Silence llbc's output by default to prevent rustc's linker output warning)
 - rust-lang/rust#157500 (Improve documentation of `align_of` and `Alignment`.)
 - rust-lang/rust#157545 (Suggest using comma to separate valid attribute list items)
 - rust-lang/rust#157559 (chore: Update annotate-snippets to 0.12.16)
 - rust-lang/rust#157560 (In `copy_nonoverlapping`, use `mul nuw nsw` to compute the byte size)
 - rust-lang/rust#157580 (Importing suggestion reported twice when reporting privacy error)
 - rust-lang/rust#157581 (Test fixup)
asukaminato0721 pushed a commit to asukaminato0721/rust-analyzer that referenced this pull request Jun 8, 2026
Rollup of 25 pull requests

Successful merges:

 - rust-lang/rust#157447 (Move cross crate tests into the appropriate folder)
 - rust-lang/rust#145108 (Resolver: Batched Import Resolution)
 - rust-lang/rust#156119 (Further optimize `SliceIndex<str>` impl for `Range<usize>`)
 - rust-lang/rust#157224 (Manually unroll loop in `str::floor_char_boundary`)
 - rust-lang/rust#157289 (Add infallible primitive type lookups to template arg resolver)
 - rust-lang/rust#157540 (Cleanup and optimize `render_impls`)
 - rust-lang/rust#157444 (Couple of work product cleanups)
 - rust-lang/rust#157543 (Reorganize `tests/ui/issues` [5/N])
 - rust-lang/rust#153513 (Syntactically reject equality predicates)
 - rust-lang/rust#155797 (LineWriter: cap write_vectored newline scan to avoid quadratic write_all_vectored)
 - rust-lang/rust#156155 (macros: report unbound metavariables directly)
 - rust-lang/rust#156188 (riscv: promote d, e, and f target_features to CfgStableToggleUnstable)
 - rust-lang/rust#156666 (Clarify meaning of ranges in pointer offset docs)
 - rust-lang/rust#157078 (Document equivalence of `highest_one` and `ilog2` methods on integers)
 - rust-lang/rust#157129 (ci: update download-artifact action to v8)
 - rust-lang/rust#157169 (triagebot: Update messages to direct changes to appropriate repositories)
 - rust-lang/rust#157323 (Document Repeat::last panic behavior)
 - rust-lang/rust#157370 (Clarify MaybeUninit::zeroed padding docs)
 - rust-lang/rust#157399 (Silence llbc's output by default to prevent rustc's linker output warning)
 - rust-lang/rust#157500 (Improve documentation of `align_of` and `Alignment`.)
 - rust-lang/rust#157545 (Suggest using comma to separate valid attribute list items)
 - rust-lang/rust#157559 (chore: Update annotate-snippets to 0.12.16)
 - rust-lang/rust#157560 (In `copy_nonoverlapping`, use `mul nuw nsw` to compute the byte size)
 - rust-lang/rust#157580 (Importing suggestion reported twice when reporting privacy error)
 - rust-lang/rust#157581 (Test fixup)
@Kmeakin Kmeakin deleted the km/optimize-str-index branch June 8, 2026 13:31
return false;
}

if start == slice.len() {

@avjewe avjewe Jun 11, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
if start == slice.len() {
if start == end {

Any empty slice in range should be fine, not just at the end, and this will save an instruction or two for empty slices.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think this would be valid: slicing in the middle of a Unicode character is invalid, even if the slice is zero length

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I can see how that could be argued.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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