Skip to content

Commit 6672f7b

Browse files
committed
Memoize the I/O vector count limit
Keep the I/O vector count limit in a `SyncOnceCell` to avoid the overhead of repeatedly calling `sysconf` as these limits are guaranteed to not change during the lifetime of a process by POSIX.
1 parent 9468752 commit 6672f7b

File tree

1 file changed

+17
-12
lines changed
  • library/std/src/sys/unix

1 file changed

+17
-12
lines changed

library/std/src/sys/unix/fd.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use crate::cmp;
44
use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read};
5+
use crate::lazy::SyncOnceCell;
56
use crate::mem;
67
use crate::sys::cvt;
78
use crate::sys_common::AsInner;
@@ -28,18 +29,22 @@ const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
2829

2930
#[cfg(any(target_os = "linux", target_os = "macos"))]
3031
fn max_iov() -> c_int {
31-
let ret = unsafe {
32-
libc::sysconf(
33-
#[cfg(target_os = "linux")]
34-
libc::_SC_IOV_MAX,
35-
#[cfg(target_os = "macos")]
36-
libc::_SC_UIO_MAXIOV,
37-
)
38-
};
39-
40-
// 1024 is the default value on modern Linux systems
41-
// and hopefully more useful than `c_int::MAX`.
42-
if ret > 0 { ret as c_int } else { 1024 }
32+
static LIM: SyncOnceCell<c_int> = SyncOnceCell::new();
33+
34+
*LIM.get_or_init(|| {
35+
let ret = unsafe {
36+
libc::sysconf(
37+
#[cfg(target_os = "linux")]
38+
libc::_SC_IOV_MAX,
39+
#[cfg(target_os = "macos")]
40+
libc::_SC_UIO_MAXIOV,
41+
)
42+
};
43+
44+
// 1024 is the default value on modern Linux systems
45+
// and hopefully more useful than `c_int::MAX`.
46+
if ret > 0 { ret as c_int } else { 1024 }
47+
})
4348
}
4449

4550
#[cfg(not(any(target_os = "linux", target_os = "macos")))]

0 commit comments

Comments
 (0)