Use subslice patterns in slice methods by cuviper · Pull Request #69706 · rust-lang/rust (original) (raw)

At a minimum, this means the generated code will rely less on inlining for performance, but in some cases it also optimizes better.

To back this up, here is the compiler explorer comparing the immutable methods. Note the missing functions in the output, indicating that LLVM merged them with their counterpart. Then last demonstrates improved optimization:

example::last: xor ecx, ecx mov rdx, rsi sub rdx, 1 lea rax, [rdi + 4*rdx] cmovb rax, rcx cmp rdx, rsi cmovae rax, rcx ret

example::last_pattern: xor eax, eax sub rsi, 1 lea rcx, [rdi + 4*rsi] cmovae rax, rcx ret

Here is the same exploration for the mutable methods. It looks like LLVM's merge-functions pass was less successful here, outputing split methods that are identical AFAICS. Then last_mut shows the exact same optimization difference as last did.