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
fix(parser): avoid temporary strings for borrowed ASCII runs
RoversXf585821db7b7
diff --git a/mux/src/lib.rs b/mux/src/lib.rsindex f32ff489..eb8894b4 100644--- a/mux/src/lib.rs+++ b/mux/src/lib.rs@@ -27,6 +27,7 @@ use std::sync::{Arc, Weak};use std::thread;use std::time::{Duration, Instant, SystemTime};use termwiz::escape::csi::{DecPrivateMode, DecPrivateModeCode, Device, Mode};+use termwiz::escape::parser::ParsedAction;use termwiz::escape::{Action, CSI};use thiserror::*;use wezterm_term::color::ColorPalette;@@ -669,7 +670,14 @@ fn parse_buffered_data(Ok(size) => {unapplied += size;let work_started = Instant::now();- parser.parse_print_runs(&buf[0..size], |action| {+ parser.parse_with_borrowed_text(&buf[0..size], |event| {+ let action = match event {+ ParsedAction::Action(action) => action,+ text @ ParsedAction::Print(_) => {+ text.append_to(&mut actions);+ return;+ }+ };let mut flush = false;match &action {Action::CSI(CSI::Mode(Mode::SetDecPrivateMode(DecPrivateMode::Code(@@ -3719,7 +3727,9 @@ pub fn spawn_idle_image_sweeper() {log::warn!("idle image sweeper not started: no scheduler configured");return;}- log::debug!("idle image sweeper started: every {IDLE_IMAGE_SWEEP:?}, {IDLE_IMAGE_TICKS} quiet sweeps");+ log::debug!(+ "idle image sweeper started: every {IDLE_IMAGE_SWEEP:?}, {IDLE_IMAGE_TICKS} quiet sweeps"+ );promise::spawn::spawn_into_main_thread(async {loop {smol::Timer::after(IDLE_IMAGE_SWEEP).await;diff --git a/term/src/terminal.rs b/term/src/terminal.rsindex e10ee766..8a8d5b8c 100644--- a/term/src/terminal.rs+++ b/term/src/terminal.rs@@ -1,7 +1,7 @@use super::*;use crate::terminalstate::performer::Performer;use std::sync::Arc;-use wezterm_escape_parser::parser::Parser;+use wezterm_escape_parser::parser::{ParsedAction, Parser};#[derive(Debug, Clone, Copy, PartialEq, Eq)]#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]@@ -169,7 +169,10 @@ impl Terminal {let mut performer = Performer::new(&mut self.state);self.parser- .parse_print_runs(bytes, |action| performer.perform(action));+ .parse_with_borrowed_text(bytes, |event| match event {+ ParsedAction::Action(action) => performer.perform(action),+ ParsedAction::Print(text) => performer.print_text(text),+ });}self.trigger_unseen_output_notif();}diff --git a/term/src/terminalstate/performer.rs b/term/src/terminalstate/performer.rsindex 0a033216..1a136c0c 100644--- a/term/src/terminalstate/performer.rs+++ b/term/src/terminalstate/performer.rs@@ -304,6 +304,14 @@ impl<'a> Performer<'a> {}}+ pub fn print_text(&mut self, text: &str) {+ if let Some(title) = self.accumulating_title.as_mut() {+ title.push_str(text);+ } else {+ self.print.push_str(text);+ }+ }+pub fn perform(&mut self, action: Action) {debug!("perform {:?}", action);if self.suppress_initial_title_change {@@ -321,13 +329,7 @@ impl<'a> Performer<'a> {}match action {Action::Print(c) => self.print(c),- Action::PrintString(s) => {- if let Some(title) = self.accumulating_title.as_mut() {- title.push_str(&s);- } else {- self.print.push_str(&s);- }- }+ Action::PrintString(s) => self.print_text(&s),Action::Control(code) => self.control(code),Action::DeviceControl(ctrl) => self.device_control(ctrl),Action::OperatingSystemCommand(osc) => self.osc_dispatch(*osc),@@ -1295,16 +1297,31 @@ mod ascii_tests {};let mut expected = make();let mut actual = make();+ let mut direct = make();let mut parser = Parser::new();+ let mut direct_parser = Parser::new();let input = format!("{setup}abc #q~0123456789az next\rOVERWRITE\nmore\te\u{301}1\u{fe0f}\u{20e3}界\x1b[0m\x1bkASCII title界\x1b\\end");for chunk in input.as_bytes().chunks(chunk_size) {let mut actions = Vec::new();- parser.parse_print_runs(chunk, |action| {+ parser.parse_with_borrowed_text(chunk, |action| {action.append_to(&mut actions)});actual.increment_seqno();expected.increment_seqno();+ direct.increment_seqno();{+ let mut direct_performer = Performer::new(&mut direct);+ direct_parser.parse_with_borrowed_text(+ chunk,+ |event| match event {+ wezterm_escape_parser::parser::ParsedAction::Action(+ action,+ ) => direct_performer.perform(action),+ wezterm_escape_parser::parser::ParsedAction::Print(+ text,+ ) => direct_performer.print_text(text),+ },+ );let mut scalar = Performer::new(&mut expected);scalar.disable_ascii_batch = true;let mut batched = Performer::new(&mut actual);@@ -1322,6 +1339,8 @@ mod ascii_tests {}assert_eq!(actual.snapshot(), expected.snapshot(),"rows={rows}, cols={cols}, setup={setup:?}, normalize={normalize}, custom_widths={custom_widths}, conpty={conpty}, chunk_size={chunk_size}");+ assert_eq!(direct.snapshot(), expected.snapshot(),+ "direct: rows={rows}, cols={cols}, setup={setup:?}, normalize={normalize}, custom_widths={custom_widths}, conpty={conpty}, chunk_size={chunk_size}");}}}diff --git a/wezterm-escape-parser/examples/print-runs.rs b/wezterm-escape-parser/examples/print-runs.rsnew file mode 100644index 00000000..2b30ca7e--- /dev/null+++ b/wezterm-escape-parser/examples/print-runs.rs@@ -0,0 +1,121 @@+//! Isolate printable-run allocation and callback costs from terminal rendering.+//! Pass `stats` to count allocating calls separately from timing measurements.+//! The `print-buffer` consumer models reusable text accumulation; `mux-coalesce`+//! uses the actual action coalescer. Neither models screen updates or PTY IO.++use std::alloc::{GlobalAlloc, Layout, System};+use std::hint::black_box;+use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::Relaxed};+use std::time::Instant;+use wezterm_escape_parser::{+ Action,+ parser::{ParsedAction, Parser},+};+static COUNT: AtomicBool = AtomicBool::new(false);+static ALLOCS: AtomicUsize = AtomicUsize::new(0);+static REALLOCS: AtomicUsize = AtomicUsize::new(0);+struct Counter;+// SAFETY: every allocation operation delegates unchanged to System; the+// counters only observe calls and do not alter pointers, layouts, or ownership.+unsafe impl GlobalAlloc for Counter {+ unsafe fn alloc(&self, layout: Layout) -> *mut u8 {+ if COUNT.load(Relaxed) {+ ALLOCS.fetch_add(1, Relaxed);+ }+ unsafe { System.alloc(layout) }+ }+ unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {+ unsafe { System.dealloc(ptr, layout) }+ }+ unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {+ if COUNT.load(Relaxed) {+ REALLOCS.fetch_add(1, Relaxed);+ }+ unsafe { System.realloc(ptr, layout, new_size) }+ }+}+#[global_allocator]+static ALLOC: Counter = Counter;+fn run(data: &[u8], mode: u8, mux: bool, iterations: usize) -> usize {+ let mut parser = Parser::new();+ let mut sum = 0usize;+ for _ in 0..iterations {+ let mut prints = String::new();+ if mux {+ let mut actions = Vec::new();+ match mode {+ 0 => parser.parse(black_box(data), |a| a.append_to(&mut actions)),+ 1 => parser.parse_print_runs(black_box(data), |a| a.append_to(&mut actions)),+ _ => {+ parser.parse_with_borrowed_text(black_box(data), |a| a.append_to(&mut actions))+ }+ }+ black_box(&actions);+ sum = sum.wrapping_add(actions.len());+ } else {+ fn apply(a: Action, prints: &mut String, sum: &mut usize) {+ match a {+ Action::Print(c) => prints.push(c),+ Action::PrintString(s) => prints.push_str(&s),+ _ => {+ *sum = sum.wrapping_add(black_box(prints.len()));+ black_box(&prints);+ prints.clear();+ }+ }+ }+ match mode {+ 0 => parser.parse(black_box(data), |a| apply(a, &mut prints, &mut sum)),+ 1 => parser.parse_print_runs(black_box(data), |a| apply(a, &mut prints, &mut sum)),+ _ => parser.parse_with_borrowed_text(black_box(data), |a| match a {+ ParsedAction::Action(a) => apply(a, &mut prints, &mut sum),+ ParsedAction::Print(s) => prints.push_str(s),+ }),+ }+ sum = sum.wrapping_add(black_box(prints.len()));+ black_box(&prints);+ }+ }+ black_box(sum)+}+fn main() {+ let stats = std::env::args().any(|a| a == "stats");+ println!("case,path,mode,rep,bytes,ns,alloc,realloc");+ for n in [2, 4, 8, 16, 64] {+ for kind in ["sgr", "unicode"] {+ let pattern = if kind == "sgr" {+ format!("\x1b[31m{}\x1b[32m{}", "x".repeat(n), "y".repeat(n))+ } else {+ format!("{}中", "x".repeat(n))+ };+ let data = pattern.repeat((65536 / pattern.len()).max(1)).into_bytes();+ for mux in [false, true] {+ let mut a = Vec::new();+ let mut b = Vec::new();+ Parser::new().parse(&data, |v| v.append_to(&mut a));+ Parser::new().parse_with_borrowed_text(&data, |v| v.append_to(&mut b));+ assert_eq!(a, b);+ for rep in 0..if stats { 1 } else { 5 } {+ for mode in if rep % 2 == 0 { [0, 1, 2] } else { [2, 1, 0] } {+ let iters = if stats { 1 } else { 64 };+ ALLOCS.store(0, Relaxed);+ REALLOCS.store(0, Relaxed);+ COUNT.store(stats, Relaxed);+ let start = Instant::now();+ run(&data, mode, mux, iters);+ let ns = start.elapsed().as_nanos();+ COUNT.store(false, Relaxed);+ println!(+ "{kind}-{n},{},{},{rep},{},{ns},{},{}",+ if mux { "mux-coalesce" } else { "print-buffer" },+ ["scalar", "owned", "borrowed"][mode as usize],+ data.len() * iters,+ ALLOCS.load(Relaxed),+ REALLOCS.load(Relaxed)+ );+ }+ }+ }+ }+ }+}diff --git a/wezterm-escape-parser/src/parser/mod.rs b/wezterm-escape-parser/src/parser/mod.rsindex 9c9c79d4..0a7243e0 100644--- a/wezterm-escape-parser/src/parser/mod.rs+++ b/wezterm-escape-parser/src/parser/mod.rs@@ -55,6 +55,50 @@ struct ParseState {tmux_state: Option<RefCell<crate::tmux_cc::Parser>>,}+/// A synchronous parser event. Printable runs borrow the input only for the+/// callback; queued consumers must copy them into their owned action buffer.+#[derive(Debug, PartialEq)]+pub enum ParsedAction<'a> {+ Action(Action),+ Print(&'a str),+}++impl ParsedAction<'_> {+ /// Convert to the owned representation, preserving single-character actions.+ pub fn into_owned(self) -> Action {+ match self {+ Self::Action(action) => action,+ Self::Print(text) => {+ let mut chars = text.chars();+ match (chars.next(), chars.next()) {+ (Some(c), None) => Action::Print(c),+ _ => Action::PrintString(text.to_string()),+ }+ }+ }+ }++ /// Coalesce text directly into the queue's owned string, without allocating+ /// a temporary string for every borrowed run. This also keeps Unicode+ /// grapheme sequences together when an ASCII base precedes a combining mark.+ pub fn append_to(self, dest: &mut Vec<Action>) {+ match self {+ Self::Action(action) => action.append_to(dest),+ Self::Print("") => {}+ Self::Print(text) => match dest.last_mut() {+ Some(Action::PrintString(prior)) => prior.push_str(text),+ Some(Action::Print(prior)) => {+ let mut combined = String::with_capacity(prior.len_utf8() + text.len());+ combined.push(*prior);+ combined.push_str(text);+ *dest.last_mut().unwrap() = Action::PrintString(combined);+ }+ _ => dest.push(Self::Print(text).into_owned()),+ },+ }+ }+}+/// The `Parser` struct holds the state machine that is used to decode/// a sequence of bytes. The byte sequence can be streaming into the/// state machine.@@ -90,25 +134,52 @@ impl Parser {}pub fn parse<F: FnMut(Action)>(&mut self, bytes: &[u8], callback: F) {- self.parse_impl(bytes, callback, false);+ let mut callback = callback;+ self.parse_with_borrowed_text(bytes, |event| match event {+ ParsedAction::Action(action) => callback(action),+ ParsedAction::Print(text) => {+ for byte in text.bytes() {+ callback(Action::Print(byte as char));+ }+ }+ });}/// Like `parse`, but emits contiguous printable ASCII as `PrintString`./// Run boundaries have no semantic meaning: consumers must still combine/// adjacent print actions before Unicode grapheme segmentation.pub fn parse_print_runs<F: FnMut(Action)>(&mut self, bytes: &[u8], callback: F) {- self.parse_impl(bytes, callback, true);+ let mut callback = callback;+ self.parse_with_borrowed_text(bytes, |event| callback(event.into_owned()));}- fn parse_impl<F: FnMut(Action)>(&mut self, bytes: &[u8], mut callback: F, print_runs: bool) {+ /// Parse with borrowed printable ASCII runs, valid only during each callback.+ /// Run boundaries are not grapheme boundaries: append adjacent print events+ /// before segmenting Unicode text. Use `ParsedAction::append_to` for queues.+ ///+ /// The callback cannot retain a borrowed run after it returns:+ /// ```compile_fail+ /// use wezterm_escape_parser::parser::{ParsedAction, Parser};+ /// let mut retained = Vec::new();+ /// Parser::new().parse_with_borrowed_text(b"text", |event| {+ /// if let ParsedAction::Print(text) = event {+ /// retained.push(text);+ /// }+ /// });+ /// ```+ pub fn parse_with_borrowed_text<F: FnMut(ParsedAction<'_>)>(+ &mut self,+ bytes: &[u8],+ mut callback: F,+ ) {#[cfg(feature = "tmux_cc")]let is_tmux_mode: bool = self.state.borrow().tmux_state.is_some();#[cfg(feature = "tmux_cc")]if is_tmux_mode {match self.advance_tmux_bytes(bytes) {Ok(tmux_events) => {- callback(Action::DeviceControl(DeviceControlMode::TmuxEvents(- Box::new(tmux_events),+ callback(ParsedAction::Action(Action::DeviceControl(+ DeviceControlMode::TmuxEvents(Box::new(tmux_events)),)));}Err(err_buf) => {@@ -119,7 +190,6 @@ impl Parser {let mut perform = Performer {callback: &mut callback,state: &mut parser_state,- print_runs,};self.state_machine.parse(unparsed_str.as_bytes(), &mut perform);@@ -131,7 +201,6 @@ impl Parser {let mut perform = Performer {callback: &mut callback,state: &mut self.state.borrow_mut(),- print_runs,};self.state_machine.parse(bytes, &mut perform);}@@ -148,16 +217,15 @@ impl Parser {let mut first_idx = None;{let mut perform = Performer {- callback: &mut |action| {+ callback: &mut |event: ParsedAction<'_>| {// capture the action, but only if it is the first one// we've seen. Preserve an existing one if any.if first.borrow().is_some() {return;}- *first.borrow_mut() = Some(action);+ *first.borrow_mut() = Some(event.into_owned());},state: &mut self.state.borrow_mut(),- print_runs: false,};for (idx, b) in bytes.iter().enumerate() {self.state_machine.parse_byte(*b, &mut perform);@@ -193,9 +261,8 @@ impl Parser {self.state_machine.parse_byte(*b,&mut Performer {- callback: &mut |action| actions.push(action),+ callback: &mut |event: ParsedAction<'_>| actions.push(event.into_owned()),state: &mut self.state.borrow_mut(),- print_runs: false,},);if !actions.is_empty() && self.state_machine.is_ground() {@@ -208,10 +275,9 @@ impl Parser {}}-struct Performer<'a, F: FnMut(Action) + 'a> {+struct Performer<'a, F: FnMut(ParsedAction<'_>) + 'a> {callback: &'a mut F,state: &'a mut ParseState,- print_runs: bool,}fn is_short_dcs(intermediates: &[u8], byte: u8) -> bool {@@ -223,24 +289,28 @@ fn is_short_dcs(intermediates: &[u8], byte: u8) -> bool {}}-impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> {+impl<F: FnMut(ParsedAction<'_>)> Performer<'_, F> {+ fn emit(&mut self, action: Action) {+ (self.callback)(ParsedAction::Action(action));+ }+}++impl<'a, F: FnMut(ParsedAction<'_>)> VTActor for Performer<'a, F> {fn print(&mut self, c: char) {- (self.callback)(Action::Print(c));+ self.emit(Action::Print(c));}fn print_ascii(&mut self, text: &str) {- if self.print_runs && text.len() > 1 {- (self.callback)(Action::PrintString(text.to_string()));+ if text.len() == 1 {+ self.print(text.as_bytes()[0] as char);} else {- for byte in text.bytes() {- self.print(byte as char);- }+ (self.callback)(ParsedAction::Print(text));}}fn execute_c0_or_c1(&mut self, byte: u8) {match FromPrimitive::from_u8(byte) {- Some(code) => (self.callback)(Action::Control(code)),+ Some(code) => self.emit(Action::Control(code)),None => error!("impossible C0/C1 control code {:?} 0x{:x} was dropped",byte as char, byte@@ -250,7 +320,7 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> {fn apc_dispatch(&mut self, data: Vec<u8>) {if let Some(img) = super::KittyImage::parse_apc(&data) {- (self.callback)(Action::KittyImage(Box::new(img)))+ self.emit(Action::KittyImage(Box::new(img)))} else {log::trace!("Ignoring APC data: {:?}", String::from_utf8_lossy(&data));}@@ -284,7 +354,7 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> {self.state.borrow_mut().tmux_state =Some(RefCell::new(crate::tmux_cc::Parser::new()));}- (self.callback)(Action::DeviceControl(DeviceControlMode::Enter(Box::new(+ self.emit(Action::DeviceControl(DeviceControlMode::Enter(Box::new(EnterDeviceControlMode {byte,params: params.to_vec(),@@ -309,8 +379,8 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> {match tmux_parser.advance_byte(data) {Ok(optional_events) => {if let Some(tmux_event) = optional_events {- (self.callback)(Action::DeviceControl(DeviceControlMode::TmuxEvents(- Box::new(vec![tmux_event]),+ (self.callback)(ParsedAction::Action(Action::DeviceControl(+ DeviceControlMode::TmuxEvents(Box::new(vec![tmux_event])),)));}}@@ -321,33 +391,33 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> {}return;}- (self.callback)(Action::DeviceControl(DeviceControlMode::Data(data)));+ self.emit(Action::DeviceControl(DeviceControlMode::Data(data)));}}fn dcs_unhook(&mut self) {if let Some(dcs) = self.state.dcs.take() {- (self.callback)(Action::DeviceControl(+ self.emit(Action::DeviceControl(DeviceControlMode::ShortDeviceControl(Box::new(dcs)),));} else if let Some(mut sixel) = self.state.sixel.take() {sixel.finish();- (self.callback)(Action::Sixel(Box::new(sixel.sixel)));+ self.emit(Action::Sixel(Box::new(sixel.sixel)));} else if let Some(tcap) = self.state.get_tcap.take() {- (self.callback)(Action::XtGetTcap(tcap.finish()));+ self.emit(Action::XtGetTcap(tcap.finish()));} else {- (self.callback)(Action::DeviceControl(DeviceControlMode::Exit));+ self.emit(Action::DeviceControl(DeviceControlMode::Exit));}}fn osc_dispatch(&mut self, osc: &[&[u8]]) {let osc = OperatingSystemCommand::parse(osc);- (self.callback)(Action::OperatingSystemCommand(Box::new(osc)));+ self.emit(Action::OperatingSystemCommand(Box::new(osc)));}fn csi_dispatch(&mut self, params: &[CsiParam], parameters_truncated: bool, control: u8) {for action in CSI::parse(params, parameters_truncated, control as char) {- (self.callback)(Action::CSI(action));+ self.emit(Action::CSI(action));}}@@ -361,7 +431,7 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> {// It doesn't appear to be possible for params.len() > 1 due to the way// that the state machine in vte functions. As such, it also seems to// be impossible for ignored_extra_intermediates to be true too.- (self.callback)(Action::Esc(Esc::parse(+ self.emit(Action::Esc(Esc::parse(if intermediates.len() == 1 {Some(intermediates[0])} else {@@ -788,10 +858,16 @@ mod test {for size in 1..=input.len() {let mut batched = Parser::new();let mut actual = vec![];+ let mut borrowed = Parser::new();+ let mut borrowed_actions = vec![];for chunk in input.chunks(size) {batched.parse_print_runs(chunk, |action| action.append_to(&mut actual));+ borrowed.parse_with_borrowed_text(chunk, |event| {+ event.append_to(&mut borrowed_actions)+ });}assert_eq!(actual, expected, "input={input:?}, chunk={size}");+ assert_eq!(borrowed_actions, expected, "input={input:?}, chunk={size}");}}let mut actions = vec![];@@ -802,6 +878,56 @@ mod test {);}+ #[test]+ fn borrowed_runs_reference_input_but_queued_actions_own_their_text() {+ let mut input = b"ab\x1b[31mcd".to_vec();+ let mut actions = vec![];+ let mut offsets = vec![];+ Parser::new().parse_with_borrowed_text(&input, |event| {+ if let ParsedAction::Print(text) = &event {+ offsets.push(text.as_ptr() as usize - input.as_ptr() as usize);+ }+ event.append_to(&mut actions);+ });+ assert_eq!(offsets, [0, 7]);+ input.fill(b'x');+ assert_eq!(encode(&actions), "ab\x1b[31mcd");+ }++ #[test]+ fn borrowed_runs_preserve_parser_states_for_every_byte() {+ for prefix in [+ &b""[..],+ &b"\x1b"[..],+ &b"\x1b["[..],+ &b"\x1b]0;"[..],+ &b"\x1bP"[..],+ &b"\x1b_"[..],+ &b"\xc3"[..],+ &b"\xf0\x9f"[..],+ ] {+ for byte in 0..=255 {+ let mut input = prefix.to_vec();+ input.push(byte);+ input.extend_from_slice(b"ab\x1b\\tail\x1b[0m");+ let mut expected = vec![];+ Parser::new().parse(&input, |action| action.append_to(&mut expected));+ for size in [1, 2, 7, 64] {+ let mut actual = vec![];+ let mut parser = Parser::new();+ for chunk in input.chunks(size) {+ parser+ .parse_with_borrowed_text(chunk, |event| event.append_to(&mut actual));+ }+ assert_eq!(+ actual, expected,+ "prefix={prefix:?}, byte={byte}, chunk={size}"+ );+ }+ }+ }+ }+fn parse_as(s: &str, expected: &str) -> Vec<Action> {let mut p = Parser::new();let actions = p.parse_as_vec(s.as_bytes());