Skip to content

Commit 5008444

Browse files
committed
Auto merge of rust-lang#121101 - GnomedDev:dyn-small-c-string, r=<try>
Reduce monomorphisation bloat in small_c_string This is a code path usually next to an FFI call, so taking the `dyn` slowdown for the 1159 llvm-line (fat lto, codegen-units 1, release build) drop in my testing program [t2fanrd](https://github.com/GnomedDev/t2fanrd) is worth it imo.
2 parents ee9c7c9 + 8f5dc71 commit 5008444

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

library/std/src/sys/pal/common/small_c_string.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,32 @@ const NUL_ERR: io::Error =
1717
#[inline]
1818
pub fn run_path_with_cstr<T, F>(path: &Path, f: F) -> io::Result<T>
1919
where
20-
F: FnOnce(&CStr) -> io::Result<T>,
20+
F: FnMut(&CStr) -> io::Result<T>,
2121
{
2222
run_with_cstr(path.as_os_str().as_encoded_bytes(), f)
2323
}
2424

2525
#[inline]
26-
pub fn run_with_cstr<T, F>(bytes: &[u8], f: F) -> io::Result<T>
26+
pub fn run_with_cstr<T, F>(bytes: &[u8], mut f: F) -> io::Result<T>
2727
where
28-
F: FnOnce(&CStr) -> io::Result<T>,
28+
F: FnMut(&CStr) -> io::Result<T>,
2929
{
30+
// Dispatch and dyn erase the closure type to prevent mono bloat.
31+
// See https://github.com/rust-lang/rust/pull/121101.
3032
if bytes.len() >= MAX_STACK_ALLOCATION {
31-
return run_with_cstr_allocating(bytes, f);
33+
run_with_cstr_allocating(bytes, &mut f)
34+
} else {
35+
unsafe { run_with_cstr_stack(bytes, &mut f) }
3236
}
37+
}
3338

39+
/// # Safety
40+
///
41+
/// `bytes` must have a length less than `MAX_STACK_ALLOCATION`.
42+
unsafe fn run_with_cstr_stack<T>(
43+
bytes: &[u8],
44+
f: &mut dyn FnMut(&CStr) -> io::Result<T>,
45+
) -> io::Result<T> {
3446
let mut buf = MaybeUninit::<[u8; MAX_STACK_ALLOCATION]>::uninit();
3547
let buf_ptr = buf.as_mut_ptr() as *mut u8;
3648

@@ -47,10 +59,10 @@ where
4759

4860
#[cold]
4961
#[inline(never)]
50-
fn run_with_cstr_allocating<T, F>(bytes: &[u8], f: F) -> io::Result<T>
51-
where
52-
F: FnOnce(&CStr) -> io::Result<T>,
53-
{
62+
fn run_with_cstr_allocating<T>(
63+
bytes: &[u8],
64+
f: &mut dyn FnMut(&CStr) -> io::Result<T>,
65+
) -> io::Result<T> {
5466
match CString::new(bytes) {
5567
Ok(s) => f(&s),
5668
Err(_) => Err(NUL_ERR),

src/tools/miri/tests/fail/shims/fs/isolated_file.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LL | let fd = cvt_r(|| unsafe { open64(path.as_ptr(), flags, opts.mode a
1111
= note: inside `std::sys::pal::PLATFORM::cvt_r::<i32, {closure@std::sys::pal::PLATFORM::fs::File::open_c::{closure#0}}>` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
1212
= note: inside `std::sys::pal::PLATFORM::fs::File::open_c` at RUSTLIB/std/src/sys/pal/PLATFORM/fs.rs:LL:CC
1313
= note: inside closure at RUSTLIB/std/src/sys/pal/PLATFORM/fs.rs:LL:CC
14+
= note: inside `std::sys::pal::PLATFORM::small_c_string::run_with_cstr_stack::<std::sys::pal::PLATFORM::fs::File>` at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC
1415
= note: inside `std::sys::pal::PLATFORM::small_c_string::run_with_cstr::<std::sys::pal::PLATFORM::fs::File, {closure@std::sys::pal::PLATFORM::fs::File::open::{closure#0}}>` at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC
1516
= note: inside `std::sys::pal::PLATFORM::small_c_string::run_path_with_cstr::<std::sys::pal::PLATFORM::fs::File, {closure@std::sys::pal::PLATFORM::fs::File::open::{closure#0}}>` at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC
1617
= note: inside `std::sys::pal::PLATFORM::fs::File::open` at RUSTLIB/std/src/sys/pal/PLATFORM/fs.rs:LL:CC

0 commit comments

Comments
 (0)