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): skip re-marking a scroll region already marked this batch
RoversX36b91ccfba5f
diff --git a/term/src/screen.rs b/term/src/screen.rsindex 91f6590b..2c388331 100644--- a/term/src/screen.rs+++ b/term/src/screen.rs@@ -48,6 +48,19 @@ pub struct Screen {pub dpi: u32,pub(crate) saved_cursor: Option<SavedCursor>,++ /// Physical rows that the last region scroll left with every line's+ /// sequence number at least the one given, with no line moved into or+ /// replaced within them since. A further scroll of that region in the+ /// same batch then has no row left to mark. Every other change to the+ /// arrangement of `lines` clears it.+ region_marked: Option<(Range<PhysRowIndex>, SequenceNo)>,+}++#[cfg(test)]+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);}/// How much of the scrollback allowance to pre-reserve at creation. Enough@@ -97,6 +110,7 @@ impl Screen {dpi: size.dpi,keyboard_stack: vec![],saved_cursor: None,+ region_marked: None,}}@@ -143,6 +157,8 @@ impl Screen {pub(crate) fn replace_lines(&mut self, lines: VecDeque<Line>, stable_row_index_offset: usize) {debug_assert!(lines.len() >= self.physical_rows);debug_assert!(lines.len() <= self.line_capacity());+ // Restored lines keep the sequence numbers they were saved with.+ self.region_marked = None;self.lines = lines;self.stable_row_index_offset = stable_row_index_offset;}@@ -247,6 +263,7 @@ impl Screen {seqno: SequenceNo,is_conpty: bool,) -> CursorPosition {+ self.region_marked = None;let physical_rows = size.rows.max(1);let physical_cols = size.cols.max(1);@@ -767,10 +784,22 @@ impl Screen {// of the screen based scrolling, the StableRowIndex does not change,// so we use the scroll region bounds to gate the invalidation.if !scrollback_ok {- // Walk the deque's slices: indexing each row re-derives its slot- // from the deque header, which dominated region scrolling.- for line in self.lines.range_mut(phys_scroll.clone()) {- line.update_last_change_seqno(seqno);+ let marked = self.region_marked.as_ref() == Some(&(phys_scroll.clone(), seqno));+ #[cfg(test)]+ let marked = marked && !ALWAYS_MARK_REGIONS.with(|always| always.get());+ if marked {+ // An earlier scroll of this region in this batch marked+ // every row, and rows have only moved within it since.+ debug_assert!(self+ .lines+ .range(phys_scroll.clone())+ .all(|line| line.current_seqno() >= seqno));+ } else {+ // Walk the deque's slices: indexing each row re-derives its+ // slot from the deque header, which dominated region scrolling.+ for line in self.lines.range_mut(phys_scroll.clone()) {+ line.update_last_change_seqno(seqno);+ }}}@@ -872,9 +901,19 @@ impl Screen {line.update_last_change_seqno(seqno);}}++ // Without history, the rows only rotated within the region: the one+ // leaving it came back as the new bottom row, stamped with `seqno`.+ // Moving rows into history shifts them unmarked, so forget any mark.+ self.region_marked = if scrollback_ok {+ None+ } else {+ Some((phys_scroll, seqno))+ };}pub fn erase_scrollback(&mut self) {+ self.region_marked = None;let len = self.lines.len();let to_clear = len - self.physical_rows;for _ in 0..to_clear {@@ -906,6 +945,7 @@ impl Screen {bidi_mode: BidiMode,) {debug!("scroll_down {:?} {}", scroll_region, num_rows);+ self.region_marked = None;let phys_scroll = self.phys_range(scroll_region);let num_rows = num_rows.min(phys_scroll.end - phys_scroll.start);diff --git a/term/src/test/mod.rs b/term/src/test/mod.rsindex 6361eee1..498246e8 100644--- a/term/src/test/mod.rs+++ b/term/src/test/mod.rs@@ -1450,6 +1450,66 @@ fn region_scrolls_mark_the_region_wherever_the_row_ring_wraps() {}}+#[test]+fn skipped_region_marks_match_marking_every_scroll() {+ use crate::screen::ALWAYS_MARK_REGIONS;+ // Region scrolls in one batch skip rows that an earlier scroll of the+ // same region marked. Interleave everything else that moves, replaces+ // or adds rows, in batches of every size, and compare with marking on+ // every scroll.+ let lf = |n: usize| "x\r\n".repeat(n);+ let steps = [+ format!("\x1b[2;4r\x1b[4;1H{}", lf(9)),+ format!("\x1b[4;1H{}\x1b[2;1H\x1bM\x1bM{}", lf(3), lf(3)),+ format!("\x1b[3;1H\x1b[2L{}\x1b[3;1H\x1b[M{}", lf(4), lf(4)),+ format!("\x1b[2S{}\x1b[T{}", lf(2), lf(2)),+ format!("\x1b[r\x1b[6;1H{}\x1b[2;4r\x1b[4;1H{}", lf(12), lf(5)),+ format!("\x1b[?1049h\x1b[1;3r\x1b[3;1H{}\x1b[?1049l{}", lf(6), lf(6)),+ format!("\x1b[3J\x1b[2;5r\x1b[5;1H{}\x1b#8{}", lf(7), lf(7)),+ format!("\x1b[1;6r\x1b[6;1H{}\x1b[2;4r\x1b[4;1H{}", lf(5), lf(5)),+ format!(+ "\x1b[2;4r\x1b[4;1H\x1bD\x1bD\x1bE{}\x1b[4;1H\x1b[2J{}",+ lf(2),+ lf(3)+ ),+ ];+ let input: String = steps.concat();+ for chunk in [1usize, 5, 17, 64, 100_000] {+ let mut skipping = TestTerm::new(6, 4, 3);+ let mut marking = TestTerm::new(6, 4, 3);+ for (i, piece) in input.as_bytes().chunks(chunk).enumerate() {+ if i % 7 == 3 {+ // A resize between batches moves every row.+ let rows = 5 + i % 3;+ for term in [&mut skipping, &mut marking] {+ term.resize(TerminalSize {+ rows,+ cols: 4,+ pixel_width: 32,+ pixel_height: rows * 16,+ dpi: 0,+ });+ }+ }+ ALWAYS_MARK_REGIONS.with(|always| always.set(false));+ skipping.print(piece);+ ALWAYS_MARK_REGIONS.with(|always| always.set(true));+ marking.print(piece);+ ALWAYS_MARK_REGIONS.with(|always| always.set(false));+ assert_eq!(+ skipping.screen().all_lines(),+ marking.screen().all_lines(),+ "chunk={chunk}, batch={i}"+ );+ assert_eq!(+ skipping.cursor_pos(),+ marking.cursor_pos(),+ "chunk={chunk}, batch={i}"+ );+ }+ }+}+#[test]fn test_region_scrollback_limit() {// Ensure scrollback is truncated properly, when it reaches the line limit