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.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
thinkterm-web: warm the fallback canvas after the first frame
RoversXa97fce2f20b0
diff --git a/docs/thinkterm/web-baseline.md b/docs/thinkterm/web-baseline.mdindex d6c53edb..a2080b95 100644--- a/docs/thinkterm/web-baseline.md+++ b/docs/thinkterm/web-baseline.md@@ -24,9 +24,10 @@ The symbols face is larger than the wasm on the wire.|---|---|| DOMContentLoaded | 28-37 ms || first status line | 15-35 ms |-| attached to the pane | 121-142 ms |+| attached to the pane | 121-197 ms |-Loopback; the wasm fetch itself is ~22 ms.+Loopback; the wasm fetch itself is ~22 ms. Attach varies by ±40 ms from run+to run with nothing changed, so treat a difference below that as noise.## Idle@@ -38,24 +39,25 @@ content). The page does not repaint on its own.| | first screen | a second screen of *different* fresh CJK ||---|---|---|-| frames | 2-3 | 2-3 |-| worst gap between frames | 71-79 ms | 30-44 ms |-| long tasks (>50 ms) | one, 70-73 ms | none |-| CPU profile, top self time | `clearRect` 44 ms, glue 24 ms | `fillText` 4 ms, `getImageData` 1 ms |+| frames | 3 | 2-3 |+| worst gap between frames | 23-28 ms | 16 ms |+| long tasks (>50 ms) | none | none |+| CPU profile, top self time | `fillText` 21-25 ms in total | `fillText` ~4 ms |-The long task is a one-time cost of the 2D canvas's first use (the first-`clearRect` waits for the context and its readback pipeline to exist), not-a per-glyph cost: the second cold screen has none. The 8 ms per-frame-fallback budget holds once the canvas exists. A warm-up during font loading-would move those ~45 ms off the first CJK frame.+Before the page warmed the canvas (`GlyphCache::warm`, run 250 ms after the+first frame) the first screen carried one 70-79 ms task, 44 ms of it in the+first `clearRect`: the 2D context and its readback path coming into being.+That was a one-time cost, not per glyph -- a second cold screen never had+it -- and the warm-up moves it to a moment nobody is waiting on. From then+on the 8 ms per-frame fallback budget holds.-Repainting the same CJK from the cache: 3 frames, 19-31 ms apart, no long-task.+Repainting the same CJK from the cache: 2 frames, 11 ms apart.## Sustained output (300 lines, one every 10 ms)-251-253 frames in 5 s; gaps median 17 ms, p95 18 ms, max 23-38 ms. That is-vsync pacing with nothing dropped.+219-253 frames in 5 s; gaps median 17 ms, p95 18 ms. That is vsync pacing+with nothing dropped. The largest gap (23-207 ms) is the python interpreter+starting between the echoed command and its first line, not a paint.## A burst (`seq 1 20000`)diff --git a/thinkterm-web/smoke/bench.js b/thinkterm-web/smoke/bench.jsindex c836ce6c..7411a456 100644--- a/thinkterm-web/smoke/bench.js+++ b/thinkterm-web/smoke/bench.js@@ -143,12 +143,16 @@ const INSTRUMENT = `const pct = (a, p) => { const b = [...a].sort((x, y) => x - y); return Math.round(b[Math.min(b.length - 1, Math.floor(b.length * p))]); };const line = (text) => `printf '%s\\n' '${text}' | ${sendPrefix}`;- const CJK = 'python3 -c "print((\\"中文测试汉字渲染性能基线 한국어 테스트 日本語のテスト ★☆♥ \\" * 2 + chr(10)) * 30, end=\\"\\")"';- const cjk = line(CJK);- const cjk2 = line("clear; " + CJK);+ // The CJK screens come from files and `cat`, not from an interpreter: a+ // python3 start-up inside the window read as a 100-300 ms gap between+ // the echoed command and the output, which is not the page's doing.+ const screen = (name, row) => { const f = `${os.tmpdir()}/tt-bench-${name}.txt`; fs.writeFileSync(f, (row.repeat(2) + "\n").repeat(30)); return f; };+ const cjkFile = screen("cjk1", "中文测试汉字渲染性能基线 한국어 테스트 日本語のテスト ★☆♥ ");+ const cjkFile2 = screen("cjk2", "東京都渋谷区神宮前 대한민국 서울특별시 강남구 北京市朝阳区 ♠♣♦ ");+ const cjk = line(`cat ${cjkFile}`);+ const cjk2 = line(`clear; cat ${cjkFile}`);+ const cjk_other = line(`clear; cat ${cjkFile2}`);const seq = line("seq 1 20000");- const CJK2 = 'python3 -c "print((\\"東京都渋谷区神宮前 대한민국 서울특별시 강남구 北京市朝阳区 ♠♣♦ \\" * 2 + chr(10)) * 30, end=\\"\\")"';- const cjk_other = line("clear; " + CJK2);const trickle = line('python3 -c "import time\nfor i in range(300): print(i, flush=True); time.sleep(0.01)"');const results = {bundle: wasmRes, load: { dom_content_loaded_ms: Math.round(nav.domContentLoadedEventEnd), first_status_ms: tFirst, attached_ms: tAttached },diff --git a/thinkterm-web/src/app.rs b/thinkterm-web/src/app.rsindex dc1be9ab..44f07af9 100644--- a/thinkterm-web/src/app.rs+++ b/thinkterm-web/src/app.rs@@ -851,6 +851,11 @@ impl App {self.request_frame();}+ /// See `GlyphCache::warm`. Called once, after the first frame.+ pub fn warm_glyph_canvas(&self) {+ self.inner.borrow_mut().glyphs.warm();+ }+/// Fit the grid to the canvas's CSS box at the device pixel ratio, and/// tell the server if this browser owns the viewport.pub fn resize(&self) {diff --git a/thinkterm-web/src/glyphs.rs b/thinkterm-web/src/glyphs.rsindex 86601fb6..046af13e 100644--- a/thinkterm-web/src/glyphs.rs+++ b/thinkterm-web/src/glyphs.rs@@ -358,6 +358,20 @@ impl GlyphCache {}}+ /// Pay the canvas's first-use cost now rather than on the first frame+ /// that needs a fallback glyph.+ ///+ /// Measured: the first draw through a fresh 2D context is a single+ /// ~45 ms stall (the context and its readback path coming into being),+ /// and it landed inside the first frame of CJK as a 70 ms task. Every+ /// draw after it is milliseconds. One throwaway glyph here, off the+ /// frame path, moves that stall to where nobody is waiting on it.+ pub fn warm(&mut self) {+ if let Some(scratch) = self.scratch() {+ let _ = scratch.measure("x", 1);+ }+ }+/// Keep what the canvas produced, including that it produced nothing.fn remember(&mut self,diff --git a/thinkterm-web/src/page.rs b/thinkterm-web/src/page.rsindex 41040c6c..7fd8f2d7 100644--- a/thinkterm-web/src/page.rs+++ b/thinkterm-web/src/page.rs@@ -259,6 +259,20 @@ async fn start_attached(let _ = textarea.focus();app.resize();app.request_frame();+ // A moment after the first frame, in its own task: the fallback+ // canvas's first use is a one-off stall, and this is when nobody is+ // waiting on it. Not at 0 ms, which could land before the first paint+ // and the status line, and did show up in the attach time.+ {+ let app = Rc::clone(&app);+ let warm = Closure::once_into_js(move || app.warm_glyph_canvas());+ if let Some(window) = web_sys::window() {+ let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(+ warm.as_ref().unchecked_ref(),+ 250,+ );+ }+ }// The push and close handlers and every DOM listener hold the app.Ok(())}