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(term): append grapheme runs to clustered rows in one write

RoversXcea83f2a795b
diff --git a/term/src/terminalstate/performer.rs b/term/src/terminalstate/performer.rs
index 444c668c..aa53a9fe 100644
--- a/term/src/terminalstate/performer.rs
+++ b/term/src/terminalstate/performer.rs
@@ -35,6 +35,8 @@ pub(crate) struct Performer<'a> {
print: String,
#[cfg(test)]
disable_ascii_batch: bool,
+ #[cfg(test)]
+ disable_grapheme_runs: bool,
}
impl<'a> Deref for Performer<'a> {
@@ -64,6 +66,8 @@ impl<'a> Performer<'a> {
print: String::new(),
#[cfg(test)]
disable_ascii_batch: false,
+ #[cfg(test)]
+ disable_grapheme_runs: false,
}
}
@@ -152,7 +156,23 @@ impl<'a> Performer<'a> {
if ascii_batch {
self.flush_ascii(text);
} else {
+ // Graphemes written one after another at the end of a clustered
+ // row are appended as one run (`Line::append_grapheme_run`). The
+ // writes are only deferred: nothing reads the row before the run
+ // is flushed. Remapped or custom-width text and insert mode keep
+ // writing one grapheme at a time.
+ let runs = !self.insert
+ && charset == CharSet::Ascii
+ && self.unicode_version.cell_widths.is_none();
+ #[cfg(test)]
+ let runs = runs && !self.disable_grapheme_runs;
+ let mut run = GraphemeRun::default();
+ // A row found unable to take a run, until the cursor next wraps.
+ let mut no_run_row = None;
+ let mut offset = 0;
for g in Graphemes::new(text) {
+ let start = offset;
+ offset += g.len();
let g = self.remap_grapheme(g);
let mut print_width = grapheme_column_width(g, Some(&self.unicode_version));
@@ -182,14 +202,17 @@ impl<'a> Performer<'a> {
}
}
+ if self.wrap_next {
+ // Wrapping marks the row and may scroll it away.
+ self.flush_grapheme_run(&mut run, text, seqno);
+ no_run_row = None;
+ }
self.wrap_if_needed();
let x = self.cursor.x;
let y = self.cursor.y;
let width = self.left_and_right_margins.end;
- let pen = self.pen.clone();
-
let wrappable = x + print_width >= width;
if self.insert {
@@ -210,8 +233,22 @@ impl<'a> Performer<'a> {
g,
self.pen
);
- self.screen_mut()
- .set_cell_grapheme(x, y, g, print_width, pen, seqno);
+ let in_run = runs && no_run_row != Some(y) && {
+ if !run.continues(x, y, start) {
+ self.flush_grapheme_run(&mut run, text, seqno);
+ if !self.can_start_grapheme_run(x, y) {
+ no_run_row = Some(y);
+ }
+ }
+ no_run_row != Some(y)
+ };
+ if in_run {
+ run.push(x, y, start, offset, print_width);
+ } else {
+ let pen = self.pen.clone();
+ self.screen_mut()
+ .set_cell_grapheme(x, y, g, print_width, pen, seqno);
+ }
if !wrappable {
self.cursor.x += print_width;
@@ -220,11 +257,60 @@ impl<'a> Performer<'a> {
self.wrap_next = self.dec_auto_wrap;
}
}
+ self.flush_grapheme_run(&mut run, text, seqno);
}
std::mem::swap(&mut self.print, &mut p);
self.print.clear();
}
+ fn can_start_grapheme_run(&mut self, x: usize, y: VisibleRowIndex) -> bool {
+ let screen = self.screen_mut();
+ let idx = screen.phys_row(y);
+ screen.line_mut(idx).can_append_grapheme_run(x)
+ }
+
+ /// Write the graphemes of `run`, which began where
+ /// `can_start_grapheme_run` held and has had nothing written since.
+ fn flush_grapheme_run(
+ &mut self,
+ run: &mut GraphemeRun,
+ text: &str,
+ seqno: wezterm_surface::SequenceNo,
+ ) {
+ if std::mem::take(&mut run.count) == 0 {
+ return;
+ }
+ let run_text = &text[run.start..run.end];
+ let pen = self.pen.clone();
+ let appended = {
+ let screen = self.screen_mut();
+ let idx = screen.phys_row(run.y);
+ screen.line_mut(idx).append_grapheme_run(
+ run.x,
+ run_text,
+ run.max_len,
+ run.cells,
+ &run.wide[..run.num_wide],
+ run.last_width,
+ &pen,
+ seqno,
+ )
+ };
+ if !appended {
+ // Refused for an overflowing cluster width, or a grapheme too
+ // long to tell how the row's text grows; write each grapheme
+ // instead. Zero-width ones in a run are white space, shown as
+ // one cell.
+ let mut x = run.x;
+ for g in Graphemes::new(run_text) {
+ let width = grapheme_column_width(g, Some(&self.unicode_version)).max(1);
+ self.screen_mut()
+ .set_cell_grapheme(x, run.y, g, width, pen.clone(), seqno);
+ x += width;
+ }
+ }
+ }
+
fn wrap_if_needed(&mut self) {
let seqno = self.seqno;
if self.wrap_next {
@@ -1215,6 +1301,73 @@ fn selection_to_selection(sel: Selection) -> ClipboardSelection {
}
}
+/// Graphemes waiting to be appended to one row in a single write.
+struct GraphemeRun {
+ x: usize,
+ y: VisibleRowIndex,
+ /// Byte range of the run within the text being printed.
+ start: usize,
+ end: usize,
+ cells: usize,
+ count: usize,
+ /// Byte length of the longest grapheme.
+ max_len: usize,
+ /// Cell offsets from `x` of the graphemes wider than one cell.
+ wide: [u16; GraphemeRun::MAX],
+ num_wide: usize,
+ last_width: u8,
+}
+
+impl Default for GraphemeRun {
+ fn default() -> Self {
+ Self {
+ x: 0,
+ y: 0,
+ start: 0,
+ end: 0,
+ cells: 0,
+ count: 0,
+ max_len: 0,
+ wide: [0; GraphemeRun::MAX],
+ num_wide: 0,
+ last_width: 0,
+ }
+ }
+}
+
+impl GraphemeRun {
+ const MAX: usize = 64;
+
+ /// Whether a grapheme at `x, y` starting at byte `start` extends the run.
+ fn continues(&self, x: usize, y: VisibleRowIndex, start: usize) -> bool {
+ self.count > 0
+ && self.count < Self::MAX
+ && self.y == y
+ && self.x + self.cells == x
+ && self.end == start
+ }
+
+ fn push(&mut self, x: usize, y: VisibleRowIndex, start: usize, end: usize, width: usize) {
+ if self.count == 0 {
+ self.x = x;
+ self.y = y;
+ self.start = start;
+ self.cells = 0;
+ self.max_len = 0;
+ self.num_wide = 0;
+ }
+ if width > 1 {
+ self.wide[self.num_wide] = self.cells as u16;
+ self.num_wide += 1;
+ }
+ self.max_len = self.max_len.max(end - start);
+ self.cells += width;
+ self.end = end;
+ self.count += 1;
+ self.last_width = width as u8;
+ }
+}
+
#[cfg(all(test, feature = "use_serde"))]
mod ascii_tests {
use super::*;
@@ -1347,4 +1500,85 @@ mod ascii_tests {
}
}
}
+
+ #[test]
+ fn grapheme_runs_match_one_grapheme_at_a_time() {
+ let long = format!("{}e\u{301}{}", "界".repeat(45), "字".repeat(40));
+ // Graphemes longer than a new row's text capacity: runs holding
+ // them are refused and written one at a time.
+ let marked = format!("ab界e{}界x{}y", "\u{301}".repeat(45), "\u{301}".repeat(150));
+ let texts = [
+ "中文,全角!ABC测试".to_string(),
+ "ab界e\u{301}x\u{200b}y\u{2028}z\u{3000} 🙂👍🏽 1\u{fe0f}\u{20e3}é".to_string(),
+ long,
+ marked,
+ ];
+ for (rows, cols) in [(1, 1), (1, 2), (3, 7), (4, 23)] {
+ for setup in [
+ "",
+ "a界",
+ "\x1b[?7l",
+ "\x1b[4h",
+ "\x1b(0",
+ "\x1b)0\x0e",
+ "\x1b[?1049h",
+ "\x1b[44m",
+ "\x1b[2;3r\x1b[3;6H",
+ "\x1b[?69h\x1b[2;6s\x1b[3;7H",
+ "\x1b]8;;https://example.com\x1b\\",
+ "\x1b[3G",
+ "\x1b[1;2H界界",
+ ] {
+ for custom_widths in [false, true] {
+ for text in &texts {
+ for chunk_size in [1, 5, 4096] {
+ let make = || {
+ TerminalState::new(
+ TerminalSize {
+ rows,
+ cols,
+ pixel_width: cols * 8,
+ pixel_height: rows * 16,
+ dpi: 96,
+ },
+ Arc::new(TestConfig {
+ normalize: false,
+ custom_widths,
+ }),
+ "test",
+ "test",
+ Box::new(Vec::new()),
+ )
+ };
+ let mut runs = make();
+ let mut single = make();
+ let mut parser = Parser::new();
+ let input = format!("{setup}{text}\r\n{text}\x1b[A\x1b[3G{text}end");
+ for chunk in input.as_bytes().chunks(chunk_size) {
+ let mut actions = Vec::new();
+ parser.parse_with_borrowed_text(chunk, |action| {
+ action.append_to(&mut actions)
+ });
+ runs.increment_seqno();
+ single.increment_seqno();
+ let mut batched = Performer::new(&mut runs);
+ let mut scalar = Performer::new(&mut single);
+ scalar.disable_grapheme_runs = true;
+ for action in actions {
+ batched.perform(action.clone());
+ scalar.perform(action);
+ }
+ drop(batched);
+ drop(scalar);
+ assert_eq!(runs.snapshot(), single.snapshot(),
+ "rows={rows}, cols={cols}, setup={setup:?}, custom_widths={custom_widths}, text={text:?}, chunk_size={chunk_size}");
+ assert_eq!(runs.screen().all_lines(), single.screen().all_lines(),
+ "rows={rows}, cols={cols}, setup={setup:?}, custom_widths={custom_widths}, text={text:?}, chunk_size={chunk_size}");
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
diff --git a/wezterm-surface/src/line/clusterline.rs b/wezterm-surface/src/line/clusterline.rs
index 242ea333..2d3b46bc 100644
--- a/wezterm-surface/src/line/clusterline.rs
+++ b/wezterm-surface/src/line/clusterline.rs
@@ -299,6 +299,75 @@ impl ClusteredLine {
self.len += cell_width as u32;
}
+ /// `append_grapheme` for each grapheme of a run in turn, all with
+ /// `attrs`: `text` is their concatenation, `max_len` the byte length of
+ /// the longest, `cells` the sum of their widths, `wide` the offsets from
+ /// the current end of those more than one cell wide, and `last_width`
+ /// the width of the final one. The text and the wide-cell set are left
+ /// with the capacities appending one at a time would give them. Returns
+ /// false, changing nothing, if the run would overflow a cluster's width,
+ /// where appending one at a time would start another cluster, or if the
+ /// text's capacity would depend on more than `max_len` and the total.
+ pub fn append_run(
+ &mut self,
+ text: &str,
+ max_len: usize,
+ cells: usize,
+ wide: &[u16],
+ last_width: u8,
+ attrs: &CellAttributes,
+ ) -> bool {
+ let extend = matches!(self.clusters.last(), Some(cluster) if cluster.attrs == *attrs);
+ let prior = if extend {
+ self.clusters
+ .last()
+ .map_or(0, |cluster| cluster.cell_width as usize)
+ } else {
+ 0
+ };
+ if prior + cells > u16::MAX as usize {
+ return false;
+ }
+ let Some(capacity) =
+ grown_capacity(self.text.capacity(), self.text.len() + text.len(), max_len)
+ else {
+ return false;
+ };
+ match self.clusters.last_mut() {
+ Some(cluster) if extend => cluster.cell_width = (prior + cells) as u16,
+ _ => self.clusters.push(Cluster {
+ attrs: attrs.clone(),
+ cell_width: cells as u16,
+ }),
+ }
+ self.text.reserve_exact(capacity - self.text.len());
+ self.text.push_str(text);
+ if let (Some(&first), Some(&last)) = (wide.first(), wide.last()) {
+ let base = self.len as usize;
+ let bitset = self.is_double_wide.get_or_insert_with(|| {
+ Box::new(FixedBitSet::with_capacity(base + first as usize + 1))
+ });
+ // Grown wherever growing for each wide cell in turn would add a
+ // block (32 bits, or a multiple), so it reallocates to the same
+ // sizes.
+ let mut blocks = bitset.len().div_ceil(32);
+ for &offset in wide {
+ let bits = base + offset as usize + 1;
+ if bits.div_ceil(32) > blocks {
+ bitset.grow(bits);
+ blocks = bits.div_ceil(32);
+ }
+ }
+ bitset.grow(base + last as usize + 1);
+ for &offset in wide {
+ bitset.set(base + offset as usize, true);
+ }
+ }
+ self.last_cell_width = NonZeroU8::new(last_width);
+ self.len += cells as u32;
+ true
+ }
+
pub fn append(&mut self, cell: Cell) {
let cell_width = cell.width() as u16;
let new_cluster = match self.clusters.last() {
@@ -445,6 +514,27 @@ impl<'a> Iterator for ClusterLineCellIter<'a> {
}
}
+/// The capacity a String of `capacity` ends with when graphemes of at most
+/// `max_len` bytes are pushed one at a time until it holds `len` bytes, or
+/// None where that would depend on each one's length. A String grows to
+/// twice its capacity, or to what it needs if more, and to at least 8 bytes.
+fn grown_capacity(capacity: usize, len: usize, max_len: usize) -> Option<usize> {
+ if len <= capacity {
+ return Some(capacity);
+ }
+ let mut capacity = match capacity {
+ // The first push takes it to 8 bytes.
+ 0 if max_len <= 8 => 8,
+ // No push then needs more than twice the capacity.
+ 4.. if max_len <= capacity => capacity,
+ _ => return None,
+ };
+ while capacity < len {
+ capacity *= 2;
+ }
+ Some(capacity)
+}
+
#[cfg(test)]
mod test {
use super::*;
diff --git a/wezterm-surface/src/line/line.rs b/wezterm-surface/src/line/line.rs
index da1deff4..8f7559e7 100644
--- a/wezterm-surface/src/line/line.rs
+++ b/wezterm-surface/src/line/line.rs
@@ -800,6 +800,53 @@ impl Line {
self.set_cell_grapheme_nonappend(idx, text, width, attr, seqno);
}
+ /// Whether `append_grapheme_run` can take graphemes starting at `idx`:
+ /// the end of clustered storage without implicit hyperlinks, which the
+ /// first write would otherwise expand to cells.
+ pub fn can_append_grapheme_run(&self, idx: usize) -> bool {
+ match &self.cells {
+ CellStorage::C(cl) => {
+ idx == cl.len() && (self.bits & LineBits::HAS_IMPLICIT_HYPERLINKS) == LineBits::NONE
+ }
+ _ => false,
+ }
+ }
+
+ /// `set_cell_grapheme` for each grapheme of a run written one after
+ /// another from `idx`, all with `attr`; see `ClusteredLine::append_run`
+ /// for the arguments. Returns false, changing nothing, unless
+ /// `can_append_grapheme_run(idx)` and `append_run` takes the run; the
+ /// caller then writes the graphemes one at a time.
+ pub fn append_grapheme_run(
+ &mut self,
+ idx: usize,
+ text: &str,
+ max_len: usize,
+ cells: usize,
+ wide: &[u16],
+ last_width: u8,
+ attr: &CellAttributes,
+ seqno: SequenceNo,
+ ) -> bool {
+ if !self.can_append_grapheme_run(idx) {
+ return false;
+ }
+ let CellStorage::C(cl) = &mut self.cells else {
+ return false;
+ };
+ if !cl.append_run(text, max_len, cells, wide, last_width, attr) {
+ return false;
+ }
+ // What each set_cell_grapheme would do; repeating it changes nothing.
+ if attr.hyperlink().is_some() {
+ self.bits |= LineBits::HAS_HYPERLINK;
+ }
+ self.invalidate_implicit_hyperlinks(seqno);
+ self.invalidate_zones();
+ self.update_last_change_seqno(seqno);
+ true
+ }
+
// Keep padding and overwrites out of the common sequential append path.
#[inline(never)]
fn set_cell_grapheme_nonappend(
diff --git a/wezterm-surface/src/line/test.rs b/wezterm-surface/src/line/test.rs
index fdb50ed1..ed79f328 100644
--- a/wezterm-surface/src/line/test.rs
+++ b/wezterm-surface/src/line/test.rs
@@ -1097,3 +1097,166 @@ fn blank_fill_to_the_end_matches_scalar_writes_then_prune() {
}
}
}
+
+#[test]
+fn grapheme_runs_match_one_grapheme_at_a_time() {
+ let plain = CellAttributes::default();
+ let bold = CellAttributes::default()
+ .set_intensity(wezterm_cell::Intensity::Bold)
+ .clone();
+ let mut linked = CellAttributes::default();
+ linked.set_hyperlink(Some(Arc::new(Hyperlink::new("https://example.org"))));
+ let long_wide = vec![("界", 2); 60];
+ let long_mixed: Vec<(&str, usize)> = (0..40)
+ .map(|i| {
+ if i % 3 == 0 {
+ ("e\u{301}", 1)
+ } else {
+ ("中", 2)
+ }
+ })
+ .collect();
+ // A grapheme longer than a new row's text capacity.
+ let marked = format!("e{}", "\u{301}".repeat(45));
+ let runs: &[&[(&str, usize)]] = &[
+ &[("界", 2)],
+ &[("x", 1)],
+ &[("中", 2), ("文", 2), ("字", 2)],
+ &[
+ ("a", 1),
+ ("é", 1),
+ ("界", 2),
+ ("e\u{301}", 1),
+ ("🙂", 2),
+ ("\u{3000}", 2),
+ ("\u{200b}", 1),
+ ],
+ &long_wide,
+ &long_mixed,
+ &[("界", 2), (&marked, 1), ("x", 1)],
+ ];
+ // Starting rows: empty, narrow or wide text in the same or other
+ // attributes, one scanned without finding a link (still clustered) and
+ // one with an implicit link (expanded to cells, so refused).
+ let prefixes: &[&[(&str, usize)]] = &[
+ &[],
+ &[("a", 1), ("b", 1)],
+ &[("界", 2), ("x", 1)],
+ &[
+ ("h", 1),
+ ("t", 1),
+ ("t", 1),
+ ("p", 1),
+ ("s", 1),
+ (":", 1),
+ ("/", 1),
+ ("/", 1),
+ ("e", 1),
+ (".", 1),
+ ("c", 1),
+ ("o", 1),
+ ],
+ ];
+ let rules = [Rule::new(r"https://\S+", "$0").unwrap()];
+ for prefix in prefixes {
+ for prefix_attrs in [&plain, &bold] {
+ for (scan, compress) in [(false, false), (true, false), (false, true)] {
+ let mut base = Line::new(1);
+ let mut end = 0;
+ for (g, w) in prefix.iter() {
+ base.set_cell_grapheme(end, g, *w, prefix_attrs.clone(), 2);
+ end += w;
+ }
+ if scan {
+ base.scan_and_create_hyperlinks(&rules);
+ }
+ if compress {
+ // Rebuilt compactly, with text capacity cut to its length.
+ base.cells_mut();
+ base.compress_for_scrollback();
+ }
+ // Text capacities: cut to the length (as cloning leaves it),
+ // a few bytes spare, and a new row's.
+ for spare in [0, 3, 80] {
+ let copy = |line: &Line| {
+ let mut line = line.clone();
+ if let crate::line::storage::CellStorage::C(cl) = &mut line.cells {
+ cl.text.reserve_exact(spare);
+ }
+ line
+ };
+ for attrs in [&plain, &bold, &linked] {
+ for run in runs {
+ for idx in [end, end + 1] {
+ let mut expected = copy(&base);
+ let mut x = idx;
+ for (g, w) in run.iter() {
+ expected.set_cell_grapheme(x, g, *w, attrs.clone(), 5);
+ x += w;
+ }
+ let text: String = run.iter().map(|(g, _)| *g).collect();
+ let cells = run.iter().map(|(_, w)| w).sum();
+ let mut wide = vec![];
+ let mut offset = 0;
+ for (_, w) in run.iter() {
+ if *w > 1 {
+ wide.push(offset as u16);
+ }
+ offset += w;
+ }
+ let last = run.last().unwrap().1 as u8;
+ let max_len = run.iter().map(|(g, _)| g.len()).max().unwrap();
+ let mut actual = copy(&base);
+ let (len, capacity) = match &actual.cells {
+ crate::line::storage::CellStorage::C(cl) => {
+ (cl.text.len(), cl.text.capacity())
+ }
+ _ => (0, 0),
+ };
+ let can = actual.can_append_grapheme_run(idx);
+ let appended = actual.append_grapheme_run(
+ idx, &text, max_len, cells, &wide, last, attrs, 5,
+ );
+ // Refused where the text's growth would depend
+ // on each grapheme's length: one longer than the
+ // capacity, or a capacity under 4 bytes.
+ let predictable = len + text.len() <= capacity
+ || if capacity == 0 {
+ max_len <= 8
+ } else {
+ capacity >= 4 && max_len <= capacity
+ };
+ assert_eq!(appended, can && predictable);
+ if appended {
+ assert_eq!(actual, expected);
+ assert_eq!(actual.as_str(), expected.as_str());
+ // The same text capacity is left behind.
+ match (&actual.cells, &expected.cells) {
+ (
+ crate::line::storage::CellStorage::C(a),
+ crate::line::storage::CellStorage::C(e),
+ ) => {
+ assert_eq!(a.text.capacity(), e.text.capacity());
+ }
+ _ => panic!("a run is only taken by clustered storage"),
+ }
+ } else {
+ assert_eq!(actual, base);
+ }
+ // Only a row ending at `idx` in clustered storage takes a run.
+ assert_eq!(
+ can,
+ idx == end
+ && matches!(
+ base.cells,
+ crate::line::storage::CellStorage::C(_)
+ )
+ );
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}