about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorAdam Reichold <adam.reichold@t-online.de>2020-08-01 14:29:42 +0200
committerAdam Reichold <adam.reichold@t-online.de>2020-08-05 16:57:02 +0200
commit6672f7be032e1fc392cb0e2a785563b82b095117 (patch)
tree08dca229b01199c73fd8dac32927a5e187de980f /library/std/src
parent94687525815cb2138779e17a766e24c826819d7c (diff)
downloadrust-6672f7be032e1fc392cb0e2a785563b82b095117.tar.gz
rust-6672f7be032e1fc392cb0e2a785563b82b095117.zip
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.
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/sys/unix/fd.rs29
1 files changed, 17 insertions, 12 deletions
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<c_int> = 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")))]