Little improves in CString `new` when creating from slice by AngelicosPhosphoros...
source link: https://github.com/rust-lang/rust/pull/92124
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
I checked generated asm for CString::new
call using such dylib:
use std::ffi::{CString, NulError}; #[no_mangle] pub extern "Rust" fn make_c_string()->Result<CString, NulError>{ CString::new("Hello world!") }
Asm generated using command: rustc +stage1 --crate-type=cdylib cstring_new.rs --emit=asm -Copt-level=3 -Ccodegen-units=1
Old generated code
New generated code
It can be seen that new caller code is shorter.
As for lto performance, I compiled same code used rustc +stage1 --crate-type=cdylib cstring_new.rs -Copt-level=3 -Ccodegen-units=1 -Clto=fat
then looked it using IdaPRO (on Windows).
When I used memchr
on new buffer, there is unrolled loop which checks each byte of new buffer if it is zero and branch on it. When I use self
, compiler eliminates check completely.
On Unix, there is probably no difference because Rust uses libc
implementation anyway in that case.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK