From 6672f7be032e1fc392cb0e2a785563b82b095117 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Sat, 1 Aug 2020 14:29:42 +0200 Subject: 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. --- library/std/src/sys/unix/fd.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'library/std/src/sys') diff --git a/library/std/src/sys/unix/fd.rs b/library/std/src/sys/unix/fd.rs index 675528bd526..e294df98c60 100644 --- a/library/std/src/sys/unix/fd.rs +++ b/library/std/src/sys/unix/fd.rs @@ -2,6 +2,7 @@ use crate::cmp; use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read}; +use crate::lazy::SyncOnceCell; use crate::mem; use crate::sys::cvt; use crate::sys_common::AsInner; @@ -28,18 +29,22 @@ const READ_LIMIT: usize = libc::ssize_t::MAX as usize; #[cfg(any(target_os = "linux", target_os = "macos"))] fn max_iov() -> c_int { - let ret = unsafe { - libc::sysconf( - #[cfg(target_os = "linux")] - libc::_SC_IOV_MAX, - #[cfg(target_os = "macos")] - libc::_SC_UIO_MAXIOV, - ) - }; - - // 1024 is the default value on modern Linux systems - // and hopefully more useful than `c_int::MAX`. - if ret > 0 { ret as c_int } else { 1024 } + static LIM: SyncOnceCell = SyncOnceCell::new(); + + *LIM.get_or_init(|| { + let ret = unsafe { + libc::sysconf( + #[cfg(target_os = "linux")] + libc::_SC_IOV_MAX, + #[cfg(target_os = "macos")] + libc::_SC_UIO_MAXIOV, + ) + }; + + // 1024 is the default value on modern Linux systems + // and hopefully more useful than `c_int::MAX`. + if ret > 0 { ret as c_int } else { 1024 } + }) } #[cfg(not(any(target_os = "linux", target_os = "macos")))] -- cgit 1.4.1-3-g733a5