From 9e6abce96338febdf726d37e7ff1fe3c04df43a6 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 19 Mar 2021 22:06:18 -0700 Subject: [PATCH] mux: adjust pty output buffering/reading I've had a few people comment that the screen repaints stutter more since the most recent release. One of the main changes in that area was to increase throughput for timg case, where a lot of data was being pumped through. I think that, ironically, the decreased latency results in more frequent repaints where not all of the updated screen is visible in a full screen redraw, so it appears more janky. This commit introduces a small 1ms delay to see if additional output is forthcoming when parsing the data. It will keep delaying and accumulating until there's at least one parsed output action to process, so there is a small constant latency overhead added to a single character output (thread context switch + 1ms delay). This small delay is counter-balanced with raising the priority of dispatching the render actions; previously we'd spawn them at lower-than-input priority. With the batching potential, I think spawning them at the same priority is OK; the main reason for the lower priority was to ensure timely ctrl-c processing when a lot of output is being dumped to the terminal. It's hard for me to gauge whether this fixes the reported issue, as I've been unable to reproduce it for myself. refs: https://github.com/wez/wezterm/issues/559 refs: https://github.com/wez/wezterm/issues/546 --- mux/src/lib.rs | 95 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 17 deletions(-) diff --git a/mux/src/lib.rs b/mux/src/lib.rs index 13555459a..4853d8aa9 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -8,10 +8,13 @@ use log::error; use portable_pty::ExitStatus; use std::cell::{Ref, RefCell, RefMut}; use std::collections::HashMap; +use std::collections::VecDeque; use std::io::Read; use std::rc::Rc; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; -use std::sync::Arc; +use std::sync::{Arc, Condvar, Mutex}; +use std::time::Duration; + use std::thread; use termwiz::escape::Action; use thiserror::*; @@ -58,7 +61,7 @@ pub struct Mux { /// It blocks until the mux has finished consuming the data, which provides /// some back-pressure so that eg: ctrl-c can remain responsive. fn send_actions_to_mux(pane_id: PaneId, dead: &Arc, actions: Vec) { - promise::spawn::block_on(promise::spawn::spawn_into_main_thread_with_low_priority({ + promise::spawn::block_on(promise::spawn::spawn_into_main_thread({ let dead = Arc::clone(&dead); async move { let mux = Mux::get().unwrap(); @@ -75,6 +78,64 @@ fn send_actions_to_mux(pane_id: PaneId, dead: &Arc, actions: Vec>, + cond: Condvar, + dead: Arc, +} + +impl BufState { + fn write(&self, buf: &[u8]) { + let mut queue = self.queue.lock().unwrap(); + queue.extend(buf); + self.cond.notify_one(); + } +} + +fn parse_buffered_data(pane_id: PaneId, state: &Arc) { + let mut parser = termwiz::escape::parser::Parser::new(); + let mut queue = state.queue.lock().unwrap(); + + loop { + if queue.is_empty() { + if state.dead.load(Ordering::Relaxed) { + return; + } + queue = state.cond.wait(queue).unwrap(); + continue; + } + + let mut actions = vec![]; + let buf = queue.make_contiguous(); + parser.parse(buf, |action| actions.push(action)); + queue.truncate(0); + + // Yield briefly to see if more data showed up and + // lump it together with what we've got + loop { + let wait_res = state + .cond + .wait_timeout(queue, Duration::from_millis(1)) + .unwrap(); + queue = wait_res.0; + if queue.is_empty() { + break; + } + let buf = queue.make_contiguous(); + parser.parse(buf, |action| actions.push(action)); + queue.truncate(0); + if !actions.is_empty() { + // Don't delay very long if we've got stuff to display! + break; + } + } + + if !actions.is_empty() { + send_actions_to_mux(pane_id, &state.dead, actions); + } + } +} + /// This function is run in a separate thread; its purpose is to perform /// blocking reads from the pty (non-blocking reads are not portable to /// all platforms and pty/tty types), parse the escape sequences and @@ -87,10 +148,19 @@ fn read_from_pane_pty(pane_id: PaneId, banner: Option, mut reader: Box, mut reader: Box { - let buf = &buf[..size]; - let actions = parser.parse_as_vec(&buf); - if !actions.is_empty() { - send_actions_to_mux(pane_id, &dead, actions); - } + state.write(&buf[..size]); } } } - dead.store(true, Ordering::Relaxed); - match configuration().exit_behavior { ExitBehavior::Hold | ExitBehavior::CloseOnCleanExit => { // We don't know if we can unilaterally close // this pane right now, so don't! - send_actions_to_mux( - pane_id, - &dead, - parser.parse_as_vec(b"\n[Process completed]"), - ); - return; + state.write(b"\n[Process completed]"); } ExitBehavior::Close => { promise::spawn::spawn_into_main_thread(async move { @@ -134,6 +193,8 @@ fn read_from_pane_pty(pane_id: PaneId, banner: Option, mut reader: Box