1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/cli/util/unix.rs
Divy Srivastava 0b75a7169b
perf: lazy atexit setup (#21053)
`libc::atexit` incurrs 2% dyld cost at startup on macOS. This PR moves
the setup to when the tty mode is changed using op_stdin_set_raw.
2023-11-04 17:30:12 +05:30

45 lines
1.2 KiB
Rust

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
/// Raise soft file descriptor limit to hard file descriptor limit.
/// This is the difference between `ulimit -n` and `ulimit -n -H`.
pub fn raise_fd_limit() {
#[cfg(unix)]
// TODO(bartlomieju):
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe {
let mut limits = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
if 0 != libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) {
return;
}
if limits.rlim_cur == libc::RLIM_INFINITY {
return;
}
// No hard limit? Do a binary search for the effective soft limit.
if limits.rlim_max == libc::RLIM_INFINITY {
let mut min = limits.rlim_cur;
let mut max = 1 << 20;
while min + 1 < max {
limits.rlim_cur = min + (max - min) / 2;
match libc::setrlimit(libc::RLIMIT_NOFILE, &limits) {
0 => min = limits.rlim_cur,
_ => max = limits.rlim_cur,
}
}
return;
}
// Raise the soft limit to the hard limit.
if limits.rlim_cur < limits.rlim_max {
limits.rlim_cur = limits.rlim_max;
libc::setrlimit(libc::RLIMIT_NOFILE, &limits);
}
}
}