image(perf): hash each transmitted image once (#8065)

ImageDataType::Rgba8 carries the sha256 of its pixels, set wherever one
is built and refreshed wherever the pixels are mutated.
compute_hash always re-hashed the pixels to produce same stored hash.

Image hashing ran 3 times for every transmitted image:
- once in new_single_frame on image creation
- once for the cache key in raw_image_to_image_data
- once more in ImageData::with_data on the miss path

A 2400x1440 RGBA frame is 13.18MiB, so a client transmitting frames at
60fps spends most of its time budget there.

AnimRgba8 keeps hashing, because its stored hashes are per-frame, not
global, which compute_hash needs (all frames + their durations).

---------
Co-authored-by: Ido Samuelson <i-am-logger@users.noreply.github.com>
This commit is contained in:
LoggerandIdo Samuelson authored and GitHub committed 2026-08-19 18:37:03 +02:00
1 parent 33891b4a5e
commit 3ff7522b96
3 files changed
+65 -6

No files matched your search

+2
View File
@@ -152,6 +152,8 @@ As features stabilize some brief notes about them will accumulate here.
search matching. Thanks to @mrdziuban! #7385
#### Fixed
* perf: Terminal images were hashed three times each on the transmit path; the sha256
an RGBA image already carries is now reused instead. Thanks to @i-am-logger! #8065
* `ResetTerminal` (RIS) did not reset the `modifyOtherKeys` state. A program
that left it enabled and exited uncleanly could leave ctrl keys emitting
escape sequences that the shell doesn't expect. RIS now also resets the
+54
View File
@@ -1,6 +1,8 @@
//! Tests for inline image protocol handling
use super::*;
use wezterm_cell::image::ImageDataType;
use wezterm_surface::change::ImageData;
/// A tiny but valid 11x11 PNG, base64 encoded.
/// Taken from the reproduction in <https://github.com/wezterm/wezterm/issues/6344>.
@@ -70,3 +72,55 @@ fn kitty_image_with_zero_pixel_dimensions_does_not_panic() {
term.advance_bytes(b"ok");
assert_visible_contents(&term, file!(), line!(), &["ok", "", ""]);
}
/// A 2x2 RGBA image, base64 encoded.
const TINY_RGBA_BASE64: &str = "AQID/wQFBv8HCAn/CgsM/w==";
/// The image data attached to the first cell that carries one.
fn first_image(term: &TestTerm) -> Arc<ImageData> {
for line in term.screen().visible_lines().iter() {
for cell in line.visible_cells() {
if let Some(im) = cell
.attrs()
.images()
.and_then(|images| images.into_iter().next())
{
return Arc::clone(im.image_data());
}
}
}
panic!("no image was attached to the screen");
}
/// An Rgba8 stores the hash of its pixels and `compute_hash` reports it without
/// recomputing, so a kitty frame transmission that edits those pixels in place
/// must leave the stored hash describing what is now there.
#[test]
fn kitty_frame_edit_keeps_the_stored_hash_current() {
let mut term = TestTerm::new(3, 10, 0);
// a=T: transmit and display, f=32: RGBA, s/v: 2x2 pixels.
let seq = format!("\x1b_Ga=T,t=d,f=32,s=2,v=2,i=1;{}\x1b\\", TINY_RGBA_BASE64);
term.advance_bytes(seq.as_bytes());
let transmitted_hash = first_image(&term).data().compute_hash();
// a=f: transmit a frame, r=1: edit frame 1 in place, painting the top left
// pixel opaque red over the 0x01,0x02,0x03 it arrived with.
term.advance_bytes(b"\x1b_Ga=f,t=d,f=32,s=1,v=1,i=1,r=1,x=0,y=0;/wAA/w==\x1b\\");
let image = first_image(&term);
let image = image.data();
let edited_hash = image.compute_hash();
match &*image {
ImageDataType::Rgba8 { data, .. } => {
// Asserted on the pixels rather than on the hash, so a blit that
// did nothing cannot be mistaken for a hash that went stale.
k9::assert_equal!(&data[0..4], &[0xff, 0x00, 0x00, 0xff][..]);
k9::assert_equal!(edited_hash, ImageDataType::hash_bytes(data));
}
other => panic!("expected Rgba8, got {:?}", other),
}
assert_ne!(edited_hash, transmitted_hash);
}
+9 -6
View File
@@ -216,6 +216,8 @@ pub enum ImageDataType {
data: Vec<u8>,
width: u32,
height: u32,
/// sha256 of data, which is what compute_hash reports. Update it
/// whenever data changes.
hash: [u8; 32],
},
/// Data is an animated sequence
@@ -304,14 +306,15 @@ impl ImageDataType {
pub fn compute_hash(&self) -> [u8; 32] {
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
match self {
ImageDataType::EncodedFile(data) => hasher.update(data),
ImageDataType::EncodedLease(lease) => return lease.content_id().as_hash_bytes(),
ImageDataType::Rgba8 { data, .. } => hasher.update(data),
// Both of these already hold the hash, so neither builds a hasher.
ImageDataType::EncodedLease(lease) => lease.content_id().as_hash_bytes(),
ImageDataType::Rgba8 { hash, .. } => *hash,
ImageDataType::EncodedFile(data) => ImageDataType::hash_bytes(data),
ImageDataType::AnimRgba8 {
frames, durations, ..
} => {
let mut hasher = sha2::Sha256::new();
for data in frames {
hasher.update(data);
}
@@ -320,9 +323,9 @@ impl ImageDataType {
let b = d.to_ne_bytes();
hasher.update(b);
}
hasher.finalize().into()
}
};
hasher.finalize().into()
}
}
/// Divides the animation frame durations by the provided