Skip to content

Use unchecked_sub in str indexing #123561

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

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions library/core/src/str/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Trait implementations for `str`.

use crate::cmp::Ordering;
use crate::intrinsics::unchecked_sub;
use crate::ops;
use crate::ptr;
use crate::slice::SliceIndex;
Expand Down Expand Up @@ -210,9 +211,10 @@ unsafe impl SliceIndex<str> for ops::Range<usize> {

// SAFETY: the caller guarantees that `self` is in bounds of `slice`
// which satisfies all the conditions for `add`.
let ptr = unsafe { slice.as_ptr().add(self.start) };
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.

ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), new_len) as *const str
}
}
#[inline]
unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
Expand All @@ -229,9 +231,10 @@ unsafe impl SliceIndex<str> for ops::Range<usize> {
);

// SAFETY: see comments for `get_unchecked`.
let ptr = unsafe { slice.as_mut_ptr().add(self.start) };
let len = self.end - self.start;
ptr::slice_from_raw_parts_mut(ptr, len) as *mut str
unsafe {
let new_len = unchecked_sub(self.end, self.start);
ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), new_len) as *mut str
}
}
#[inline]
fn index(self, slice: &str) -> &Self::Output {
Expand Down
28 changes: 28 additions & 0 deletions tests/codegen/slice-indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,31 @@ pub unsafe fn get_unchecked_mut_by_range(x: &mut [i32], r: Range<usize>) -> &mut
// CHECK: sub nuw i64
x.get_unchecked_mut(r)
}

// CHECK-LABEL: @str_index_by_range(
#[no_mangle]
pub fn str_index_by_range(x: &str, r: Range<usize>) -> &str {
// CHECK: sub nuw i64
&x[r]
}

// CHECK-LABEL: @str_get_unchecked_by_range(
#[no_mangle]
pub unsafe fn str_get_unchecked_by_range(x: &str, r: Range<usize>) -> &str {
// CHECK: sub nuw i64
x.get_unchecked(r)
}

// CHECK-LABEL: @str_index_mut_by_range(
#[no_mangle]
pub fn str_index_mut_by_range(x: &mut str, r: Range<usize>) -> &mut str {
// CHECK: sub nuw i64
&mut x[r]
}

// CHECK-LABEL: @str_get_unchecked_mut_by_range(
#[no_mangle]
pub unsafe fn str_get_unchecked_mut_by_range(x: &mut str, r: Range<usize>) -> &mut str {
// CHECK: sub nuw i64
x.get_unchecked_mut(r)
}
Loading