mirror of
https://github.com/denoland/deno.git
synced 2025-02-01 20:25:12 -05:00
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.
This commit is contained in:
parent
8acf059ac6
commit
0b75a7169b
3 changed files with 35 additions and 25 deletions
|
@ -323,7 +323,6 @@ pub(crate) fn unstable_warn_cb(feature: &str) {
|
|||
pub fn main() {
|
||||
setup_panic_hook();
|
||||
|
||||
util::unix::prepare_stdio();
|
||||
util::unix::raise_fd_limit();
|
||||
util::windows::ensure_stdio_open();
|
||||
#[cfg(windows)]
|
||||
|
|
|
@ -43,27 +43,3 @@ pub fn raise_fd_limit() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prepare_stdio() {
|
||||
#[cfg(unix)]
|
||||
// SAFETY: Save current state of stdio and restore it when we exit.
|
||||
unsafe {
|
||||
use libc::atexit;
|
||||
use libc::tcgetattr;
|
||||
use libc::tcsetattr;
|
||||
use libc::termios;
|
||||
|
||||
let mut termios = std::mem::zeroed::<termios>();
|
||||
if tcgetattr(libc::STDIN_FILENO, &mut termios) == 0 {
|
||||
static mut ORIG_TERMIOS: Option<termios> = None;
|
||||
ORIG_TERMIOS = Some(termios);
|
||||
|
||||
extern "C" fn reset_stdio() {
|
||||
// SAFETY: Reset the stdio state.
|
||||
unsafe { tcsetattr(libc::STDIN_FILENO, 0, &ORIG_TERMIOS.unwrap()) };
|
||||
}
|
||||
|
||||
atexit(reset_stdio);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -118,6 +118,41 @@ fn op_stdin_set_raw(
|
|||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
fn prepare_stdio() {
|
||||
// SAFETY: Save current state of stdio and restore it when we exit.
|
||||
unsafe {
|
||||
use libc::atexit;
|
||||
use libc::tcgetattr;
|
||||
use libc::tcsetattr;
|
||||
use libc::termios;
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
// Only save original state once.
|
||||
static ORIG_TERMIOS: OnceCell<Option<termios>> = OnceCell::new();
|
||||
ORIG_TERMIOS.get_or_init(|| {
|
||||
let mut termios = std::mem::zeroed::<termios>();
|
||||
if tcgetattr(libc::STDIN_FILENO, &mut termios) == 0 {
|
||||
extern "C" fn reset_stdio() {
|
||||
// SAFETY: Reset the stdio state.
|
||||
unsafe {
|
||||
tcsetattr(
|
||||
libc::STDIN_FILENO,
|
||||
0,
|
||||
&ORIG_TERMIOS.get().unwrap().unwrap(),
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
atexit(reset_stdio);
|
||||
return Some(termios);
|
||||
}
|
||||
|
||||
None
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
prepare_stdio();
|
||||
let tty_mode_store = state.borrow::<TtyModeStore>().clone();
|
||||
let previous_mode = tty_mode_store.get(rid);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue