Skip to content

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.

perf(surface): drop compact rows on clear and pad gaps in chunks

RoversXeabc101b57ef
diff --git a/wezterm-surface/src/line/line.rs b/wezterm-surface/src/line/line.rs
index c8398331..276e49d4 100644
--- a/wezterm-surface/src/line/line.rs
+++ b/wezterm-surface/src/line/line.rs
@@ -209,6 +209,12 @@ impl Line {
seqno: SequenceNo,
blank_attr: CellAttributes,
) {
+ // All old cells are being discarded. Expanding a compact scrollback
+ // row first would decode its text and clone its styles only to erase
+ // them below. Drop that storage before allocating the replacement.
+ if matches!(&self.cells, CellStorage::C(_)) {
+ self.cells = CellStorage::V(VecStorage::new(Vec::new()));
+ }
self.coerce_vec_storage()
.resize_and_clear(width, blank_attr);
self.update_last_change_seqno(seqno);
@@ -811,11 +817,7 @@ impl Line {
// implicitly blank
return;
}
- while cl.len() < idx {
- // Fill out any implied blanks until we can append
- // their intended cell content
- cl.append_grapheme(" ", 1, CellAttributes::blank());
- }
+ Self::pad_clustered_line(cl, idx);
}
if idx == cl.len() {
cl.append_grapheme(text, width, attr);
@@ -829,6 +831,23 @@ impl Line {
self.set_cell(idx, Cell::new_grapheme_with_width(text, width, attr), seqno);
}
+ // Keep gap filling off the overwrite path, which never needs padding.
+ #[inline(never)]
+ fn pad_clustered_line(cl: &mut ClusteredLine, idx: usize) {
+ // Tabs, cursor movement and LF without CR leave gaps. Their
+ // padding has one attribute run, so append it in chunks instead
+ // of repeating grapheme/attribute bookkeeping for every space.
+ const SPACES: &str = concat!(
+ " ",
+ " "
+ );
+ let blank = CellAttributes::blank();
+ while cl.len() < idx {
+ let count = (idx - cl.len()).min(SPACES.len());
+ cl.append_ascii(&SPACES[..count], &blank);
+ }
+ }
+
/// Write a run of printable ASCII with the same semantics as individual
/// `set_cell_grapheme` calls. Invalid input returns false without mutation.
/// Appending keeps compact storage; overwriting preserves image placements.
@@ -1196,6 +1215,16 @@ impl Line {
matches!(&self.cells, CellStorage::C(_))
}
+ /// Check image attachments without walking the text or cloning image
+ /// references when the line is in compact cluster storage.
+ #[cfg(feature = "use_image")]
+ pub fn has_images(&self) -> bool {
+ match &self.cells {
+ CellStorage::V(cells) => cells.iter().any(|cell| cell.attrs().has_images()),
+ CellStorage::C(cl) => cl.any_cluster_attrs(CellAttributes::has_images),
+ }
+ }
+
/// Whether any cell carries a hyperlink or attached image, answered
/// without decompressing cluster storage. Serialization uses this to
/// leave the great majority of lines, which carry neither, in their
@@ -1206,7 +1235,7 @@ impl Line {
return true;
}
#[cfg(feature = "use_image")]
- if attrs.images().is_some() {
+ if attrs.has_images() {
return true;
}
false
diff --git a/wezterm-surface/src/line/test.rs b/wezterm-surface/src/line/test.rs
index 054f9225..595b42ce 100644
--- a/wezterm-surface/src/line/test.rs
+++ b/wezterm-surface/src/line/test.rs
@@ -49,6 +49,36 @@ fn grapheme_appends_preserve_gaps_and_wide_overwrites() {
}
}
+#[test]
+fn compact_gap_padding_matches_individual_blank_cells() {
+ let mut linked = CellAttributes::default();
+ linked.set_hyperlink(Some(Arc::new(Hyperlink::new("https://example.org"))));
+ for prefix in ["", "ascii", "界🙂e\u{301}"] {
+ let mut base = Line::from_text(prefix, &linked, 1, None);
+ base.compress_for_scrollback();
+ for gap in [0, 1, 7, 63, 64, 127, 128, 129, 65535, 65536] {
+ for (text, width, attrs) in [
+ ("x", 1, CellAttributes::default()),
+ ("界", 2, linked.clone()),
+ ] {
+ let idx = base.len() + gap;
+ let mut expected = base.clone();
+ // Contiguous scalar appends do not use the gap-padding path.
+ while expected.len() < idx {
+ expected.set_cell_grapheme(
+ expected.len(), " ", 1, CellAttributes::blank(), 42,
+ );
+ }
+ expected.set_cell_grapheme(idx, text, width, attrs.clone(), 42);
+ let mut actual = base.clone();
+ actual.set_cell_grapheme(idx, text, width, attrs, 42);
+ assert_eq!(actual, expected, "prefix={prefix:?}, gap={gap}");
+ assert_eq!(actual.compute_shape_hash(), expected.compute_shape_hash());
+ }
+ }
+ }
+}
+
#[test]
fn scroll_reset_discards_oversized_storage() {
let mut line = Line::new(1);