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.

Stop hashing kitty image payloads three times per frame

RoversX8a8da3042f9f
diff --git a/Cargo.lock b/Cargo.lock
index 6589bf31..1d822fb6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5714,6 +5714,16 @@ dependencies = [
"cfg-if",
"cpufeatures",
"digest",
+ "sha2-asm",
+]
+
+[[package]]
+name = "sha2-asm"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b845214d6175804686b2bd482bcffe96651bb2d1200742b712003504a2dac1ab"
+dependencies = [
+ "cc",
]
[[package]]
diff --git a/term/src/terminalstate/image.rs b/term/src/terminalstate/image.rs
index add6916a..04ac85e2 100644
--- a/term/src/terminalstate/image.rs
+++ b/term/src/terminalstate/image.rs
@@ -262,8 +262,13 @@ impl TerminalState {
if let Some(item) = self.image_cache.get(&key) {
Ok(Arc::clone(item))
} else {
+ // swap_out preserves the hash byte-for-byte (Rgba8/AnimRgba8
+ // pass through; EncodedFile becomes an EncodedLease whose
+ // ContentId is the same SHA-256 of the same bytes), so the key
+ // computed for the cache lookup above is reused instead of
+ // hashing the full payload a second time.
let data = data.swap_out()?;
- let image_data = Arc::new(ImageData::with_data(data));
+ let image_data = Arc::new(ImageData::with_data_and_hash(data, key));
self.image_cache.put(key, Arc::clone(&image_data));
Ok(image_data)
}
diff --git a/wezterm-cell/Cargo.toml b/wezterm-cell/Cargo.toml
index 3cdcc41b..095aa532 100644
--- a/wezterm-cell/Cargo.toml
+++ b/wezterm-cell/Cargo.toml
@@ -23,6 +23,15 @@ wezterm-color-types.workspace = true
wezterm-escape-parser.workspace = true
wezterm-dynamic.workspace = true
+# sha2 only takes its aarch64 hardware-SHA path behind the `asm` feature
+# (x86 enables hardware unconditionally; the asymmetry is sha2's, not ours).
+# Scoped to aarch64 so `sha2-asm` (a C crate) never enters the wasm sentinel
+# or cross-build dependency graphs. The aarch64 implementation is actually
+# pure-Rust intrinsics with runtime detection; sha2-asm just rides along as
+# the feature's carrier.
+[target.'cfg(target_arch = "aarch64")'.dependencies]
+sha2 = {workspace=true, optional=true, features=["asm"]}
+
[features]
std = ["serde/std", "wezterm-char-props/std", "wezterm-color-types/std", "wezterm-escape-parser/std", "wezterm-dynamic/std", "finl_unicode/std"]
use_serde = ["dep:serde", "wezterm-color-types/use_serde"]
diff --git a/wezterm-cell/src/image.rs b/wezterm-cell/src/image.rs
index 04e095a5..19be8933 100644
--- a/wezterm-cell/src/image.rs
+++ b/wezterm-cell/src/image.rs
@@ -308,7 +308,21 @@ impl ImageDataType {
match self {
ImageDataType::EncodedFile(data) => hasher.update(data),
ImageDataType::EncodedLease(lease) => return lease.content_id().as_hash_bytes(),
- ImageDataType::Rgba8 { data, .. } => hasher.update(data),
+ ImageDataType::Rgba8 { data, hash, .. } => {
+ // The stored hash IS hash_bytes(data): the only constructor
+ // computes it and every in-place pixel edit refreshes it
+ // (see terminalstate/kitty.rs), so full-frame payloads are
+ // not re-hashed here — at video-like kitty frame rates that
+ // second SHA-256 dominated the pty read thread. The debug
+ // assert turns any future forgotten refresh into a loud
+ // test failure instead of a stale-image glitch.
+ debug_assert_eq!(
+ *hash,
+ Self::hash_bytes(data),
+ "Rgba8 hash field is stale; a pixel mutation forgot to refresh it"
+ );
+ return *hash;
+ }
ImageDataType::AnimRgba8 {
frames, durations, ..
} => {
@@ -553,7 +567,11 @@ impl ImageData {
Self::with_data_and_hash(ImageDataType::EncodedFile(data).decode(), hash)
}
- fn with_data_and_hash(data: ImageDataType, hash: [u8; 32]) -> Self {
+ /// `hash` must equal `data.compute_hash()`; the caller supplies it to
+ /// avoid re-hashing image payloads that were already hashed for cache
+ /// lookup (a full-frame SHA-256 per kitty transmission otherwise runs
+ /// twice on the pty read thread while it holds the terminal lock).
+ pub fn with_data_and_hash(data: ImageDataType, hash: [u8; 32]) -> Self {
Self {
data: Mutex::new(data),
hash,