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(term): recycle rows dropped by full-screen scrolls
RoversX6dff1bfa464e
diff --git a/term/src/screen.rs b/term/src/screen.rsindex 2c388331..f628a90c 100644--- a/term/src/screen.rs+++ b/term/src/screen.rs@@ -61,6 +61,8 @@ pub struct Screen {thread_local! {/// Lets tests compare against marking every row on every scroll.pub(crate) static ALWAYS_MARK_REGIONS: std::cell::Cell<bool> = std::cell::Cell::new(false);+ /// Lets tests compare against allocating new rows for full-screen scrolls.+ pub(crate) static NEVER_RECYCLE_FULL_SCREEN: std::cell::Cell<bool> = std::cell::Cell::new(false);}/// How much of the scrollback allowance to pre-reserve at creation. Enough@@ -747,10 +749,14 @@ impl Screen {bidi_mode: BidiMode,) {// Specialize the loop so row recycling does not add work or keep- // extra temporaries live in full-screen or styled scrolling.- if (scroll_region.start != 0 || scroll_region.end as usize != self.physical_rows)- && blank_attr == CellAttributes::blank()- {+ // extra temporaries live in styled scrolling.+ let recycle = blank_attr == CellAttributes::blank();+ #[cfg(test)]+ let recycle = recycle+ && !(NEVER_RECYCLE_FULL_SCREEN.with(|never| never.get())+ && scroll_region.start == 0+ && scroll_region.end as usize == self.physical_rows);+ if recycle {self.scroll_up_impl::<true>(scroll_region, num_rows, seqno, blank_attr, bidi_mode);} else {self.scroll_up_impl::<false>(scroll_region, num_rows, seqno, blank_attr, bidi_mode);@@ -841,10 +847,14 @@ impl Screen {for _ in 0..to_move {let mut line = self.lines.remove(remove_idx).unwrap();let line = if default_blank == blank_attr {- // Keep full-screen output on its existing allocation path:- // recycling there regresses sustained ASCII mux throughput.if RECYCLE && line.len() < self.physical_cols {- line.reset_for_scrolling(seqno, self.physical_cols);+ if scroll_region.start == 0 && insert_at_end {+ // Full-screen rows go on into the scrollback: keep+ // no more than a new row would have allocated.+ line.reset_for_scrolling_as_new(seqno);+ } else {+ line.reset_for_scrolling(seqno, self.physical_cols);+ }line} else {Line::new(seqno)diff --git a/term/src/test/mod.rs b/term/src/test/mod.rsindex 498246e8..18513bad 100644--- a/term/src/test/mod.rs+++ b/term/src/test/mod.rs@@ -1510,6 +1510,41 @@ fn skipped_region_marks_match_marking_every_scroll() {}}+#[test]+fn recycled_full_screen_rows_match_new_rows() {+ use crate::screen::NEVER_RECYCLE_FULL_SCREEN;+ // Full-screen scrolls reuse the discarded row; compare with allocating+ // a new one, on the primary screen (rows go into a full history) and the+ // alternate one, with short, long, wide and styled rows.+ let mut input = String::new();+ for i in 0..40 {+ match i % 5 {+ 0 => input.push_str("y\r\n"),+ 1 => input.push_str(&format!("{}\r\n", "long row ".repeat(3))),+ 2 => input.push_str("界🙂e\u{301}\r\n"),+ 3 => input.push_str("\x1b[44mstyled\x1b[0m\r\n"),+ _ => input.push_str("\r\n"),+ }+ }+ let input = format!("{input}\x1b[?1049h{input}\x1b[?1049l{input}");+ for chunk in [1usize, 9, 64, 100_000] {+ let mut recycling = TestTerm::new(5, 12, 7);+ let mut allocating = TestTerm::new(5, 12, 7);+ for (i, piece) in input.as_bytes().chunks(chunk).enumerate() {+ NEVER_RECYCLE_FULL_SCREEN.with(|never| never.set(false));+ recycling.print(piece);+ NEVER_RECYCLE_FULL_SCREEN.with(|never| never.set(true));+ allocating.print(piece);+ NEVER_RECYCLE_FULL_SCREEN.with(|never| never.set(false));+ assert_eq!(+ recycling.screen().all_lines(),+ allocating.screen().all_lines(),+ "chunk={chunk}, batch={i}"+ );+ }+ }+}+#[test]fn test_region_scrollback_limit() {// Ensure scrollback is truncated properly, when it reaches the line limitdiff --git a/wezterm-surface/src/line/clusterline.rs b/wezterm-surface/src/line/clusterline.rsindex 2d3b46bc..42683e02 100644--- a/wezterm-surface/src/line/clusterline.rs+++ b/wezterm-surface/src/line/clusterline.rs@@ -107,9 +107,12 @@ impl ClusteredLine {true}+ /// The text capacity a new row starts with.+ pub(crate) const NEW_TEXT_CAPACITY: usize = 80;+pub fn new() -> Self {Self {- text: String::with_capacity(80),+ text: String::with_capacity(Self::NEW_TEXT_CAPACITY),is_double_wide: None,clusters: vec![],len: 0,diff --git a/wezterm-surface/src/line/line.rs b/wezterm-surface/src/line/line.rsindex 8f7559e7..f0cac3fa 100644--- a/wezterm-surface/src/line/line.rs+++ b/wezterm-surface/src/line/line.rs@@ -118,14 +118,30 @@ impl Line {/// only bounded compact buffers. Never retain a vector of cells or a large/// historical allocation merely because the row has scrolled off screen.pub fn reset_for_scrolling(&mut self, seqno: SequenceNo, columns: usize) {+ self.reset_for_scrolling_within(seqno, columns.max(80).saturating_mul(2));+ }++ /// `reset_for_scrolling`, keeping a text buffer no larger than a new+ /// row's, for rows that may go on into the scrollback.+ pub fn reset_for_scrolling_as_new(&mut self, seqno: SequenceNo) {+ self.reset_for_scrolling_within(seqno, ClusteredLine::NEW_TEXT_CAPACITY);+ }++ fn reset_for_scrolling_within(&mut self, seqno: SequenceNo, max_text_capacity: usize) {if let CellStorage::C(cl) = &mut self.cells {- if cl.clear_for_scrolling(columns.max(80).saturating_mul(2)) {+ if cl.clear_for_scrolling(max_text_capacity) {self.bits = LineBits::NONE;self.seqno = seqno;self.zones = Vec::new();#[cfg(feature = "appdata")]{- self.appdata = Mutex::new(None);+ // Keep the lock itself, which some platforms allocate on+ // first use; a poisoned one is replaced, as a new row's.+ if self.appdata.is_poisoned() {+ self.appdata = Mutex::new(None);+ } else if let Ok(appdata) = self.appdata.get_mut() {+ *appdata = None;+ }}return;}