A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
perf(mux): reuse the parser's action buffer across batches
RoversX415e4621211a
diff --git a/mux/src/lib.rs b/mux/src/lib.rsindex eb8894b4..cc7287a0 100644--- a/mux/src/lib.rs+++ b/mux/src/lib.rs@@ -573,11 +573,17 @@ pub(crate) fn materialize_kitty_image_data_sources(actions: &mut [Action]) {}}-pub(crate) fn has_external_kitty_image_data_source(actions: &[Action]) -> bool {- actions.iter().any(|action| match action {+/// Whether `action` carries a Kitty payload still to be read from a file or+/// shared memory (see `materialize_kitty_image_data_sources`).+pub(crate) fn is_external_kitty_image_data_source(action: &Action) -> bool {+ match action {Action::KittyImage(image) => image.has_external_data_source(),_ => false,- })+ }+}++pub(crate) fn has_external_kitty_image_data_source(actions: &[Action]) -> bool {+ actions.iter().any(is_external_kitty_image_data_source)}/// A pane whose parser holds the terminal for a long time, on request:@@ -611,24 +617,38 @@ fn wedge_for_the_test(pane: &Arc<dyn Pane>) {}/// This function applies parsed actions to the pane and notifies any-/// mux subscribers about the output event-fn send_actions_to_mux(pane: &Weak<dyn Pane>, dead: &Arc<AtomicBool>, mut actions: Vec<Action>) {+/// mux subscribers about the output event. `actions` is left empty with its+/// allocation kept for the next batch. `external_kitty` says whether any of+/// the actions carries an external Kitty payload; the parser tracks that as+/// it appends, so a batch without one is not scanned here or by the pane.+fn send_actions_to_mux(+ pane: &Weak<dyn Pane>,+ dead: &Arc<AtomicBool>,+ actions: &mut Vec<Action>,+ external_kitty: bool,+) {let start = Instant::now();+ debug_assert_eq!(+ external_kitty,+ has_external_kitty_image_data_source(actions)+ );// External kitty payloads (a path or shm name) are read here, on the// parser thread and per flush, so the terminal lock never covers disk// IO and a backlog is trimmed to its newest frames before any of it is// read. Reading at parse time instead held every frame of the backlog.- if configuration().enable_kitty_graphics && has_external_kitty_image_data_source(&actions) {- materialize_kitty_image_data_sources(&mut actions);+ if external_kitty && configuration().enable_kitty_graphics {+ materialize_kitty_image_data_sources(actions);}match pane.upgrade() {Some(pane) => {wedge_for_the_test(&pane);- pane.perform_actions(actions);+ pane.perform_actions_in_place(actions, external_kitty);+ debug_assert!(actions.is_empty());histogram!("send_actions_to_mux.perform_actions.latency").record(start.elapsed());Mux::notify_from_any_thread(MuxNotification::PaneOutput(pane.pane_id()));}None => {+ actions.clear();// Something else removed the pane from// the mux, so signal that we should stop// trying to process it in read_from_pane_pty.@@ -638,6 +658,25 @@ fn send_actions_to_mux(pane: &Weak<dyn Pane>, dead: &Arc<AtomicBool>, mut actionhistogram!("send_actions_to_mux.rate").record(1.);}+/// Whether the parser should give its batch allocation back before waiting+/// for more output. It is kept while output keeps arriving, so a busy pane+/// does not regrow it for every batch; it is released once nothing is left+/// to read, and whenever a batch grew past one action per byte of a full+/// read, which only held synchronized output can reach.+fn release_action_buffer(actions: &Vec<Action>, read_capacity: usize, more_to_read: bool) -> bool {+ actions.capacity() > 0 && (!more_to_read || actions.capacity() > read_capacity)+}++/// Whether `fd` has data to read right now, without waiting for any.+fn readable_now(fd: &FileDescriptor) -> bool {+ let mut pfd = [pollfd {+ fd: fd.as_socket_descriptor(),+ events: POLLIN,+ revents: 0,+ }];+ matches!(poll(&mut pfd, Some(Duration::ZERO)), Ok(1))+}+fn parse_buffered_data(pane: Weak<dyn Pane>,pane_id: PaneId,@@ -656,8 +695,15 @@ fn parse_buffered_data(let mut action_size = 0;let mut delay = Duration::from_millis(configuration().mux_output_parser_coalesce_delay_ms);let mut deadline = None;+ let mut external_kitty = false;loop {+ if actions.is_empty() && actions.capacity() > 0 {+ let more_to_read = readable_now(&rx);+ if release_action_buffer(&actions, buf.len(), more_to_read) {+ actions = Vec::new();+ }+ }match rx.read(&mut buf) {Ok(size) if size == 0 => {dead.store(true, Ordering::Relaxed);@@ -687,7 +733,8 @@ fn parse_buffered_data(// Flush prior actionsif !actions.is_empty() {- send_actions_to_mux(&pane, &dead, std::mem::take(&mut actions));+ send_actions_to_mux(&pane, &dead, &mut actions, external_kitty);+ external_kitty = false;action_size = 0;}}@@ -703,10 +750,12 @@ fn parse_buffered_data(}_ => {}};+ external_kitty |= is_external_kitty_image_data_source(&action);action.append_to(&mut actions);if flush && !actions.is_empty() {- send_actions_to_mux(&pane, &dead, std::mem::take(&mut actions));+ send_actions_to_mux(&pane, &dead, &mut actions, external_kitty);+ external_kitty = false;action_size = 0;}});@@ -746,7 +795,8 @@ fn parse_buffered_data(}let flush_started = Instant::now();- send_actions_to_mux(&pane, &dead, std::mem::take(&mut actions));+ send_actions_to_mux(&pane, &dead, &mut actions, external_kitty);+ external_kitty = false;heartbeat.add(flush_started.elapsed(), 0);deadline = None;action_size = 0;@@ -776,7 +826,7 @@ fn parse_buffered_data(// for very short lived commands so that we don't forget to// display what they displayed.if !actions.is_empty() {- send_actions_to_mux(&pane, &dead, std::mem::take(&mut actions));+ send_actions_to_mux(&pane, &dead, &mut actions, external_kitty);}if let Some(control) = &control {control.pending_bytes.fetch_sub(unapplied, Ordering::SeqCst);@@ -3799,6 +3849,26 @@ mod tests {assert_eq!(state.note_output(9), 1);}+ #[test]+ fn action_buffer_is_kept_only_while_output_is_waiting() {+ let empty: Vec<Action> = Vec::new();+ assert!(+ !release_action_buffer(&empty, 128, false),+ "nothing to release"+ );+ let mut kept: Vec<Action> = Vec::with_capacity(64);+ assert!(+ !release_action_buffer(&kept, 128, true),+ "busy: kept for the next batch"+ );+ assert!(release_action_buffer(&kept, 128, false), "idle: given back");+ kept.reserve(256);+ assert!(+ release_action_buffer(&kept, 128, true),+ "grew past one action per byte of a full read: given back"+ );+ }+#[test]fn tab_geometry_transaction_coalesces_and_is_tab_scoped() {let mut state = TabResizeNotificationState::default();diff --git a/mux/src/localpane.rs b/mux/src/localpane.rsindex 865b2fab..04c3578a 100644--- a/mux/src/localpane.rs+++ b/mux/src/localpane.rs@@ -666,18 +666,26 @@ impl Pane for LocalPane {}fn perform_actions(&self, mut actions: Vec<termwiz::escape::Action>) {- if crate::has_external_kitty_image_data_source(&actions) {+ self.perform_actions_in_place(&mut actions, true);+ }++ fn perform_actions_in_place(+ &self,+ actions: &mut Vec<termwiz::escape::Action>,+ may_hold_external_kitty: bool,+ ) {+ if may_hold_external_kitty && crate::has_external_kitty_image_data_source(actions) {let kitty_graphics_enabled ={ self.terminal.lock().get_config().enable_kitty_graphics() };if kitty_graphics_enabled {- crate::materialize_kitty_image_data_sources(&mut actions);+ crate::materialize_kitty_image_data_sources(actions);}}let mut term = self.terminal.lock();self.apply_pending_config(&mut term);self.apply_pending_resize(&mut term);self.apply_pending_focus(&mut term);- term.perform_actions(actions);+ term.perform_actions_in_place(actions);self.apply_pending_and_store(&mut term);drop(term);// A resize that arrived after that last look, while the terminaldiff --git a/mux/src/pane.rs b/mux/src/pane.rsindex ef49c503..2f346599 100644--- a/mux/src/pane.rs+++ b/mux/src/pane.rs@@ -352,6 +352,18 @@ pub trait Pane: Downcast + Send + Sync {}fn mouse_event(&self, event: MouseEvent) -> anyhow::Result<()>;fn perform_actions(&self, _actions: Vec<termwiz::escape::Action>) {}+ /// Apply every action in `actions` exactly like `perform_actions`,+ /// leaving the vector empty. This default hands the allocation on with+ /// the actions; implementations that keep it for reuse override it.+ /// `may_hold_external_kitty` is false when the caller knows that no+ /// action carries an external Kitty payload, so it need not be scanned.+ fn perform_actions_in_place(+ &self,+ actions: &mut Vec<termwiz::escape::Action>,+ _may_hold_external_kitty: bool,+ ) {+ self.perform_actions(std::mem::take(actions));+ }fn is_dead(&self) -> bool;fn kill(&self) {}diff --git a/term/src/terminal.rs b/term/src/terminal.rsindex 8a8d5b8c..16eaecb9 100644--- a/term/src/terminal.rs+++ b/term/src/terminal.rs@@ -177,11 +177,17 @@ impl Terminal {self.trigger_unseen_output_notif();}- pub fn perform_actions(&mut self, actions: Vec<wezterm_escape_parser::Action>) {+ pub fn perform_actions(&mut self, mut actions: Vec<wezterm_escape_parser::Action>) {+ self.perform_actions_in_place(&mut actions);+ }++ /// `perform_actions`, leaving `actions` empty with its allocation kept so+ /// a caller that applies batch after batch does not regrow it each time.+ pub fn perform_actions_in_place(&mut self, actions: &mut Vec<wezterm_escape_parser::Action>) {self.state.increment_seqno();{let mut performer = Performer::new(&mut self.state);- for action in actions {+ for action in actions.drain(..) {performer.perform(action);}}diff --git a/term/src/test/mod.rs b/term/src/test/mod.rsindex 6b0bf1ad..2d2f2b9b 100644--- a/term/src/test/mod.rs+++ b/term/src/test/mod.rs@@ -1474,6 +1474,47 @@ fn styled_alt_screen_scrolling_matches_retained_primary_rows() {}}+#[test]+fn perform_actions_in_place_matches_owned_batches() {+ use wezterm_escape_parser::parser::Parser;+ let input = format!(+ "plain ascii\r\n\x1b[1;31mred\x1b[0m wide 中文 e\u{301}\r\n{}\x1b[2;4r\x1b[4H\n\n\n\x1b[r\+ \x1b]0;title\x07\x1b[38:5:24;48:2:125:136:147mx\x1b[100b\x1b[m\x1b[10A\x1b[3E\x1b[2Kend",+ "a line long enough to wrap in a narrow terminal ".repeat(3)+ );+ for chunk_size in [1, 3, 7, 64, 4096] {+ let mut owned = TestTerm::new(5, 20, 10);+ let mut in_place = TestTerm::new(5, 20, 10);+ let mut owned_parser = Parser::new();+ let mut in_place_parser = Parser::new();+ // One vector for every batch, as the mux parser thread keeps it.+ let mut reused = Vec::new();+ for chunk in input.as_bytes().chunks(chunk_size) {+ owned.perform_actions(owned_parser.parse_as_vec(chunk));+ in_place_parser.parse(chunk, |action| reused.push(action));+ in_place.perform_actions_in_place(&mut reused);+ assert!(reused.is_empty(), "every action is applied and removed");+ // Line equality covers cells, attributes, bits and seqnos.+ assert_eq!(+ in_place.screen().all_lines(),+ owned.screen().all_lines(),+ "chunk_size={chunk_size}"+ );+ assert_eq!(in_place.cursor_pos(), owned.cursor_pos());+ assert_eq!(in_place.current_seqno(), owned.current_seqno());+ assert_eq!(in_place.get_title(), owned.get_title());+ assert_eq!(+ in_place.is_alt_screen_active(),+ owned.is_alt_screen_active()+ );+ }+ assert!(+ reused.capacity() > 0,+ "the allocation stays for the next batch"+ );+ }+}+#[test]fn test_hyperlinks() {let mut term = TestTerm::new(3, 5, 0);