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): remove per-update scans from the render push path
RoversXa5145ab808ee
diff --git a/thinkterm-session/src/delta_queue.rs b/thinkterm-session/src/delta_queue.rsindex 9ec2897f..d7e6ce29 100644--- a/thinkterm-session/src/delta_queue.rs+++ b/thinkterm-session/src/delta_queue.rs@@ -31,9 +31,9 @@ impl RenderDeltaQueue {!std::mem::replace(&mut self.draining, true)}- /// The next push to apply, with the rows the pushes behind it carry- /// when there are any, or None once the drain is over.- pub fn take(&mut self) -> Option<(GetPaneRenderChangesResponse, Option<RowsCarried>)> {+ /// The next push and whether a newer one waits. Taking an ordinary text+ /// update must not scan every update behind it.+ pub fn take(&mut self) -> Option<(GetPaneRenderChangesResponse, bool)> {let delta = match self.pending.pop_front() {Some(delta) => delta,None => {@@ -41,15 +41,19 @@ impl RenderDeltaQueue {return None;}};- if self.pending.is_empty() {- return Some((delta, None));- }+ Some((delta, !self.pending.is_empty()))+ }++ /// Only needed when skipping an obsolete image actually omitted rows.+ /// The single drain has not popped another push while hydrating this+ /// one, so these are still the pushes that can recover those rows.+ pub fn rows_carried(&self) -> RowsCarried {let mut carried = RowsCarried::default();for newer in &self.pending {carried.rows.extend(newer.bonus_lines.rows());carried.ranges.extend(newer.dirty_lines.iter().cloned());}- Some((delta, Some(carried)))+ carried}/// The drain's task ended without reaching the end of the queue (it@@ -80,15 +84,15 @@ mod render_delta_queue_tests {};assert!(queue.push(delta(1)), "the first push starts the drain");assert!(!queue.push(delta(2)), "the second rides along");- let (first, carried) = queue.take().unwrap();+ let (first, has_newer) = queue.take().unwrap();assert_eq!(first.seqno, 1);assert!(- carried.is_some(),+ has_newer,"so the first is applied without fetching pictures");- let (second, carried) = queue.take().unwrap();+ let (second, has_newer) = queue.take().unwrap();assert_eq!(second.seqno, 2);- assert!(carried.is_none(), "the latest one fetches");+ assert!(!has_newer, "the latest one fetches");assert!(queue.take().is_none(), "and the drain ends");}@@ -125,8 +129,9 @@ mod render_delta_queue_tests {queue.push(delta(2, vec![4], vec![10..12]));queue.push(delta(3, vec![7], vec![]));- let (_, carried) = queue.take().unwrap();- let carried = carried.expect("two pushes wait behind the first");+ let (_, has_newer) = queue.take().unwrap();+ assert!(has_newer, "two pushes wait behind the first");+ let carried = queue.rows_carried();assert!(carried.contains(4), "a bonus row of a later push");assert!(carried.contains(7));assert!(carried.contains(11), "a dirty row of a later push");@@ -135,5 +140,21 @@ mod render_delta_queue_tests {"row 3 is only in the push being applied: left out, it must be marked dirty");assert!(!carried.contains(12), "ranges are half-open");++ queue.take().unwrap();+ let carried = queue.rows_carried();+ assert!(+ !carried.contains(4),+ "an applied update no longer carries a row"+ );+ assert!(!carried.contains(11));+ assert!(carried.contains(7));+ queue.take().unwrap();+ assert!(!queue.rows_carried().contains(7));+ assert!(queue.take().is_none());+ assert!(+ queue.push(delta(4, vec![8], vec![])),+ "a new drain starts after idle"+ );}}diff --git a/thinkterm-session/src/pane.rs b/thinkterm-session/src/pane.rsindex 99da9cc4..8ce180bf 100644--- a/thinkterm-session/src/pane.rs+++ b/thinkterm-session/src/pane.rs@@ -3,7 +3,7 @@//! Detached tasks hold a `Weak` to it; a pane that is gone by the time an//! answer arrives is simply not updated.use crate::clock::Clock;-use crate::delta_queue::{RenderDeltaQueue, RowsCarried};+use crate::delta_queue::RenderDeltaQueue;use crate::host::{request, HostConfig, HostPaneId, PduLink, SessionEvents, SessionHost, Spawner};use crate::hydrate::hydrate_lines;use crate::images::ImageStore;@@ -670,15 +670,15 @@ impl<H: SessionHost> PaneSession<H> {let Some(me) = weak.upgrade() else {return;};- let Some((delta, carried)) = me.render_deltas.lock().take() else {+ let Some((delta, has_newer)) = me.render_deltas.lock().take() else {return;};// A push with a newer one already behind it is applied without// asking for pictures: the newer push names the current ones, and// rows whose pictures are missing keep showing the previous frame.let started = me.now();- let fetched_pictures = carried.is_none();- me.apply_render_delta(delta, carried).await;+ let fetched_pictures = !has_newer;+ me.apply_render_delta(delta, has_newer).await;log::debug!("render push for pane {} applied in {:?} (fetched pictures: {})",me.host_pane_id,@@ -688,7 +688,7 @@ impl<H: SessionHost> PaneSession<H> {}}- /// Apply one push. With `carried` given, newer pushes wait behind this+ /// Apply one push. With `has_newer` set, newer pushes wait behind this/// one and it is applied without fetching pictures; a row it then has/// to leave out is marked dirty unless one of those pushes brings it,/// since the server sent it as a bonus row and nothing else would ever@@ -696,7 +696,7 @@ impl<H: SessionHost> PaneSession<H> {async fn apply_render_delta(self: &Arc<Self>,mut delta: GetPaneRenderChangesResponse,- carried: Option<RowsCarried>,+ has_newer: bool,) {let bonus_lines = std::mem::take(&mut delta.bonus_lines);let (bonus_lines, left_out) = hydrate_lines(@@ -704,10 +704,14 @@ impl<H: SessionHost> PaneSession<H> {&self.images,delta.pane_id,bonus_lines,- carried.is_none(),+ !has_newer,).await;- if let Some(carried) = carried {+ // Rows are only left out when pictures were not fetched, which only+ // happens with a newer push queued behind this one.+ debug_assert!(has_newer || left_out.is_empty());+ if !left_out.is_empty() {+ let carried = self.render_deltas.lock().rows_carried();for row in left_out {if !carried.contains(row) {delta.dirty_lines.push(row..row + 1);@@ -1570,6 +1574,85 @@ mod tests {assert!(session.state().lines.len() <= 256);}+ #[test]+ fn skipped_image_rows_are_recovered_unless_a_later_push_carries_them() {+ let (host, session) = session(&[(1, "recovered"), (2, "must not fetch")]);+ let picture = Arc::new(ImageData::with_raw_data(vec![1, 2, 3]));+ let image_line = || {+ let mut line = Line::from_text("picture", &CellAttributes::default(), 1, None);+ line.cells_mut()[0]+ .attrs_mut()+ .attach_image(Box::new(ImageCell::new(+ TextureCoordinate::new_f32(0., 0.),+ TextureCoordinate::new_f32(1., 1.),+ Arc::clone(&picture),+ )));+ line+ };+ let mut first = delta(1, false, false);+ first.bonus_lines = vec![(1, image_line()), (2, image_line())].into();+ let mut next = delta(2, false, false);+ next.bonus_lines = vec![(+ 2,+ Line::from_text("latest", &CellAttributes::default(), 2, None),+ )]+ .into();+ session.queue_render_delta(first);+ session.queue_render_delta(next);+ host.spawner.run_all();+ assert_eq!(*host.link.asked.borrow(), ["GetLines"]);+ assert_eq!(+ host.link.rows_asked.get(),+ 1,+ "only the uncarried image row is fetched"+ );+ let st = session.state();+ assert!(+ matches!(st.lines.peek(&1), Some(LineEntry::Line(line)) if line.as_str() == "recovered")+ );+ assert!(+ matches!(st.lines.peek(&2), Some(LineEntry::Line(line)) if line.as_str() == "latest")+ );+ assert_eq!(st.seqno, 2);+ }++ #[test]+ fn queued_text_updates_keep_sparse_rows_and_apply_the_final_state() {+ let (host, session) = session(&[]);+ for seqno in 1..=100 {+ let mut update = delta(seqno, false, seqno % 2 == 0);+ let row = (seqno % 24) as StableRowIndex;+ update.bonus_lines = vec![(+ row,+ Line::from_text(+ &format!("update {seqno}"),+ &CellAttributes::default(),+ seqno,+ None,+ ),+ )]+ .into();+ update.title = format!("title {seqno}");+ session.queue_render_delta(update);+ }+ host.spawner.run_all();+ let st = session.state();+ assert_eq!(st.seqno, 100);+ assert_eq!(st.title, "title 100");+ assert!(st.mouse_grabbed);+ for seqno in 77..=100 {+ let row = (seqno % 24) as StableRowIndex;+ assert!(+ matches!(st.lines.peek(&row), Some(LineEntry::Line(line)) if line.as_str() == format!("update {seqno}"))+ );+ }+ assert!(+ host.link.asked.borrow().is_empty(),+ "text needs no recovery fetch"+ );+ assert_eq!(host.events.outputs.get(), 100);+ }+#[test]fn image_hydration_accepts_animation_tails_and_rejects_bad_pixels() {use termwiz::image::ImageDataType;diff --git a/wezterm-mux-server-impl/src/sent_images.rs b/wezterm-mux-server-impl/src/sent_images.rsindex bf35df58..82249208 100644--- a/wezterm-mux-server-impl/src/sent_images.rs+++ b/wezterm-mux-server-impl/src/sent_images.rs@@ -69,6 +69,12 @@ impl SentImages {/// Note every image attached to `lines`, which are about to be sent.pub fn remember(&mut self, lines: &[(StableRowIndex, Line)]) {for (_, line) in lines {+ // Wire rows are compressed before reaching us. Checking their+ // attribute runs avoids a grapheme walk of ordinary text. Keep+ // vector rows on the single-pass path instead of scanning twice.+ if line.is_compressed_for_scrollback() && !line.has_images() {+ continue;+ }for cell in line.visible_cells() {let Some(images) = cell.attrs().images() else {continue;@@ -218,6 +224,41 @@ mod tests {assert!(sent.get(&image(b"two").hash()).is_none());}+ #[test]+ fn compact_rows_keep_images_after_text_and_link_only_rows() {+ use termwiz::hyperlink::Hyperlink;+ let picture = image(b"picture after text");+ for compact in [false, true] {+ let mut text = Line::from_text("plain text", &CellAttributes::default(), 1, None);+ let mut attrs = CellAttributes::default();+ attrs.set_hyperlink(Some(Arc::new(Hyperlink::new("https://example.org"))));+ let mut linked = Line::from_text("a link without pictures", &attrs, 1, None);+ let mut pictured =+ Line::from_text("text then image", &CellAttributes::default(), 1, None);+ pictured.cells_mut()[10]+ .attrs_mut()+ .attach_image(Box::new(ImageCell::new(+ TextureCoordinate::new_f32(0., 0.),+ TextureCoordinate::new_f32(1., 1.),+ Arc::clone(&picture),+ )));+ if compact {+ text.compress_for_scrollback();+ linked.compress_for_scrollback();+ pictured.compress_for_scrollback();+ }+ assert!(!text.has_images());+ assert!(!linked.has_images());+ assert!(linked.has_hyperlinks_or_images());+ assert!(pictured.has_images());+ let mut sent = SentImages::default();+ sent.remember(&[(0, text), (1, linked)]);+ assert!(sent.get(&picture.hash()).is_none());+ sent.remember(&[(2, pictured)]);+ assert!(Arc::ptr_eq(&sent.get(&picture.hash()).unwrap(), &picture));+ }+ }+#[test]fn the_oldest_image_goes_first_when_over_the_byte_budget() {let mut sent = SentImages::with_limits(10, 25);diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rsindex 12f292ab..c94dd81d 100644--- a/wezterm-mux-server-impl/src/sessionhandler.rs+++ b/wezterm-mux-server-impl/src/sessionhandler.rs@@ -318,6 +318,37 @@ struct PaneReading {input_serial: Option<InputSerial>,}+/// Select from owned rows after the pane's terminal lock has been released.+/// Keep dirty-set traversal, mutation and compression out of that lock:+/// under full-screen output these do not save any row copies and would+/// otherwise extend the time the parser has to wait for its terminal.+fn select_dirty_lines(+ first: StableRowIndex,+ lines: Vec<wezterm_term::Line>,+ dirty: &mut rangeset::RangeSet<StableRowIndex>,+) -> Vec<(StableRowIndex, wezterm_term::Line)> {+ let end = first + lines.len() as StableRowIndex;+ let selected = lines+ .into_iter()+ .enumerate()+ .filter_map(|(idx, mut line)| {+ let row = first + idx as StableRowIndex;+ if dirty.contains(row) {+ line.compress_for_scrollback();+ Some((row, line))+ } else {+ None+ }+ })+ .collect();+ // All dirty rows in the actual returned range were copied. Remove+ // the range once instead of allocating bookkeeping for each row.+ if first < end {+ dirty.remove_range(first..end);+ }+ selected+}+/// Read `pane` against what was `last` sent: None when nothing changed and/// nothing forces a push. Takes the pane's terminal lock, several times;/// no `PerPane` lock may be held by the caller.@@ -380,20 +411,7 @@ fn read_pane_changes(dims.physical_top..dims.physical_top + dims.viewport_rows as StableRowIndex;let (first_line, lines) = pane.get_lines(viewport_range);- let mut bonus_lines = lines- .into_iter()- .enumerate()- .filter_map(|(idx, mut line)| {- let stable_row = first_line + idx as StableRowIndex;- if all_dirty_lines.contains(stable_row) {- all_dirty_lines.remove(stable_row);- line.compress_for_scrollback();- Some((stable_row, line))- } else {- None- }- })- .collect::<Vec<_>>();+ let mut bonus_lines = select_dirty_lines(first_line, lines, &mut all_dirty_lines);// Always send the cursor's row, as that tends to the busiest and we don't// have a sequencing concept for our idea of the remote state.@@ -2974,6 +2992,56 @@ mod tests {use wezterm_term::color::ColorPalette;use wezterm_term::TerminalSize;+ #[test]+ fn selecting_dirty_lines_preserves_content_and_outside_invalidation() {+ use rangeset::RangeSet;+ use termwiz::cell::CellAttributes;+ use wezterm_term::Line;++ for changed in [+ vec![],+ vec![101],+ vec![100, 102],+ vec![100, 101, 102, 103],+ ] {+ let mut dirty = RangeSet::new();+ dirty.add_range(10..20);+ dirty.add(104);+ for row in &changed {+ dirty.add(*row);+ }+ let lines = (100..104)+ .map(|row| {+ Line::from_text(&format!("row {row}"), &CellAttributes::default(), 7, None)+ })+ .collect();+ // Use the actual first row returned by the pane, even if a+ // concurrent scroll moved it since the viewport was requested.+ let selected = super::select_dirty_lines(100, lines, &mut dirty);+ assert_eq!(+ selected.iter().map(|(row, _)| *row).collect::<Vec<_>>(),+ changed+ );+ for (row, line) in selected {+ assert_eq!(line.as_str(), format!("row {row}"));+ assert_eq!(line.current_seqno(), 7);+ assert!(line.is_compressed_for_scrollback());+ }+ assert_eq!(+ dirty.iter().cloned().collect::<Vec<_>>(),+ vec![10..20, 104..105]+ );+ }+ }++ #[test]+ fn an_empty_line_read_keeps_dirty_rows_for_recovery() {+ let mut dirty = rangeset::RangeSet::new();+ dirty.add_range(100..104);+ assert!(super::select_dirty_lines(100, vec![], &mut dirty).is_empty());+ assert_eq!(dirty.iter().cloned().collect::<Vec<_>>(), vec![100..104]);+ }+#[test]fn cold_thread_materialization_does_not_require_an_existing_owner() {let request = Pdu::EnsureThinkTermThread(EnsureThinkTermThread {