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.
This commit is contained in:
parent
9468752581
commit
6672f7be03
1 changed files with 16 additions and 11 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use crate::cmp;
|
use crate::cmp;
|
||||||
use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read};
|
use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read};
|
||||||
|
use crate::lazy::SyncOnceCell;
|
||||||
use crate::mem;
|
use crate::mem;
|
||||||
use crate::sys::cvt;
|
use crate::sys::cvt;
|
||||||
use crate::sys_common::AsInner;
|
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"))]
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||||
fn max_iov() -> c_int {
|
fn max_iov() -> c_int {
|
||||||
let ret = unsafe {
|
static LIM: SyncOnceCell<c_int> = SyncOnceCell::new();
|
||||||
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
|
*LIM.get_or_init(|| {
|
||||||
// and hopefully more useful than `c_int::MAX`.
|
let ret = unsafe {
|
||||||
if ret > 0 { ret as c_int } else { 1024 }
|
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")))]
|
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue