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.

wezterm-client: keep one Arc per image hash, and refetch rows a push left out

RoversX182192ffb431
diff --git a/codec/src/lib.rs b/codec/src/lib.rs
index 5986b90b..1e6ce707 100644
--- a/codec/src/lib.rs
+++ b/codec/src/lib.rs
@@ -1498,6 +1498,11 @@ pub struct SerializedLines {
}
impl SerializedLines {
+ /// The stable rows these lines are for.
+ pub fn rows(&self) -> impl Iterator<Item = StableRowIndex> + '_ {
+ self.lines.iter().map(|(row, _)| *row)
+ }
+
/// Reconsitute hyperlinks or other attributes that were decomposed for
/// serialization, and return the line data.
pub fn extract_data(self) -> (Vec<(StableRowIndex, Line)>, Vec<SerializedImageCell>) {
diff --git a/wezterm-client/src/pane/clientpane.rs b/wezterm-client/src/pane/clientpane.rs
index e0b45889..3f361193 100644
--- a/wezterm-client/src/pane/clientpane.rs
+++ b/wezterm-client/src/pane/clientpane.rs
@@ -454,10 +454,15 @@ impl ClientPane {
}
}
+ /// Apply one push. With `carried` given, newer pushes wait behind this
+ /// one and it is applied without fetching pictures; a row it then has
+ /// to leave out is marked dirty unless one of those pushes brings it,
+ /// since the server sent it as a bonus row and nothing else would ever
+ /// fetch it again.
async fn apply_render_delta(
&self,
mut delta: GetPaneRenderChangesResponse,
- fetch_images: bool,
+ carried: Option<RowsCarried>,
) {
*self.mouse_grabbed.lock() = delta.mouse_grabbed;
*self.alt_screen.lock() = delta.alt_screen;
@@ -465,7 +470,15 @@ impl ClientPane {
let bonus_lines = std::mem::take(&mut delta.bonus_lines);
let client = { Arc::clone(&self.renderable.lock().inner.borrow().client) };
- let bonus_lines = hydrate_lines(client, delta.pane_id, bonus_lines, fetch_images).await;
+ let (bonus_lines, left_out) =
+ hydrate_lines(client, delta.pane_id, bonus_lines, carried.is_none()).await;
+ if let Some(carried) = carried {
+ for row in left_out {
+ if !carried.contains(row) {
+ delta.dirty_lines.push(row..row + 1);
+ }
+ }
+ }
self.renderable
.lock()
@@ -1417,6 +1430,20 @@ struct RenderDeltaQueue {
draining: bool,
}
+/// The rows named by the pushes still waiting in the queue: as bonus rows,
+/// or as dirty ranges the client will fetch on its own.
+#[derive(Default)]
+struct RowsCarried {
+ rows: std::collections::HashSet<StableRowIndex>,
+ ranges: Vec<Range<StableRowIndex>>,
+}
+
+impl RowsCarried {
+ fn contains(&self, row: StableRowIndex) -> bool {
+ self.rows.contains(&row) || self.ranges.iter().any(|range| range.contains(&row))
+ }
+}
+
impl RenderDeltaQueue {
/// Queue `delta`; true when the caller has to start the drain.
fn push(&mut self, delta: GetPaneRenderChangesResponse) -> bool {
@@ -1424,16 +1451,25 @@ impl RenderDeltaQueue {
!std::mem::replace(&mut self.draining, true)
}
- /// The next push to apply and whether another already waits behind it,
- /// or None once the drain is over.
- fn take(&mut self) -> Option<(GetPaneRenderChangesResponse, bool)> {
- match self.pending.pop_front() {
- Some(delta) => Some((delta, !self.pending.is_empty())),
+ /// The next push to apply, with the rows the pushes behind it carry
+ /// when there are any, or None once the drain is over.
+ fn take(&mut self) -> Option<(GetPaneRenderChangesResponse, Option<RowsCarried>)> {
+ let delta = match self.pending.pop_front() {
+ Some(delta) => delta,
None => {
self.draining = false;
- None
+ return None;
}
+ };
+ if self.pending.is_empty() {
+ return Some((delta, None));
+ }
+ let mut carried = RowsCarried::default();
+ for newer in &self.pending {
+ carried.rows.extend(newer.bonus_lines.rows());
+ carried.ranges.extend(newer.dirty_lines.iter().cloned());
}
+ Some((delta, Some(carried)))
}
}
@@ -1443,7 +1479,10 @@ struct DrainingDeltas(PaneId);
impl Drop for DrainingDeltas {
fn drop(&mut self) {
- if let Some(pane) = Mux::get().get_pane(self.0) {
+ let Some(mux) = Mux::try_get() else {
+ return;
+ };
+ if let Some(pane) = mux.get_pane(self.0) {
if let Some(pane) = pane.downcast_ref::<ClientPane>() {
pane.render_deltas.lock().draining = false;
}
@@ -1460,18 +1499,19 @@ async fn drain_render_deltas(local_pane_id: PaneId) {
let Some(pane) = pane.downcast_ref::<ClientPane>() else {
return;
};
- let Some((delta, newer_waiting)) = pane.render_deltas.lock().take() else {
+ let Some((delta, carried)) = pane.render_deltas.lock().take() else {
return;
};
// A push with a newer one already behind it is applied without
// asking for pictures: the newer push names the current ones, and
// rows whose pictures are missing keep showing the previous frame.
let started = std::time::Instant::now();
- pane.apply_render_delta(delta, !newer_waiting).await;
+ let fetched_pictures = carried.is_none();
+ pane.apply_render_delta(delta, carried).await;
log::debug!(
"render push for pane {local_pane_id} applied in {:?} (fetched pictures: {})",
started.elapsed(),
- !newer_waiting
+ fetched_pictures
);
}
}
@@ -1976,18 +2016,62 @@ mod palette_delivery_tests {
};
assert!(queue.push(delta(1)), "the first push starts the drain");
assert!(!queue.push(delta(2)), "the second rides along");
- let (first, newer_waiting) = queue.take().unwrap();
+ let (first, carried) = queue.take().unwrap();
assert_eq!(first.seqno, 1);
assert!(
- newer_waiting,
+ carried.is_some(),
"so the first is applied without fetching pictures"
);
- let (second, newer_waiting) = queue.take().unwrap();
+ let (second, carried) = queue.take().unwrap();
assert_eq!(second.seqno, 2);
- assert!(!newer_waiting, "the latest one fetches");
+ assert!(carried.is_none(), "the latest one fetches");
assert!(queue.take().is_none(), "and the drain ends");
}
+ /// A push applied without pictures leaves rows out; the rows the
+ /// pushes behind it name are the ones something else will bring.
+ #[test]
+ fn a_push_applied_without_pictures_knows_which_rows_the_newer_ones_carry() {
+ use termwiz::surface::Line;
+ let mut queue = super::RenderDeltaQueue::default();
+ let delta =
+ |seqno,
+ bonus: Vec<wezterm_term::StableRowIndex>,
+ dirty: Vec<std::ops::Range<wezterm_term::StableRowIndex>>| {
+ codec::GetPaneRenderChangesResponse {
+ pane_id: 1,
+ mouse_grabbed: false,
+ alt_screen: false,
+ keyboard_encoding: Default::default(),
+ cursor_position: Default::default(),
+ dimensions: Default::default(),
+ dirty_lines: dirty,
+ title: String::new(),
+ working_dir: None,
+ bonus_lines: bonus
+ .into_iter()
+ .map(|row| (row, Line::with_width(1, 0)))
+ .collect::<Vec<_>>()
+ .into(),
+ input_serial: None,
+ seqno,
+ }
+ };
+ queue.push(delta(1, vec![3, 4], vec![]));
+ queue.push(delta(2, vec![4], vec![10..12]));
+ queue.push(delta(3, vec![7], vec![]));
+ let (_, carried) = queue.take().unwrap();
+ let carried = carried.expect("two pushes wait behind the first");
+ assert!(carried.contains(4), "a bonus row of a later push");
+ assert!(carried.contains(7));
+ assert!(carried.contains(11), "a dirty row of a later push");
+ assert!(
+ !carried.contains(3),
+ "row 3 is only in the push being applied: left out, it must be marked dirty"
+ );
+ assert!(!carried.contains(12), "ranges are half-open");
+ }
+
#[test]
fn a_drain_that_ends_early_gives_the_queue_back() {
let queue = std::sync::Arc::new(parking_lot::Mutex::new(super::WriteQueue::default()));
diff --git a/wezterm-client/src/pane/renderable.rs b/wezterm-client/src/pane/renderable.rs
index 15067754..94383344 100644
--- a/wezterm-client/src/pane/renderable.rs
+++ b/wezterm-client/src/pane/renderable.rs
@@ -987,7 +987,7 @@ impl RenderableInner {
let result = match result {
Ok(result) => {
- let lines =
+ let (lines, _) =
hydrate_lines(Arc::clone(&client), remote_pane_id, result.lines, true)
.await;
Ok(lines)
@@ -1217,17 +1217,20 @@ pub(crate) fn forget_images_for_domain(domain_id: DomainId) {
/// nothing is asked of the server: images already held are attached, and
/// rows whose pictures would have to be fetched are left out, so the row
/// the cache already shows stays -- the previous frame rather than a
-/// blank. Used for a push that a newer push has already overtaken.
+/// blank. Used for a push that a newer push has already overtaken. The
+/// rows left out are returned with the lines: the caller has to see that
+/// something brings them, or they would stay as they were for good, since
+/// the push carried them as bonus rows and nothing marks those dirty.
pub(crate) async fn hydrate_lines(
client: Arc<ClientInner>,
pane_id: PaneId,
serialized_lines: SerializedLines,
fetch_images: bool,
-) -> Vec<(StableRowIndex, Line)> {
+) -> (Vec<(StableRowIndex, Line)>, Vec<StableRowIndex>) {
let (mut lines, image_cells) = serialized_lines.extract_data();
if image_cells.is_empty() {
- return lines;
+ return (lines, vec![]);
}
let mut requests = HashMap::new();
@@ -1268,6 +1271,7 @@ pub(crate) async fn hydrate_lines(
}
}
+ let mut left_out = vec![];
if !fetch_images && !requests.is_empty() {
let unfetched: std::collections::HashSet<StableRowIndex> = image_cells
.iter()
@@ -1275,6 +1279,7 @@ pub(crate) async fn hydrate_lines(
.map(|im| im.line_idx)
.collect();
lines.retain(|(idx, _)| !unfetched.contains(idx));
+ left_out = unfetched.into_iter().collect();
requests.clear();
}
@@ -1294,10 +1299,7 @@ pub(crate) async fn hydrate_lines(
for ((asked_for, _, _), data) in asked.into_iter().zip(fetched) {
let Some(data) = data else { continue };
- IMAGE_LRU
- .lock()
- .unwrap()
- .put((domain_id, data.hash()), Arc::clone(&data));
+ let data = file_image(domain_id, data);
// Filed under the hash the cell named as well: the server may have
// answered with the picture now in the cell, whose hash differs.
data_by_hash.insert(asked_for, Arc::clone(&data));
@@ -1331,7 +1333,34 @@ pub(crate) async fn hydrate_lines(
}
}
- line_by_idx.into_iter().collect()
+ (line_by_idx.into_iter().collect(), left_out)
+}
+
+/// File `data` under its hash and return the Arc to attach to cells. When
+/// a copy is already filed there, that copy is brought up to date from
+/// `data` and is the one returned: the glyph cache is keyed by hash and
+/// keeps the first Arc it met, so a second Arc under the same hash would
+/// leave the painter on the old one, blind to every frame that grows in
+/// the new. Two fetches can miss the same hash at once (a row fetch and a
+/// push both naming it), and a fetch answered with the picture now in the
+/// cell lands under a hash that may well be filed already.
+fn file_image(domain_id: DomainId, data: Arc<ImageData>) -> Arc<ImageData> {
+ let key = (domain_id, data.hash());
+ let existing = IMAGE_LRU.lock().unwrap().get(&key).cloned();
+ match existing {
+ Some(existing) if !Arc::ptr_eq(&existing, &data) => {
+ if !super::images::merge_into(&existing, &data, 0, data.generation()) {
+ // The copy holds more than the fetch brought, and is
+ // therefore at least as current as it.
+ existing.set_generation(existing.generation().max(data.generation()));
+ }
+ existing
+ }
+ _ => {
+ IMAGE_LRU.lock().unwrap().put(key, Arc::clone(&data));
+ data
+ }
+ }
}
/// Fetch the image `request` names and bring `held`, the copy already
@@ -1378,7 +1407,17 @@ async fn fetch_image(
if fresh.hash() != whole.data_hash {
// Not the picture asked for but the one now in the cell:
// a newer frame. Its own copy, never merged into the
- // one held for the old hash.
+ // one held for the old hash. It is always sent whole;
+ // a tail of it would be frames counted from a copy of
+ // some other picture, and adopting it as the whole
+ // would leave a truncated animation under its hash.
+ if frames_from != 0 {
+ log::warn!(
+ "the server answered a fetch for one image with part of another; \
+ ignoring it"
+ );
+ return None;
+ }
fresh.set_generation(data_generation);
return Some(fresh);
}
@@ -1405,7 +1444,13 @@ async fn fetch_image(
response = client.client.get_image_cell(GetImageCell { ..whole }).await;
continue;
}
+ // The copy stays, and is marked as current as the server's:
+ // left at its old generation, every later push would find
+ // it behind and fetch the whole image again, for good.
log::debug!("the whole image did not fit the copy held either; keeping the copy");
+ if let Some(held) = &held {
+ held.set_generation(data_generation);
+ }
return held;
}
Ok(GetImageCellResponse { data: None, .. }) => {
@@ -1663,6 +1708,49 @@ impl RenderableState {
mod test {
use super::*;
+ /// The glyph cache keys on the hash and keeps the first Arc it met:
+ /// a hash that is already filed keeps its Arc, brought up to date
+ /// from what was fetched, whatever the fetch brought.
+ #[test]
+ fn a_hash_already_filed_keeps_its_arc_and_grows_it() {
+ let hash = [77u8; 32];
+ let anim = |pixels: &[u8]| termwiz::image::ImageDataType::AnimRgba8 {
+ width: 1,
+ height: 1,
+ durations: vec![std::time::Duration::from_millis(40); pixels.len()],
+ frames: pixels.iter().map(|p| vec![*p; 4]).collect(),
+ hashes: pixels.iter().map(|p| [*p; 32]).collect(),
+ };
+ let frames = |image: &ImageData| super::super::images::frame_count(&image.data());
+ let domain = usize::MAX - 7;
+
+ let first = Arc::new(ImageData::with_data_and_hash(anim(&[1, 2]), hash));
+ let filed = super::file_image(domain, Arc::clone(&first));
+ assert!(
+ Arc::ptr_eq(&filed, &first),
+ "nothing filed yet: this Arc is the one"
+ );
+
+ let longer = Arc::new(ImageData::with_data_and_hash(anim(&[1, 2, 3]), hash));
+ longer.set_generation(3);
+ let filed = super::file_image(domain, longer);
+ assert!(Arc::ptr_eq(&filed, &first), "the Arc already filed is kept");
+ assert_eq!(frames(&first), 3, "and holds what the fetch brought");
+ assert_eq!(first.generation(), 3);
+
+ let shorter = Arc::new(ImageData::with_data_and_hash(anim(&[1]), hash));
+ shorter.set_generation(7);
+ let filed = super::file_image(domain, shorter);
+ assert!(Arc::ptr_eq(&filed, &first));
+ assert_eq!(frames(&first), 3, "a copy never shrinks under the painter");
+ assert_eq!(
+ first.generation(),
+ 7,
+ "but counts as current, or every push would fetch it again"
+ );
+ super::forget_images_for_domain(domain);
+ }
+
fn line(width: usize) -> Line {
Line::with_width(width, SEQ_ZERO)
}