rust/library/core/src
Samuel Tardieu 460a627b83
Rollup merge of #143907 - Kijewski:pr-inline-split_at_unchecked, r=Mark-Simulacrum
core: make `str::split_at_unchecked()` inline

This PR adds `#[inline]` to the method `str::split_at_unchecked()`. This is done for two reasons:

1. The method is tiny, e.g. on AMD-64 (<https://godbolt.org/z/ba68fdfxn>):

   ```asm
   movq    %rdi, %rax
   subq    %rcx, %rdx
   movq    %rsi, (%rdi)
   addq    %rcx, %rsi
   movq    %rcx, 8(%rdi)
   movq    %rsi, 16(%rdi)
   movq    %rdx, 24(%rdi)
   retq
   ```

2. More importantly, inlining the method enables further automatic optimizations. E.g. if you split at index 3, then in the compiler (rustc, llvm or both) knows that this code cannot fail, and the panicking path is omitted in the generated code:

   ```rust
   pub fn punctuation(i: &str) -> Result<(), ()> {
       const THREE_CHARS: &[[u8; 3]] = &[*b"<<=", *b">>=", *b"...", *b"..="];

       if let Some((head, _)) = i.split_at_checked(3)
           && THREE_CHARS.contains(&head.as_bytes().try_into().unwrap())
       {
           Ok(())
       } else {
           Err(())
       }
   }
   ```

   <details>
   <summary>Without PR</summary>

   <https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=0234de8158f467eebd73286f20d6e27a>

   ```asm
   playground::punctuation:
           subq    $40, %rsp
           movq    %rsi, %rdx
           movq    %rdi, %rsi
           movb    $1, %al
           cmpq    $3, %rdx
           ja      .LBB2_2
           je      .LBB2_3
   .LBB2_11:
           addq    $40, %rsp
           retq
   .LBB2_2:
           cmpb    $-64, 3(%rsi)
           jl      .LBB2_11
   .LBB2_3:
           leaq    8(%rsp), %rdi
           movl    $3, %ecx
           callq   *core::str::<impl str>::split_at_unchecked@GOTPCREL(%rip)
           movq    8(%rsp), %rcx
           movb    $1, %al
           testq   %rcx, %rcx
           je      .LBB2_11
           cmpq    $3, 16(%rsp)
           jne     .LBB2_12
           movzwl  (%rcx), %edx
           movzbl  2(%rcx), %ecx
           shll    $16, %ecx
           orl     %edx, %ecx
           cmpl    $4013115, %ecx
           jg      .LBB2_8
           cmpl    $3026478, %ecx
           je      .LBB2_10
           cmpl    $4009518, %ecx
           je      .LBB2_10
           jmp     .LBB2_11
   .LBB2_8:
           cmpl    $4013630, %ecx
           je      .LBB2_10
           cmpl    $4013116, %ecx
           jne     .LBB2_11
   .LBB2_10:
           xorl    %eax, %eax
           addq    $40, %rsp
           retq
   .LBB2_12:
           leaq    .Lanon.d98a7fbb86d10a97c24516e267466134.2(%rip), %rdi
           leaq    .Lanon.d98a7fbb86d10a97c24516e267466134.1(%rip), %rcx
           leaq    .Lanon.d98a7fbb86d10a97c24516e267466134.6(%rip), %r8
           leaq    7(%rsp), %rdx
           movl    $43, %esi
           callq   *core::result::unwrap_failed@GOTPCREL(%rip)
   ```
   </details>

   <details>
   <summary>With PR</summary>

   <https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=5d4058c79ce0f6cb1a434190427d2055>

   ```asm
   playground::punctuation:
           movb    $1, %al
           cmpq    $3, %rsi
           ja      .LBB0_2
           je      .LBB0_3
   .LBB0_9:
           retq
   .LBB0_2:
           cmpb    $-64, 3(%rdi)
           jl      .LBB0_9
   .LBB0_3:
           movzwl  (%rdi), %eax
           movzbl  2(%rdi), %ecx
           shll    $16, %ecx
           orl     %eax, %ecx
           movb    $1, %al
           cmpl    $4013115, %ecx
           jg      .LBB0_6
           cmpl    $3026478, %ecx
           je      .LBB0_8
           cmpl    $4009518, %ecx
           je      .LBB0_8
           jmp     .LBB0_9
   .LBB0_6:
           cmpl    $4013630, %ecx
           je      .LBB0_8
           cmpl    $4013116, %ecx
           jne     .LBB0_9
   .LBB0_8:
           xorl    %eax, %eax
           retq
   ```
   </details>
2025-07-15 12:52:41 +02:00
..
alloc clippy fix: indentation 2025-07-04 11:52:17 +00:00
array simplify receivers for some array method calls 2025-07-03 14:39:27 +00:00
ascii Add some track_caller info to precondition panics 2025-05-21 09:10:06 -04:00
async_iter Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
bstr Replace colon with parentheses, add missing period 2025-04-21 10:48:46 -04:00
cell Update version placeholders 2025-07-01 10:54:33 -07:00
char Add diagnostic items for Clippy 2025-06-20 17:53:09 +02:00
clone CloneToUninit: use a private specialization trait 2024-07-29 20:44:43 +03:00
cmp Use generic NonZero everywhere in core. 2024-02-22 15:17:33 +01:00
convert constify From and Into 2025-07-11 08:30:47 +00:00
ffi move the va_copy, va_arg and va_end to core::intrinsics 2025-07-05 09:18:49 +02:00
fmt Rollup merge of #143426 - hkBst:clippy-fix-indent-1, r=jhpratt 2025-07-08 22:50:27 -05:00
future Address documentation issues identified with Future 2025-06-07 20:13:54 +01:00
hash Remove some unsized tuple impls now that we don't support unsizing tuples anymore 2025-07-02 14:17:54 +00:00
intrinsics Change "allocated object" to "allocation". 2025-07-14 15:01:58 +07:00
io Auto merge of #142885 - a1phyr:borrowed_cursor_to_buf, r=Mark-Simulacrum 2025-07-14 23:45:18 +00:00
iter update issue number for const_trait_impl 2025-07-13 23:55:06 +08:00
macros make cfg_select a builtin macro 2025-07-13 14:34:40 +02:00
marker phantom_variance_markers: fix identifier usage in macro 2025-06-22 16:06:25 -07:00
mem Remove support for dynamic allocas 2025-07-07 23:04:06 +02:00
net Add Ipv4Addr and Ipv6Addr diagnostic items 2025-05-14 09:34:25 +02:00
num Rollup merge of #143611 - GrigorenkoPV:ParseIntError, r=tgross35 2025-07-11 07:35:20 +02:00
ops update issue number for const_trait_impl 2025-07-13 23:55:06 +08:00
panic Use a NonNull pointer 2025-06-23 13:45:56 +00:00
pin Remove PointerLike trait 2025-07-03 20:03:49 +00:00
prelude Remove the deprecated concat_idents! macro 2025-06-24 11:07:16 +00:00
ptr Rollup merge of #143917 - theemathas:change-allocated-object-to-allocation, r=oli-obk 2025-07-14 11:04:56 +02:00
range Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
slice Rollup merge of #143875 - fee1-dead-contrib:push-zvqrmzrprpzt, r=compiler-errors 2025-07-14 11:04:55 +02:00
str Rollup merge of #143907 - Kijewski:pr-inline-split_at_unchecked, r=Mark-Simulacrum 2025-07-15 12:52:41 +02:00
sync core docs: improve clarity of considerations about atomic CAS operations 2025-06-10 17:14:04 +02:00
task Rollup merge of #142158 - xizheyin:141617, r=jdonszelmann 2025-06-13 05:16:56 +02:00
unicode Remove uncessary parens in closure body with unused lint 2025-07-10 09:25:56 +08:00
any.rs Add opaque TypeId handles for CTFE 2025-07-09 16:37:11 +00:00
arch.rs update version placeholders 2025-05-12 15:33:30 +02:00
ascii.rs add doc(alias("AsciiChar")) to core::ascii::Char 2025-06-20 11:11:51 -05:00
asserting.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
bool.rs Add methods for converting bool to Result<(), E> 2025-06-19 23:20:06 +02:00
borrow.rs Suggest borrowing on fn argument that is impl AsRef 2024-05-09 23:25:31 +00:00
cell.rs update issue number for const_trait_impl 2025-07-13 23:55:06 +08:00
clone.rs Make Clone a const_trait 2025-06-19 16:36:33 -07:00
cmp.rs fix PartialEq const feature name and const_cmp tracking issue 2025-07-11 17:57:50 +02:00
contracts.rs Apply suggestions from code review 2025-04-10 16:32:56 -07:00
default.rs update issue number for const_trait_impl 2025-07-13 23:55:06 +08:00
error.md Mention core's PanicInfo in error.md. 2024-06-11 15:47:00 +02:00
error.rs clippy fix: indentation 2025-07-04 11:52:17 +00:00
escape.rs Get rid of EscapeDebugInner. 2025-06-15 22:08:41 +02:00
hint.rs Auto merge of #129658 - saethlin:spare-a-crumb, r=jhpratt 2025-05-27 22:11:53 +00:00
internal_macros.rs Use cfg_match in core 2025-03-26 14:32:35 -04:00
lib.miri.rs add 'x.py miri', and make it work for 'library/{core,alloc,std}' 2024-04-03 20:27:20 +02:00
lib.rs setup CI and tidy to use typos for spellchecking and fix few typos 2025-07-03 10:51:06 +03:00
marker.rs update issue number for const_trait_impl 2025-07-13 23:55:06 +08:00
option.rs update issue number for const_trait_impl 2025-07-13 23:55:06 +08:00
panic.rs update cfgs 2024-11-27 15:14:54 +00:00
panicking.rs Insert checks for enum discriminants when debug assertions are enabled 2025-06-27 09:37:36 +00:00
pat.rs update cfgs 2025-04-09 12:29:59 +01:00
pin.rs clippy fix: indentation 2025-07-04 11:52:17 +00:00
primitive.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
primitive_docs.rs random: Provide a Distribution<T> trait 2025-07-11 10:21:34 -07:00
random.rs random: Provide a Distribution<T> trait 2025-07-11 10:21:34 -07:00
range.rs update cfg(bootstrap) 2025-02-18 09:32:44 -08:00
result.rs Update version placeholders 2025-07-01 10:54:33 -07:00
time.rs Split duration_constructors to get non-controversial bits out faster. 2025-05-09 22:52:39 -04:00
tuple.rs random: Provide a Distribution<T> trait 2025-07-11 10:21:34 -07:00
ub_checks.rs Add some track_caller info to precondition panics 2025-05-21 09:10:06 -04:00
unit.rs core: Make Debug impl of raw pointers print metadata if present 2025-02-15 17:27:55 +01:00
unsafe_binder.rs Add unwrap_unsafe_binder and wrap_unsafe_binder macro operators 2024-12-12 16:29:40 +00:00