rust/library/std/src/ffi
bors 514b387795 Auto merge of #90007 - xfix:inline-cstr-from-str, r=kennytm
Inline CStr::from_ptr

Inlining this function is valuable, as it allows LLVM to apply `strlen`-specific optimizations without having to enable LTO.

For instance, the following function:

```rust
pub fn f(p: *const c_char) -> Option<u8> {
    unsafe { CStr::from_ptr(p) }.to_bytes().get(0).copied()
}
```

Looks like this if `CStr::from_ptr` is allowed to be inlined.

```asm
before:
        push    rax
        call    qword ptr [rip + std::ffi::c_str::CStr::from_ptr@GOTPCREL]
        mov     rcx, rax
        cmp     rdx, 1
        sete    dl
        test    rax, rax
        sete    al
        or      al, dl
        jne     .LBB1_2
        mov     dl, byte ptr [rcx]
.LBB1_2:
        xor     al, 1
        pop     rcx
        ret

after:
        mov     dl, byte ptr [rdi]
        test    dl, dl
        setne   al
        ret
```

Note that optimization turned this from O(N) to O(1) in terms of performance, as LLVM knows that it doesn't really need to call `strlen` to determine whether a string is empty or not.
2021-10-22 21:01:59 +00:00
..
c_str ffi::c_str added tests for empty strings 2021-03-28 19:58:49 +03:00
os_str std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
c_str.rs Auto merge of #90007 - xfix:inline-cstr-from-str, r=kennytm 2021-10-22 21:01:59 +00:00
mod.rs fix 'since' version number 2021-10-20 15:36:55 -06:00
os_str.rs Rollup merge of #89794 - jkugelman:must-use-to_value-conversions, r=joshtriplett 2021-10-13 21:55:13 +09:00