Skip to content

slice_split_once: bounds check optimization note#158282

Open
ArhanChaudhary wants to merge 1 commit into
rust-lang:mainfrom
ArhanChaudhary:unchecked-subslice
Open

slice_split_once: bounds check optimization note#158282
ArhanChaudhary wants to merge 1 commit into
rust-lang:mainfrom
ArhanChaudhary:unchecked-subslice

Conversation

@ArhanChaudhary

@ArhanChaudhary ArhanChaudhary commented Jun 23, 2026

Copy link
Copy Markdown

Tracking issue: #112811

Use unchecked sub-slicing operations for <T>::split_once and <T>::rsplit_once.
Note that unchecked sub-slicing operations are equivalent to the current checked sub-slicing operations.

@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 Jun 23, 2026
@rustbot

rustbot commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @jhpratt (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from Darksonn, JohnTitor, Mark-Simulacrum, clarfonthey, jhpratt

@rust-log-analyzer

This comment has been minimized.

Comment thread library/core/src/slice/mod.rs Outdated

@jhpratt jhpratt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It shouldn't be necessary to explicitly state the start and end for these (in either location).

View changes since this review

Comment thread library/core/src/slice/mod.rs Outdated
Comment thread library/core/src/slice/mod.rs Outdated
Comment thread library/core/src/slice/mod.rs Outdated
Comment on lines +2532 to +2537
let index = self.iter().position(pred)?;
Some((&self[..index], &self[index + 1..]))
// SAFETY: `index` is inside `self`
let left = unsafe { self.get_unchecked(0..index) };
// SAFETY: `index` is inside `self`
let right = unsafe { self.get_unchecked(index + 1..self.len()) };
Some((left, right))

@tgross35 tgross35 Jun 23, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I just did a quick check: both versions actually produce identical code at -Copt-level=1 https://rust.godbolt.org/z/n8cTdY4rT. So I think you can actually just leave the code as-is to save the unsafe, but instead add a comment saying the bounds check is trivially optimized out. Unless @vmmon happens to remember more about the circumstances of #112811 (comment).

Sorry about the back and forth here!

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(PR also needs a squash and commit message update before merge)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

squashing can be done with bors now :)

The comment you linked makes it sound like the compiler requires providing panic machinery because it's (theoretically) being called, which an embedded user might not want to do because of the possibility of it accidentally being used elsewhere. I could be wildly off on that; embedded is not my forte.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

squashing can be done with bors now :)

🎉 I keep forgetting we have a new bors in town

The comment you linked makes it sound like the compiler requires providing panic machinery because it's (theoretically) being called, which an embedded user might not want to do because of the possibility of it accidentally being used elsewhere. I could be wildly off on that; embedded is not my forte.

I dug a bit more and looks like that check wasn't optimized out at -O1 around the time that compiler was written, https://rust.godbolt.org/z/n8fqr61bb has a call to slice_start_index_len_fail. It was at -O2 so maybe some optimizations moved around since then?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Up to you on which you prefer. unsafe is obviously future-proof regarding optimizations, but I also wouldn't expect regressions here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

r=me with the current version unless you have a strong preference, I wouldn't expect anyone concerned about binary size to be running with no optimizations

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@bors r=tgross35,jhpratt rollup

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 think it would be preferable not to rely on the optimizer to do what we want it to. I would expect that someone would have to regularly go in and fact check that comment, while having unsafe guarantees the behavior we want regardless of how things change.

@ArhanChaudhary

Copy link
Copy Markdown
Author

@bors squash msg="slice_split_once: bounds check optimization note"

@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

🔨 5 commits were squashed into 5d07ed5.

@rust-bors rust-bors Bot force-pushed the unchecked-subslice branch from 879e4c6 to 5d07ed5 Compare June 23, 2026 05:10
@ArhanChaudhary ArhanChaudhary changed the title slice_split_once: use unchecked subslicing operations slice_split_once: bounds check optimization note Jun 23, 2026
@rust-bors

rust-bors Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 5d07ed5 has been approved by tgross35,jhpratt

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 23, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 23, 2026
… r=tgross35,jhpratt

slice_split_once: bounds check optimization note

Tracking issue: rust-lang#112811

Use unchecked sub-slicing operations for `<T>::split_once` and `<T>::rsplit_once`.
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 23, 2026
… r=tgross35,jhpratt

slice_split_once: bounds check optimization note

Tracking issue: rust-lang#112811

~~Use unchecked sub-slicing operations for `<T>::split_once` and `<T>::rsplit_once`.~~
Note that unchecked sub-slicing operations are equivalent to the current checked sub-slicing operations.
rust-bors Bot pushed a commit that referenced this pull request Jun 23, 2026
Rollup of 8 pull requests

Successful merges:

 - #157960 (delegation: add support for infers in generics)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158222 (format: ignore println newline in foreign format hints)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158257 ( fix escaping placeholder check in next solver normalization folder)
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
rust-bors Bot pushed a commit that referenced this pull request Jun 23, 2026
Rollup of 8 pull requests

Successful merges:

 - #157960 (delegation: add support for infers in generics)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158222 (format: ignore println newline in foreign format hints)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158257 ( fix escaping placeholder check in next solver normalization folder)
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 23, 2026
… r=tgross35,jhpratt

slice_split_once: bounds check optimization note

Tracking issue: rust-lang#112811

~~Use unchecked sub-slicing operations for `<T>::split_once` and `<T>::rsplit_once`.~~
Note that unchecked sub-slicing operations are equivalent to the current checked sub-slicing operations.
rust-bors Bot pushed a commit that referenced this pull request Jun 23, 2026
…uwer

Rollup of 16 pull requests

Successful merges:

 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #158020 (Update mingw-w64 C toolchain)
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158257 ( fix escaping placeholder check in next solver normalization folder)
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)

Failed merges:

 - #158256 (Avoid parser panics bubbling out to proc macros)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 23, 2026
… r=tgross35,jhpratt

slice_split_once: bounds check optimization note

Tracking issue: rust-lang#112811

~~Use unchecked sub-slicing operations for `<T>::split_once` and `<T>::rsplit_once`.~~
Note that unchecked sub-slicing operations are equivalent to the current checked sub-slicing operations.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 23, 2026
… r=tgross35,jhpratt

slice_split_once: bounds check optimization note

Tracking issue: rust-lang#112811

~~Use unchecked sub-slicing operations for `<T>::split_once` and `<T>::rsplit_once`.~~
Note that unchecked sub-slicing operations are equivalent to the current checked sub-slicing operations.
rust-bors Bot pushed a commit that referenced this pull request Jun 23, 2026
…uwer

Rollup of 23 pull requests

Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #158020 (Update mingw-w64 C toolchain)
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158257 ( fix escaping placeholder check in next solver normalization folder)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)

Failed merges:

 - #158256 (Avoid parser panics bubbling out to proc macros)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 24, 2026
… r=tgross35,jhpratt

slice_split_once: bounds check optimization note

Tracking issue: rust-lang#112811

~~Use unchecked sub-slicing operations for `<T>::split_once` and `<T>::rsplit_once`.~~
Note that unchecked sub-slicing operations are equivalent to the current checked sub-slicing operations.
rust-bors Bot pushed a commit that referenced this pull request Jun 24, 2026
Rollup of 27 pull requests

Successful merges:

 - #158315 (`rust-analyzer` subtree update)
 - #155739 (Add temporary scope to assert_eq and assert_ne)
 - #156885 (Fix misattributed type inference error span for index expressions)
 - #157271 (simplify some `proc_macro` things)
 - #157883 (Remove strict invariant node_type on hir_type during ty privacy visit)
 - #157921 (trait solver: Resolve region vars in max universe)
 - #157960 (delegation: add support for infers in generics)
 - #157983 (Lift the same-signature restriction for `extern "tail"`)
 - #158053 (Optimize network address parser)
 - #158105 (Extract all instance shim variants into new `ShimKind` enum)
 - #158207 (Resolver: local/external split of  `resolve_ident_in_module_non_globs_unadjusted` )
 - #158279 (Follow goto and drop when linting unreachable code)
 - #157527 (Move derive tests into their dedicated folder)
 - #157807 (don't ice on non-lifetime binders under `-Zassumptions-on-binders`)
 - #158020 (Update mingw-w64 C toolchain)
 - #158039 (c-variadic: test that we use equality up to free lifetimes)
 - #158222 (format: ignore println newline in foreign format hints)
 - #158223 (Move target checking for #[lang] to the attribute parser)
 - #158252 (Use `cfg_select` in `std::os`)
 - #158257 ( fix escaping placeholder check in next solver normalization folder)
 - #158263 (Only load the feature list once in the entire resolver)
 - #158267 (FromUtf8Error::into_utf8_lossy better example and suggest use)
 - #158274 (triagebot: Stop pinging myself)
 - #158282 (slice_split_once: bounds check optimization note)
 - #158300 (Improve unknown crate_type diagnostic suggestions)
 - #158304 (mailmap: update mu001999)
 - #158309 (Update `rustc-literal-escaper` version to `0.0.8`)

Failed merges:

 - #158256 (Avoid parser panics bubbling out to proc macros)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 24, 2026
… r=tgross35,jhpratt

slice_split_once: bounds check optimization note

Tracking issue: rust-lang#112811

~~Use unchecked sub-slicing operations for `<T>::split_once` and `<T>::rsplit_once`.~~
Note that unchecked sub-slicing operations are equivalent to the current checked sub-slicing operations.
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 24, 2026
… r=tgross35,jhpratt

slice_split_once: bounds check optimization note

Tracking issue: rust-lang#112811

~~Use unchecked sub-slicing operations for `<T>::split_once` and `<T>::rsplit_once`.~~
Note that unchecked sub-slicing operations are equivalent to the current checked sub-slicing operations.
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-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.

6 participants