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): reduce row clearing allocations and streamline appends

RoversX05bc32e4fa4d
diff --git a/wezterm-surface/src/line/line.rs b/wezterm-surface/src/line/line.rs
index 488569df..c8398331 100644
--- a/wezterm-surface/src/line/line.rs
+++ b/wezterm-surface/src/line/line.rs
@@ -209,14 +209,8 @@ impl Line {
seqno: SequenceNo,
blank_attr: CellAttributes,
) {
- {
- let cells = self.coerce_vec_storage();
- for c in cells.iter_mut() {
- *c = Cell::blank_with_attrs(blank_attr.clone());
- }
- cells.resize_with(width, || Cell::blank_with_attrs(blank_attr.clone()));
- cells.shrink_to_fit();
- }
+ self.coerce_vec_storage()
+ .resize_and_clear(width, blank_attr);
self.update_last_change_seqno(seqno);
self.invalidate_zones();
self.bits = LineBits::NONE;
@@ -789,15 +783,39 @@ impl Line {
}
if let CellStorage::C(cl) = &mut self.cells {
- if idx > cl.len() && text == " " && attr == CellAttributes::blank() {
- // Appending blank beyond end of line; is already
- // implicitly blank
+ if idx == cl.len() {
+ cl.append_grapheme(text, width, attr);
+ self.invalidate_implicit_hyperlinks(seqno);
+ self.invalidate_zones();
+ self.update_last_change_seqno(seqno);
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.set_cell_grapheme_nonappend(idx, text, width, attr, seqno);
+ }
+
+ // Keep padding and overwrites out of the common sequential append path.
+ #[inline(never)]
+ fn set_cell_grapheme_nonappend(
+ &mut self,
+ idx: usize,
+ text: &str,
+ width: usize,
+ attr: CellAttributes,
+ seqno: SequenceNo,
+ ) {
+ if let CellStorage::C(cl) = &mut self.cells {
+ if idx > cl.len() {
+ if text == " " && attr == CellAttributes::blank() {
+ // Appending blank beyond end of line; is already
+ // 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());
+ }
}
if idx == cl.len() {
cl.append_grapheme(text, width, attr);
@@ -827,6 +845,27 @@ impl Line {
if text.is_empty() {
return true;
}
+ // An append cannot overlap a wide cell. Write the whole run without
+ // allocating attributes for a scalar first cell, unless hyperlink
+ // invalidation can change the storage before the rest of the write.
+ // Single-character writes use the cheaper scalar append below.
+ if text.len() > 1
+ && (self.bits
+ & (LineBits::SCANNED_IMPLICIT_HYPERLINKS | LineBits::HAS_IMPLICIT_HYPERLINKS))
+ == LineBits::NONE
+ {
+ if let CellStorage::C(cl) = &mut self.cells {
+ if idx == cl.len() {
+ cl.append_ascii(text, attr);
+ if attr.hyperlink().is_some() {
+ self.bits |= LineBits::HAS_HYPERLINK;
+ }
+ self.invalidate_zones();
+ self.update_last_change_seqno(seqno);
+ return true;
+ }
+ }
+ }
// Establish the same wide-cell boundary and line metadata as a scalar
// write before taking either storage-specific path.
self.set_cell_grapheme(idx, &text[..1], 1, attr.clone(), seqno);
diff --git a/wezterm-surface/src/line/test.rs b/wezterm-surface/src/line/test.rs
index 90726385..054f9225 100644
--- a/wezterm-surface/src/line/test.rs
+++ b/wezterm-surface/src/line/test.rs
@@ -8,6 +8,47 @@ use alloc::sync::Arc;
use k9::assert_equal as assert_eq;
use wezterm_cell::{Cell, CellAttributes};
+#[test]
+fn grapheme_appends_preserve_gaps_and_wide_overwrites() {
+ let plain = CellAttributes::default();
+ let mut line = Line::new(1);
+ line.set_cell_grapheme(0, "界", 2, plain.clone(), 2);
+ line.set_cell_grapheme(2, "e\u{301}", 1, plain.clone(), 3);
+ assert_eq!(line.as_str(), "界e\u{301}");
+ assert_eq!(line.len(), 3);
+
+ // A default blank beyond the end remains implicit, including metadata.
+ let before = line.clone();
+ line.set_cell_grapheme(6, " ", 1, plain.clone(), 4);
+ assert_eq!(line, before);
+
+ line.set_cell_grapheme(5, "z", 1, plain.clone(), 5);
+ assert_eq!(line.as_str(), "界e\u{301} z");
+ assert_eq!(line.len(), 6);
+ line.set_cell_grapheme(1, "x", 1, plain.clone(), 6);
+ assert_eq!(line.as_str(), " xe\u{301} z");
+ assert_eq!(line.len(), 6);
+
+ // A colored blank must remain visible, including after a storage change.
+ for compact in [false, true] {
+ let mut line = line.clone();
+ if compact {
+ line.compress_for_scrollback();
+ }
+ let mut colored = plain.clone();
+ colored.set_background(wezterm_cell::color::ColorAttribute::PaletteIndex(4));
+ line.set_cell_grapheme(8, " ", 1, colored.clone(), 7);
+ assert_eq!(line.as_str(), " xe\u{301} z ");
+ assert_eq!(line.len(), 9);
+ assert_eq!(line.visible_cells().last().unwrap().attrs(), &colored);
+ assert_eq!(line.compute_shape_hash(), {
+ let mut expected = Line::from_text(" xe\u{301} z ", &plain, 7, None);
+ *expected.cells_mut()[8].attrs_mut() = colored;
+ expected.compute_shape_hash()
+ });
+ }
+}
+
#[test]
fn scroll_reset_discards_oversized_storage() {
let mut line = Line::new(1);
@@ -919,6 +960,32 @@ fn ascii_runs_match_scalar_storage_links_and_wide_boundaries() {
}
}
+#[test]
+fn ascii_append_matches_scalar_for_styled_compact_rows() {
+ let mut rgb = CellAttributes::default();
+ rgb.set_background(
+ wezterm_cell::color::ColorAttribute::TrueColorWithDefaultFallback(
+ (0.2, 0.4, 0.6, 1.0).into(),
+ ),
+ );
+ let mut linked = rgb.clone();
+ linked.set_hyperlink(Some(Arc::new(Hyperlink::new("https://example.org"))));
+ linked.set_semantic_type(wezterm_cell::SemanticType::Prompt);
+ let styles = [CellAttributes::default(), rgb, linked];
+ for prefix in ["", "hello", "界e\u{301}🙂"] {
+ for initial in &styles {
+ let mut base = Line::from_text(prefix, initial, 7, None);
+ base.compress_for_scrollback();
+ base.semantic_zone_ranges();
+ for attrs in &styles {
+ for text in [" ", "abc", " styled text ~ "] {
+ check_ascii_against_scalar(&base, base.len(), text, attrs);
+ }
+ }
+ }
+ }
+}
+
#[test]
fn ascii_append_keeps_compact_storage_and_splits_full_attribute_clusters() {
let mut base = Line::new(0);
diff --git a/wezterm-surface/src/line/vecstorage.rs b/wezterm-surface/src/line/vecstorage.rs
index f2cfcdc1..6f9fbb52 100644
--- a/wezterm-surface/src/line/vecstorage.rs
+++ b/wezterm-surface/src/line/vecstorage.rs
@@ -3,7 +3,7 @@ use alloc::sync::Arc;
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Serialize};
use unicode_segmentation::UnicodeSegmentation;
-use wezterm_cell::Cell;
+use wezterm_cell::{Cell, CellAttributes};
extern crate alloc;
use alloc::vec::Vec;
@@ -19,6 +19,36 @@ impl VecStorage {
Self { cells }
}
+ // Keep the clearing and allocation loops out of the line editing paths.
+ #[inline(never)]
+ pub(crate) fn resize_and_clear(&mut self, width: usize, attrs: CellAttributes) {
+ // Discard removed cells before clearing, rather than allocating new
+ // attributes for cells that will immediately be dropped.
+ self.cells.truncate(width);
+ let is_rgb = |color| {
+ !matches!(
+ color,
+ wezterm_cell::color::ColorAttribute::Default
+ | wezterm_cell::color::ColorAttribute::PaletteIndex(_)
+ )
+ };
+ if is_rgb(attrs.background()) || is_rgb(attrs.foreground()) {
+ let blank = Cell::blank_with_attrs(attrs.clone());
+ for cell in &mut self.cells {
+ cell.clone_from(&blank);
+ }
+ } else {
+ // Replacing lightweight attributes is faster than clone_from;
+ // reserve allocation reuse for heap-backed RGB colors.
+ for cell in &mut self.cells {
+ *cell = Cell::blank_with_attrs(attrs.clone());
+ }
+ }
+ self.cells
+ .resize_with(width, || Cell::blank_with_attrs(attrs.clone()));
+ self.cells.shrink_to_fit();
+ }
+
#[cfg_attr(not(feature = "use_image"), allow(unused_mut, unused_variables))]
pub(crate) fn set_cell(&mut self, idx: usize, mut cell: Cell, clear_image_placement: bool) {
#[cfg(feature = "use_image")]