Performance progress
Status: living overview. First entry 2026-09-30, commit 8274c7f plus uncommitted local changes. All numbers come from tools/profiler (150x45 terminal, demo app). Plans and concepts referenced here: sgr-cache-plan.md, cheap-redraws-concept.md.
1. Summary
22 changes have been made so far (changes 15 to 20 are described in their own sections below and not repeated in this table; changes 21 and 22 are the last two rows; in lib/tui.sh, lib/chrome/tui_modal.sh, lib/markup/tui_cache.sh, lib/markup/tui_build.sh and lib/render/tui_rowcache.sh):
| change | what it does | outcome |
|---|---|---|
Memo of _tui._sgr_from |
Caches the colour-to-escape-sequence result per (fg, bg, mods) | Works as designed; saves ~19 ms of a 378 ms theme switch, too small to show in wall time |
Pure-bash _tui._vslice replacing the awk fork in _tui._render_output_buf |
Slices coloured and scrolled pane lines without spawning a process | Removes every fork from hover, focus, click and scroll, and halves subprocess time on page switches; page switch −5 to −11%, scroll −8 to −9% |
Overlay redraw generation fix in _tui_overlay.draw_all (now _tui_layer.draw_all; the main-loop redraw it fixed is gone) |
Stops the main loop from redrawing overlays on every tick | Idle frames 20.2 → 2.0 per second, idle bytes 39.2 → 3.8 KB/s, idle CPU 5.0% → 2.7% of a core |
Interior row built once in _tui._draw_pane_buf |
Bordered panes stop re-resolving the same border/fill styles for every interior row | Pane drawing per full render roughly halved (44 → 22 ms page switch); page switch first visit −8%, palette close −30%, theme first use −8% |
| Skip the per-widget style re-bake on cache replay when the theme overlay is unchanged | tui.cache.replay no longer re-runs tui.class for every widget |
Replay 122 → 92 ms; page switch revisit 303 → 265 ms (−13%), first visit 285 → 260 ms (−9%) (8-round confirmation) |
<bind> tags recorded for cache replay |
Page key bindings (alt+1..0, alt+n) exist on cached pages | alt+2 navigates on a warm start; nav.key now measures a real page switch (368 ms) |
Input settle timeout while a render is queued (TUI_INPUT_SETTLE_TIMEOUT) |
Removes a 50 ms idle wait before queued renders are flushed | Scroll 58 → 18 ms, Page Down 54 → 14 ms, page switch about −35 ms |
Batched stat and fork-free reads in the page cache check and load |
One stat call for any number of files, reads without cat forks |
Warm start 823 → 308 ms (−63%), load cache 361 → 35 ms |
Layout: single-pass content-fit index, no duplicate fit refresh, _eff_border inlined into _inset |
tui.render stops scanning every widget once per pane (twice) |
Layout layer 42 → 26 ms per page switch, 38 → 22 ms per resize; _pane_content_need 13 → 1.3 ms |
Shared pane inset for consecutive widgets (_TUI_WP_REUSE) in the render widget pass and the hit test |
_widget_pos stops recomputing one pane’s inset per widget |
Small: layout 26.7 → 24.0 ms per page switch; hover 9.6 → 8.2 ms, click 11.1 → 10.2 ms |
tui.goto defers tui.render while loading (_TUI_DEFER_RENDER), fork-free docs titles |
Removes the duplicate full render inside on_visit |
components page switch 364 → 276 ms, docs 384 → 287 ms; mean page switch 261 → 240 ms |
Fork-free page build, _tui_cache_class, process-tree kill (/proc children, one grace period) |
Removes about 9,000 forks from the cache build and 250 ms of sleeps from leaving the Terminal page | Cold start 5.54 → 3.11 s (−44%); scrolling page switch 426 → 261 ms (−39%) |
Remaining cold-start forks in the cache recording (deps_of, goto records, _markup_wx attributes, spinner clock) |
Removes 872 more forks from the cold start | Cold start 3.11 → 2.87 s, warm start 303 → 287 ms |
Content-addressed fragment cache (lib/render/tui_rowcache.sh) |
Caches the bytes of bordered panes and label/button/checkbox widgets, keyed on geometry, state, resolved text and a style epoch | Render span −27% in the 40-switch bench; palette close −30%, resize −24%, revisits −15%; first visits slightly slower, output bytes unchanged |
Held keys: tui.focus draws its known dirty set as one raw frame (change 21) |
No row diff on a focus move; pane borders only when the keyboard pane changes | Nav bar hold: work per 1 s 478 → 47 ms, bytes 49.5 → 4.3 KB, per-press lag 8.5 → 4.5 ms; tab hold: work 420 → 60 ms, lag 33.2 → 21.2 ms |
| Arrow-key focus search reads the hit index (change 22) | tui.action.focus_dir uses _TUI_HZ_WR/WC/WW instead of one _tui._widget_pos per focusable per press |
focus_dir 80 → 8 ms per hold action (Scrolling page) |
The profile also changed what we think matters. Colour and theme work is worth about 5% of a page switch at best. The remaining time is the full repaint: Flush, Layout, Render and Style together are about 160 ms of a ~270 ms page switch. The frame-source investigation (section 17) showed that the extra frames per action are small (one 40 KB render plus ~15 KB across the rest), so dirty flags are no longer the lead item; redundant chrome drawing and page loading are.
Preliminary result: first deep run vs current deep run
Status: preliminary. Both runs are --deep full scenarios (5 rounds, traced cold start), taken 2026-09-30 about 2.5 hours apart on the same machine: the first is 20260930-193435 (before any change in this document), the current is 20260930-214915 (after changes 1–7). Medians of the probe-measured latency; budgets are the perceived-instant lines from tools/profiler/scenarios/demo.py.
Latency, all interactions
| interaction | budget | first deep | current deep | change | state |
|---|---|---|---|---|---|
| Cold start | 5000 ms | 5836 ms | 5597 ms | -4% | over (1.1×) |
| Warm start | 1000 ms | 857 ms | 823 ms | -4% | within |
| Page switch, first visit | 150 ms | 356 ms | 228 ms | -36% | over (1.5×) |
| Page switch, revisit | 100 ms | 370 ms | 225 ms | -39% | over (2.3×) |
| Page switch by key (alt+2) | 100 ms | 10.9 ms | 368 ms | +3285% | over (3.7×) |
| Focus next (Tab) | 50.0 ms | 3.05 ms | 3.32 ms | +9% | within |
| Focus previous | 50.0 ms | 2.43 ms | 3.22 ms | +33% | within |
| Mouse hover | 30.0 ms | 9.45 ms | 9.56 ms | +1% | within |
| Mouse click | 100 ms | 11.4 ms | 11.1 ms | -3% | within |
| Wheel scroll, single | 60.0 ms | 64.1 ms | 18.3 ms | -72% | within |
| Wheel scroll, burst | 150 ms | 68.9 ms | 22.9 ms | -67% | within |
| Page Down | 100 ms | 60.2 ms | 14.3 ms | -76% | within |
| Palette open | 100 ms | 29.4 ms | 28.4 ms | -4% | within |
| Palette typing | 50.0 ms | 25.7 ms | 24.4 ms | -5% | within |
| Palette close | 100 ms | 143 ms | 57.5 ms | -60% | within |
| Theme switch, first use | 250 ms | – | 306 ms | new | over (1.2×) |
| Theme switch, repeat | 150 ms | – | 16.1 ms | new | within |
| Terminal resize | 250 ms | 260 ms | 219 ms | -16% | within |
| Quit | 500 ms | 31.5 ms | 54.2 ms | +72% | within |
Read with these caveats: “Page switch by key” did not navigate in the first run (the binding was lost on cached pages), so its first value is a 11 ms no-op and the current 368 ms is a real page switch; the focus steps now start from a different focus state after that fix (see the anomalies below); theme switching did not exist as a scenario in the first run.
xychart-beta
title "Median latency (ms): first deep run vs current deep run"
x-axis ["page first", "page revisit", "palette close", "resize", "scroll step", "scroll burst", "page down"]
y-axis "ms" 0 --> 400
bar [355.5, 370.4, 142.7, 259.9, 64.1, 68.9, 60.2]
bar [227.5, 225.2, 57.5, 218.9, 18.3, 22.9, 14.3]
(First series = first deep run, second = current.)
xychart-beta
title "Idle repaints (frames per second)"
x-axis ["first deep", "current deep"]
y-axis "frames/s" 0 --> 25
bar [20.7, 2.0]
Where the attributed time went (trace, calibrated ms per action)
| scenario | first total → current | Subprocesses | Style | Flush | Layout | Render |
|---|---|---|---|---|---|---|
| page switch, first visit | 319 → 206 | 73 → 37 | 68 → 11 | 46 → 25 | 38 → 42 | 31 → 22 |
| page switch, revisit | 343 → 210 | 79 → 39 | 72 → 11 | 48 → 25 | 45 → 42 | 33 → 22 |
| mouse click | 58 → 46 | 12 → 2 | 8 → 2 | 7 → 6 | 13 → 15 | 5 → 4 |
| scroll, single | 14 → 7 | 13 → 0 | 0 → 0 | 0 → 0 | 0 → 2 | 0 → 2 |
| palette close | 140 → 48 | 29 → 0 | 43 → 4 | 33 → 10 | 10 → 17 | 21 → 9 |
| terminal resize | 149 → 107 | 18 → 3 | 40 → 8 | 32 → 17 | 25 → 38 | 20 → 18 |
pie showData
title Page switch (revisit) attributed ms, first deep run (343 ms)
"Subprocesses" : 79
"Style" : 72
"Flush" : 48
"Layout" : 45
"Render" : 33
"Other" : 66
pie showData
title Page switch (revisit) attributed ms, current deep run (210 ms)
"Subprocesses" : 39
"Style" : 11
"Flush" : 25
"Layout" : 42
"Render" : 22
"Other" : 71
Style (−85%), subprocesses (−50%) and flush (−48%) moved; layout did not (45 → 42 ms) and is now the largest single layer of a page switch together with subprocesses. The calibration check on the current run is 6.5% mean error with −1.0% bias over 60 functions, so the layer numbers are reliable.
What moved, and which change caused it
| outcome | change |
|---|---|
| Scroll, Page Down and wheel bursts −67 to −76% (about 60 → 18 ms) | 7: input settle timeout replaces the 50 ms wait before a queued render is flushed |
| Page switch −36/−39% (355/370 → 228/225 ms) | 2 (awk fork), 4 (pane interior), 5 (style re-bake skip), 7 (settle timeout) |
| Palette close −60% (143 → 58 ms) | 4 (pane interior), 2 |
| Idle 20.7 → 2.0 frames per second, bytes −65% (10.8 → 3.8 KB/s) | 3: overlay redraw generation |
| Style layer −85% in page switches | 1 (_sgr_from memo), 5 (re-bake skip) |
| Subprocess time −50% to −100% across interactions | 2 (awk fork) |
| Startup −4% (cold 5.84 → 5.60 s, warm 857 → 823 ms) | none targeted yet |
Not improved, or worse
- Warm start was essentially unchanged in this run (857 → 823 ms); change 8 below fixed it afterwards (308 ms).
load cacheis 361 ms of it. It is dominated bytui.cache.validstat forks:statis 191 runs and 257 ms per pass in the current run, 119 of those in the warm start. Attributed warm-start time fell from 857 to 445 ms, but wall time did not, because the remaining time is the fork-heavy cache load and validity check. - Cold start is still 5.6 s (over its 5.0 s budget): 4.2 s of it is the cache-building splash.
- Page switches are still over budget (1.5× first visit, 2.3× revisit), and alt+2 at 368 ms is the slowest since it lands on the heavy
componentspage. - Layout is now the biggest layer of a page switch (42 ms;
_tui._widget_pos568× and_tui._inset1,510× per click action in the trace). - Focus Tab / Shift+Tab settle went 3.8 → 9 ms (paint unchanged at 3.3 ms). Its frames now carry a 2.1 KB pane-border redraw instead of 116 B, so focus now moves between panes: the scenario state changed when alt+2 started navigating, not the code. Comparing these two rows across the runs is not valid.
- Shutdown 31.5 → 54 ms. The value is stable (54 in every sample and since
20260930-205743); its cause has not been investigated. - Resize remains noisy (113–244 ms per sample, bimodal by target size); −16% is not a reliable figure.
Open points
- The 687 B frame of the old
nav.keyis gone with the fix; nothing is open there. - Two items from the traced run need a decision:
tui.theme.liststill forks on palette open (3 forks + 1 program,tui_api.sh:671), and thestat-based cache check. - The tests were run by the maintainer after changes 5–7; their results are not recorded in this document.
2. How this was measured
- Baseline:
--deepfull run, 5 rounds, report20260930-193435. Theme baseline: 3 rounds, report20260930-200833. - After: theme
20260930-201013(3 rounds);nav20260930-202352,input20260930-202642,scroll20260930-202655(4 rounds each). - Two kinds of numbers. Wall time is the median latency measured by in-app probes in an untraced run. It is what the user feels. Attributed time comes from the bash trace, rescaled to untraced speed; it says where time goes but excludes waiting. The calibration check against the probes showed 10% mean error and −2% bias on the baseline run, so per-layer numbers are trustworthy; the wall and attributed totals should not be expected to agree.
- Noise. Run-to-run spread is roughly ±3% on medians. Changes smaller than that are not reported as changes.
3. Change 1: memoised _sgr_from
What changed
_tui._sgr_from turns #rrggbb, colour names and modifiers into an escape sequence. It is pure, so its result is now cached under the key fg|bg|mods, bounded at 4096 entries. See sgr-cache-plan.md step 2a.
Expected vs real
| expected (plan) | real | |
|---|---|---|
| Cost per call | ~78 µs → ~3 µs | 69 µs → 27 µs |
_sgr_from self time (theme scenario) |
drops by more than half | 31.8 → 12.5 ms (−61%) |
| Style layer, theme switch first use | −50% or more | 80 → 63 ms (−21%) |
| Theme switch wall time, first use | not stated; plan implied a visible gain | 390 → 378 ms (−3%, within noise) |
| Theme switch wall time, repeat | unchanged | 20.0 → 20.5 ms |
xychart-beta
title "_tui._sgr_from self time, theme scenario (ms)"
x-axis ["before", "after"]
y-axis "ms" 0 --> 40
bar [31.8, 12.5]
Why the result is smaller than expected
- The per-call estimate was too optimistic. Building the
"$1|$2|$3"key and the${var+x}test cost more in bash than assumed. 27 µs includes trace overhead, so the real cost is lower, but nowhere near 3 µs. - The function was a small share of the total. Only ~19 ms of a 378 ms switch. The rest of the Style layer is theme handling, not colour conversion:
| function (theme first use) | self time |
|---|---|
_tui.theme_parse |
13.5 ms |
_tui._sgr_from (after memo) |
12.5 ms |
_tui._style_v |
8.7 ms |
_tui._find_theme_collisions |
8.4 ms |
tui.class |
7.7 ms |
tui.style |
4.7 ms |
_tui.theme_decl |
4.5 ms |
- Where it pays more.
_sgr_fromis called ~2,850 times per pass of the full scenario, not ~460. Page switch Style time already fell from 72 ms to 40 ms (revisit), which is a real but shared gain with the other change.
Verdict
Correct, cheap, keep. It did not move the user-visible number for theme switching. The rest of the plan (_style_v baking, per-theme snapshot) is deferred: the remaining theme cost is ~40 ms inside a rare 378 ms event.
4. Change 2: no awk fork in _render_output_buf
What changed
Any pane with colour codes, scrolling or overflow used to pipe all its lines through awk (one process per pane per render). _tui._vslice does the same slicing in bash. It was checked against the original awk program on 300 cases (plain, coloured, nested and trailing escapes, UTF-8, offsets 0–40, widths 1–80) with no differences, and byte counts per action are identical before and after.
Expected vs real
| expected | real | |
|---|---|---|
| Forks on hover, focus, click, scroll | none | none (subprocess time 3–13 ms → 0 ms; click 12.3 → 1.6 ms) |
| Subprocess time, page switch | roughly halved | 73 → 37 ms (first), 79 → 40 ms (revisit) |
_render_output_buf as top fork site |
gone | gone; scroll scenario has no forks left |
| Cost of the bash replacement | small | ~110–150 µs per call, ~13 ms per page switch (88 calls) |
| Wall time, page switch | clear gain | −11% (first visit), −5% (revisit) |
| Wall time, scroll | clear gain | −8 to −9% |
| Wall time, hover / focus / click | little change (already cheap) | within ±2% |
xychart-beta
title "Subprocess time per action (ms), before vs after"
x-axis ["page first", "page revisit", "click", "focus", "hover", "scroll"]
y-axis "ms" 0 --> 90
bar [73.4, 79.3, 12.3, 2.8, 2.9, 13.0]
bar [36.9, 39.5, 1.6, 0, 0, 0]
(First series = before, second = after.)
xychart-beta
title "Median latency (ms), before vs after"
x-axis ["page first", "page revisit", "scroll step", "scroll burst", "page down"]
y-axis "ms" 0 --> 400
bar [355.5, 370.4, 64.1, 68.9, 60.2]
bar [315.2, 350.6, 58.6, 63.3, 54.5]
Why wall time improved less than compute
Attributed compute for a revisit fell from 343 ms to 269 ms (−22%), but the median latency fell 5%. Part of the compute drop is this change and part is the colour memo (Style 72 → 40 ms); wall time also contains waiting that the trace total does not. Scroll shows the clearest gap: attributed compute per tick went from 13.8 ms to 6.9 ms, yet latency only moved from 64 to 59 ms. The floor of ~55–60 ms is not rendering (4.1 KB per tick, unchanged). It looks like fixed per-cycle waiting (debounce or settle), which has not been traced yet.
Verdict
Achieved its stated goal and is the larger of the two changes. The page switch is still 315–350 ms against a 100 ms budget, so it is not sufficient on its own.
5. Change 3: idle overlay redraw loop
What changed
The main loop redraws overlays (footer, toasts, modals) when _TUI_FLUSH_GEN != _TUI_OVL_GEN. _tui_overlay.draw_all stored the generation before its own flush, and the flush increments it, so the generations differed again immediately and the next tick redrew the overlays, indefinitely. The generation is now stored after the flush, so overlays redraw only when something else has flushed since the last overlay draw. The profiler’s new frame-source view (“Who draws the frames”) found it: 57.7 of the 60.7 flushes per 3 s of idle came from _tui_overlay.draw_all called directly by tui.run.
Expected vs real
| expected | real | |
|---|---|---|
| Idle frames per second | ~1 (the clock tick) | 20.2 → 2.0 |
| Idle bytes per second | small | 39.2 KB → 1.1 KB |
| Idle CPU | lower | 5.0% → 1.3% of a core |
| Probe-measured busy time | lower | 0.35% → 0.02% |
| Other interactions | unchanged | not re-run |
xychart-beta
title "Idle repaints (frames per second)"
x-axis ["before", "after"]
y-axis "frames/s" 0 --> 25
bar [20.2, 2.0]
The remaining 2 per second are 1 main-loop flush and 1 overlay redraw per second (about 0.7 KB each), probably the clock tick.
Notes
- Before numbers come from the 3-round full run, the after number from an idle-only run with the same 3 s idle step. The CPU figure is noisy at this scale.
- Idle is now at the 2 frames per second budget line, not comfortably under it.
Verdict
Matches the expectation. The fix is one line moved plus a comment, and it was invisible to the earlier per-function profile because the cost was many small draws, not a slow function.
6. Change 4: pane interior row built once
What changed
_tui._draw_pane_buf drew every interior row of a bordered pane with about nine emit_* calls that re-resolved the same border and fill styles each time. root is the full-screen pane (about 45 rows), so it cost ~400 calls per render, and its children then draw over that interior. The row body (border, vertical bar, fill, blank, border, bar) is now built once and appended per row after its cursor move. The emitted bytes are identical by construction (same calls, same order, once instead of per row). Found with the new “Where pane drawing goes” view.
Expected vs real
Baseline: full run 20260930-204936 (after change 2, before changes 3 and 4 were measured). After: full run 20260930-205743, 3 rounds each.
| expected | real | |
|---|---|---|
root pane draw, page switch |
~17 → ~5 ms | 16.9 → 7.9 ms |
nav pane draw, page switch |
~14 → ~5 ms | 13.9 → 7.1 ms |
| Pane drawing per full render | −15 to −20 ms | page switch 44.0 → 22.6, palette close 41.3 → 21.3, resize 44.1 → 26.3, theme first use 46.6 → 26.7 ms (−18 to −22 ms) |
| Page switch first visit | ~−20 ms | 320 → 295 ms (−8%) |
| Page switch revisit | ~−20 ms | 339 → 325 ms (−4%) |
| Palette close | ~−20 ms | 88 → 62 ms (−30%) |
| Theme switch first use | ~−20 ms | 380 → 349 ms (−8%) |
| Hover, focus, click, scroll | unchanged | unchanged (±3%) |
xychart-beta
title "Pane drawing per full render (ms), before vs after"
x-axis ["page switch", "palette close", "resize", "theme first use"]
y-axis "ms" 0 --> 50
bar [44.0, 41.3, 44.1, 46.6]
bar [22.6, 21.3, 26.3, 26.7]
(First series = before, second = after.)
Notes
- Pane-level numbers match the expectation; wall-time gains are slightly smaller than the pane savings for page switches (revisit −13 ms against −21 ms of pane drawing), within the ±3% noise of the medians.
- Resize latency is noise. Its median moved 157 → 207 ms, but the three resize targets swap between ~115–150 ms and ~260–440 ms from run to run (baseline
20260930-193435: 260/156/266;204936: 113/440/150;205743: 207/149/230). Pane drawing for resize fell 44 → 26 ms, so this is not a regression; the resize metric itself needs more rounds or a steadier measurement. - Later runs settle the pane-drawing numbers. Total pane drawing per page switch was 22.6 ms in
205743, then 12.0 ms (210746), and 11.7 ms in the 3-round and 8-roundnavruns (root3.2 ms,nav3.5 ms each time). The 22.6 ms run looks like an outlier (machine load), and the only pre-change value is 44.0 ms from one run (204936), so “44 → ~12 ms” is the best estimate, not a precise one. - Shutdown went 120 → 54 ms, probably because the idle loop no longer competes with the quit key. Not investigated.
Verdict
Matches the expectation at pane level and gives a solid 20–30 ms on every full repaint. Two later 2-round nav runs showed root and nav at about 3 ms each, lower again; not explained (machine load is a candidate), so not counted here.
7. Change 5: skip the style re-bake on cache replay
What changed
A cache replay restores the page snapshot (which already contains every widget’s baked _TUI_STYLE_*) and then re-ran tui.class for every widget to pick up theme changes: ~25 ms per page switch, traced in 20260930-211857. The page snapshot now records the theme overlay the styles were baked under (_TUI_STYLE_SIG, in lib/markup/tui_cache.sh; the _TUI_STYLE_ prefix puts it in the snapshot automatically). On replay the re-bake is skipped when it equals the current overlay. tui.load_theme still runs, so the class table stays current. Snapshots without a signature, or recorded under a different overlay, re-bake as before.
Expected vs real
Baseline: nav run 20260930-211235 (3 rounds, untraced). After: nav run 20260930-213033 (8 rounds, 88 actions per scenario) and theme run 20260930-212622 (3 rounds). An earlier 3-round nav run after the change (20260930-212520) gave the same picture (259 / 261 ms).
| expected | real | |
|---|---|---|
tui.cache.replay (wall) |
~−25 ms | 122 → 92 ms (−30 ms; 88 ms first visit) |
tui.load_cached (wall) |
~−25 ms | 133 → 103 ms (99 ms first visit) |
| Page switch, revisit | ~−25 ms | 303 → 265 ms (−13%) |
| Page switch, first visit | ~−25 ms | 285 → 260 ms (−9%) |
| Theme switch, first use | unchanged (overlay active, signature mismatches) | 337 → 329 ms (within noise) |
| Theme switch, repeat | unchanged | 17 → 18 ms |
xychart-beta
title "Page switch median latency (ms), before vs after"
x-axis ["first visit", "revisit"]
y-axis "ms" 0 --> 320
bar [285, 303]
bar [260, 265]
(First series = before, second = after.)
Notes
- The gain is slightly larger than the trace estimate (−30 ms against −25 ms), plausibly because the re-bake also ran
tui.styleand debug logging per class. - Pages still differ widely (8-round medians, first visit):
scrolling479 ms,docs421,components419,monitor337, againstcase_study199 ms,home215 andlayout220. This spread is the page mix, not noise (see section 18);on_visitpage code (46–49 ms on average) is the likely driver. - With a theme overlay active the signature does not match (the cache builder records pages without the overlay), so the re-bake still runs on every replay there. Fixing it means re-capturing the snapshot after a re-bake.
- Not covered by this measurement: visual equality. Run
tests/unit/cache.t.shand the golden frames (tools/t_golden.sh) before relying on it.
Verdict
Matches the expectation and a bit more. The theme switch is unchanged, as intended.
8. Change 6: <bind> tags survive a cache replay
What changed
<bind> tags (alt+1..0, alt+n and every other page binding) are registered while the markup is built. A cache replay skips tag dispatch and the page snapshot does not include the binding tables, so on any cached page, which is every warm start, the bindings did not exist. _tui_build.tag.bind (lib/markup/tui_build.sh) now records a ready-to-eval tui.bind … call in the list that replay already runs for nav-button handlers, so replay re-creates the bindings (and the existing disk persistence carries them).
Expected vs real
| expected | real | |
|---|---|---|
| alt+2 navigates on a warm start | yes | yes (verified in a live session: alt+2 → Components, alt+3 → Settings) |
The bimodal 11 ms / 67 ms nav.key timing |
gone | gone: all 5 samples 363–371 ms, a real page switch |
| Cost of the extra replayed binds | negligible | not separated out in the profile (bind calls are not among the hot functions) |
xychart-beta
title "alt+2 samples (ms), 5 rounds: before vs after the fix"
x-axis ["r1", "r2", "r3", "r4", "r5"]
y-axis "ms" 0 --> 400
bar [10.9, 67.4, 10.9, 67.4, 10.9]
bar [368, 370, 363, 371, 368]
(First series = before, where the key did nothing or echoed on the Home page; second = after, a real page switch to Components.)
Notes
- The 687 B frame and the 67 ms samples were the Home page’s key echo consuming the key when the binding was missing.
nav.keynow measures a full page switch tocomponents(368 ms, 29 forks + 5 programs per action in the trace), so it is over budget and not comparable with its earlier values.- Key goto vs click goto (deep run
20260930-231054, same page): alt+2 tocomponentstakes 229 ms against 242 ms (first visit) and 260 ms (revisit) for clicking the same page, so the key path is not slower; it is 15–30 ms faster. The click path spends its first 18 ms on hover and press handling (goto starts at 29 ms against 11 ms for the key) and writes 7 frames against 3.componentsis one of the two heaviest pages (on_visit62 ms, render 79 ms), which is whynav.keylooks slow next to the page-switch median.nav.keyis a separate group and is not part of the page-switch median or mean. The one real difference is first visible feedback: a click paints the button highlight after about 14 ms, a key press shows nothing until the new page renders (194 ms forcomponents, 83 ms for a light page). The profiler scenario now also presses alt+7 (case_study, a light page): 114 ms against 131 ms for clicking it. - Caches already on disk from before the change lack the recorded binds until they are rebuilt; there is no cache format version. Clearing
$TUI_HOME/cache/pages/rebuilds them.
Verdict
A correctness fix, found by the profiler’s own “key does nothing” finding. The latency numbers of this scenario change meaning and should be read as page-switch numbers from here on.
9. Change 7: input settle timeout for queued renders
What changed
A queued pane render (tui.output, scroll batching) is flushed by the main loop once the input goes quiet. The loop decided that by waiting the full TUI_INPUT_POLL_TIMEOUT (0.05 s while ticking) for the next byte, so every scroll step and every page load with queued output paid an idle wait of about 50 ms before its last frame. While a render is pending the loop now polls with TUI_INPUT_SETTLE_TIMEOUT (0.01 s, lib/tui.sh). Found with the new “Timeline of one page switch” view: a 50 ms gap between the release event and _tui._flush_pending_render.
Expected vs real
| expected | real (current deep run vs previous full runs) | |
|---|---|---|
| Wheel scroll, single | 58 → ~18 ms | 58.6 → 18.3 ms (−69%) |
| Wheel scroll, burst | 63 → ~23 ms | 63.5 → 22.9 ms (−64%) |
| Page Down | 54 → ~15 ms | 54.5 → 14.3 ms (−74%) |
| Page switch, any page with queued output | ~−40 ms | first visit 260 → 228 ms, revisit 265 → 225 ms (−32 and −40 ms) |
| Hover, click, palette typing | unchanged | unchanged |
xychart-beta
title "Scroll and page switch median (ms) before/after the settle timeout"
x-axis ["scroll step", "scroll burst", "page down", "page first", "page revisit"]
y-axis "ms" 0 --> 280
bar [58.6, 63.5, 54.5, 260, 265]
bar [18.3, 22.9, 14.3, 228, 225]
(First series = before, second = after; before = nav/scroll runs 20260930-212520 and full run 20260930-210746, after = current deep run.)
Notes
- This was the “scroll floor” noted earlier as not rendering: scroll ticks wrote 4.1 KB and computed for about 7 ms, and the remaining 50 ms was the wait.
- Risk not covered by the profiler: a slow physical mouse-wheel spin with notches more than 10 ms apart now flushes per notch instead of batching. The scripted bursts arrive in one write and do not show it. If scrolling feels choppy, raise
TUI_INPUT_SETTLE_TIMEOUT(for example 0.02).
Verdict
Matches the expectation and was the largest single latency win per interaction, for a one-variable change.
10. Change 8: batched stat and fork-free cache loading
What changed
All in lib/markup/tui_cache.sh:
_tui_cache_stat_manyreads the mtimes of any number of files with onestatcall.tui.cache.validused one fork per dependency and now uses one per check;tui.cache.signaturealso uses it.tui.cache.load_dirstats every dependency of every cached page once (_TUI_CACHE_MTIME_FRESH=1), andtui.cache.validreads that snapshot untilstart_cachedfinishes its start-up check and clears the flag. The per-page-switch check stats again, so edits made while the app runs are still noticed. The rule itself is unchanged: the mtime must equal the recorded one exactly.tui.cache.load_dirreads its eight files per page with_tui_cache_slurp(a builtinread) instead of$(<file)/$(cat file), about 176 forks for 22 pages.- Two
basenameforks per page instart_cachedreplaced with parameter expansion.
Found in the traced deep run (20260930-214915): 191 stat runs and 257 ms per pass, 119 of them in the warm start, plus about 176 read forks in load_dir.
Expected vs real
Baseline: deep run 20260930-214915. After: startup run 20260930-215938 (5 rounds, traced).
| expected | real | |
|---|---|---|
stat processes in the warm start |
~119 → a handful | 119 → 2 (plus 1 in the validation gate) |
load cache phase |
361 → well under 200 ms | 361 → 35 ms |
| Warm start, ready | 823 → ~600 ms or less | 823 → 308 ms (−63%; samples 305–310) |
| First output on screen | earlier | 666 → 156 ms |
| Attributed warm-start time | 445 → lower | 445 → 121 ms; subprocess time 354 → 50 ms |
| Cold start | unchanged | 5.60 → 5.54 s (unchanged) |
xychart-beta
title "Warm start phases (ms), before vs after"
x-axis ["boot+lib", "validate", "load cache", "init term", "first page", "first frame"]
y-axis "ms" 0 --> 400
bar [68, 4, 361, 21, 94, 45]
bar [68, 4, 35, 21, 89, 44]
(First series = before, second = after.)
Notes
- The phase dropped by 326 ms. The traced estimates (about 280 ms for the stat forks, 130–180 ms for the read forks) overlap, so they cannot be simply added, but together they account for nearly all of the phase.
- The remaining warm start is the library load (64 ms), the first page (89 ms), the terminal init (21 ms) and the first frame (44 ms), about 300 ms in total with almost no subprocesses left (8 from
_tui._calc_bounds, 7 instart_cached). - The cold start did not move. It is 1.08 s of page validation plus 4.18 s of cache building in the splash (
markup1.95 s attributed, process 0.5 s in the trace). It only matters on first launch or after an update, but it is now the single largest remaining startup cost. - Paths containing spaces are still not supported by the cache signature; that limitation pre-dates this change.
Verdict
A much larger gain than expected for the effort: the warm start is now well inside its 1 s budget (308 ms, about 3×). It came from removing process spawns, not from faster code.
11. Change 9: layout, content-fit index and _inset
What changed
All in lib/tui.sh. The traced deep run (20260930-214915) put _tui._pane_content_need at the top of the layout layer: 194 calls at 668 µs, about 130 ms per pass. It scans every widget to size one pane, and a full render ran it twice per leaf (once in tui.render’s up-front refresh, once inside _tui._draw_pane_buf): panes × widgets × 2 (the components page has 36 panes and 44 widgets).
_tui._content_need_indexcomputes the per-pane maxima for all widgets in one pass.tui.renderbuilds it before its refresh loop;_tui._pane_content_needreads it while_TUI_CN_ON=1and uses the old loop otherwise.tui.rendermarks_TUI_FIT_FRESHafter the up-front pass, and_tui._draw_pane_bufskips the second refresh for leaf panes (containers and standalone draws still refresh)._tui._eff_borderis inlined into_tui._inset, which every widget position and pane rect calls (1,510 times in the deep run); the two must stay in step.
Verified by a differential test on six real demo pages (121 panes): the index result, the loop result and the original function from git agree in every case, and so do _inset and its original (242 comparisons, 0 mismatches).
Expected vs real
Baseline: deep run 20260930-214915. After: nav+palette+resize run 20260930-221040 (3 rounds, traced, calibration error 6.8%). Attributed ms per action.
| expected | real | |
|---|---|---|
| Layout layer, page switch | 42 → ~25 ms | 42 → 26–27 ms (first visit 41.6 → 26.4, revisit 41.7 → 26.7) |
| Layout layer, resize | 38 → ~22 ms | 37.9 → 22.4 ms |
| Layout layer, palette close | 17 → ~10 ms | 17.3 → 10.1 ms |
_tui._pane_content_need |
~13 → ~1 ms | 13.1 → 1.3 ms (page switch), 14.3 → 0.9 ms (resize) |
_tui._inset + _tui._eff_border |
~15 → ~11 ms | 15.2 → 12.5 ms (page switch; _inset 9.9 → 11.3 absorbed most of _eff_border’s 5.3 → 1.2) |
| Page switch, wall, first visit | ~−15 ms | 227 → 201 ms (−12%) |
| Page switch, wall, revisit | ~−15 ms | 225 → 216 ms (−4%; within run-to-run noise) |
| Terminal resize, attributed total | 107 → ~90 ms | 107 → 91 ms |
| Terminal resize, wall | not specified | 219 → 91 ms median, but the resize samples are noisy (see below) |
xychart-beta
title "Layout layer (attributed ms per action), before vs after"
x-axis ["page first", "page revisit", "resize", "palette close"]
y-axis "ms" 0 --> 45
bar [41.6, 41.7, 37.9, 17.3]
bar [26.4, 26.7, 22.4, 10.1]
(First series = before, second = after.)
Notes
- The per-function results match the estimates closely; the wall-clock gain is smaller than the attributed one because the trace inflates in-shell time (calibration factor 0.39) and because medians of 3 rounds move by a few percent.
- Resize wall time needs more rounds before it can be trusted. Its three targets give 220 / 114 / 243 ms in the deep run and 89 / 167 / 91 ms here (one 270 ms outlier), and frames dropped from 3 to 2 per resize (bytes 45 → 37 KB). The attributed total moved only 107 → 91 ms. The −58% is therefore not a reliable figure; the attributed −15% is.
- After this change the layout layer is still the second largest of a page switch (26 ms;
_tui._inset11.3 ms,_tui._pane_too_small2.5 ms,_tui._content_rect1.9 ms,_tui._layout_r4 ms)._tui._widget_pos(55 ms self in the whole run, called per widget) and_tui._draw_widget_buf(61 ms) are the largest single functions left in the draw path. - The unit tests (
tests/unit/) and golden frames were not run by me; run them before relying on this.
Verdict
Matches the expectation: the layout layer dropped 37–42% across interactions, from one O(panes × widgets) scan that was executed twice. Layout is no longer the dominant layer of a page switch.
12. Change 10: shared pane inset in _widget_pos
What changed
The plan was a per-layout-generation cache of widget geometry. It was dropped after reading the code: _tui._widget_pos runs about once per widget per render (a cache would rarely hit there), it reads some 25 widget and pane properties written at about 100 sites, and the page-cache restore replaces those arrays without going through any setter. A stale entry would mean wrong positions and mis-clicks, for a few milliseconds on hover. Instead, _tui._widget_pos (lib/tui.sh) reuses the pane inset across consecutive widgets of one pane while _TUI_WP_REUSE=1, which only tui.render’s widget pass and _tui._hit_test set, and always reset afterwards.
Verified against the original functions on 10 demo pages: 292 widget positions and 6,900 hit-test points, 0 differences.
Expected vs real
Baseline: nav+palette+resize run 20260930-221040, and deep run 20260930-214915 for hover and click. After: nav+input run 20260930-222311 (6 rounds, traced, calibration error 4.9%). Attributed ms per action.
| expected | real | |
|---|---|---|
_widget_pos inclusive, page switch |
17.7 → ~9 ms | 17.7 → 15.6 ms |
_inset inclusive, page switch |
11.3 → ~4 ms | 11.3 → 8.1 ms |
| Layout layer, page switch | ~−8 ms | 26.7 → 24.0 ms (revisit), 26.4 → 24.2 ms (first visit) |
| Hover, attributed | −1 to −2 ms | 9.7 → 8.3 ms; wall 9.56 → 8.16 ms (−15%) |
| Click, attributed | −1 to −2 ms | 45.5 → 41.3 ms (layout 15.2 → 7.5 ms, mostly change 9); wall 11.1 → 10.2 ms |
| Page switch, wall | about −3 ms | first visit 201 → 245 ms, revisit 216 → 213 ms; first visit is within its normal run-to-run swing (median 201–245 ms across runs of 3 to 6 rounds) |
xychart-beta
title "Change 10: hover, click and page-switch layout (ms), before vs after"
x-axis ["hover", "click", "layout, page switch"]
y-axis "ms" 0 --> 30
bar [9.56, 11.12, 26.7]
bar [8.16, 10.19, 24.0]
(First series = before, second = after.)
Notes
- The gain is smaller than estimated for the page switch because
W_ORDERis not grouped by pane in most pages, so consecutive widgets often belong to different panes and the reuse does not apply. - The first-visit wall median moved the wrong way (+22%), but the same run shows attributed time unchanged (187 → 192 ms); first-visit medians of pages swing this much between runs (see the stability notes in the last section).
Verdict
Correct and safe, but small. It does not change the picture: the remaining page-switch cost is in page loading and drawing, not in geometry lookup.
13. Change 11: no render inside on_visit, fork-free docs titles
What changed
- Framework (
lib/tui.sh,lib/markup/tui_markup.sh,lib/state.sh):tui.gotorenders once after loading a page, buton_visitcode often ends with its owntui.render, which drew the whole page a second time.tui.gotonow sets_TUI_DEFER_RENDER=1aroundtui.load_cached, andtui.renderreturns immediately while it is set. A goto made from inside anon_visitrestores the outer value, so only the outermost goto renders. - Demo docs page (
share/demo/docu_callbacks.sh):_docu_short_titleand_docu_banner_titleforkedbasenameandhead -1per file (about 3 forks per file for 17 files, plus the caller’s$(…)). They use parameter expansion andread; a_docu_short_title_vvariant sets a variable so the caller needs no substitution.
Found in the per-page timeline: the components page rendered twice per visit (render column 199 ms for a 103 KB frame), because _sc_controls ends with tui.render.
Checked: all 17 docs files give identical short and banner titles to the originals; in a warm-start session the components inner render returns in 0 ms, the final render still draws, and the controls, status label, tab titles and page content are on screen after navigating. The unit tests and golden frames were not run by me.
Expected vs real
Baseline: nav run 20260930-222311. After: nav run 20260930-223251 (8 rounds, traced). Median done per page, ms.
| page (revisit) | before | after | on_visit before → after |
render before → after |
|---|---|---|---|---|
| components | 364 | 276 | 150 → 61 | 166 → 82 (KB 103 → 58) |
| docs | 384 | 287 | 183 → 85 | 77 → 77 |
| scrolling | 499 | 433 | 24 → 23 | 53 → 52 |
| monitor | 312 | 293 | 97 → 96 | 64 → 64 |
| widgets, settings, terminal, layout, home | 157–221 | 160–216 | unchanged | unchanged |
xychart-beta
title "Page switch revisit, median ms per page: before vs after change 11"
x-axis ["scroll", "monitor", "docs", "compon.", "settings", "case st.", "widgets", "terminal", "layout", "home"]
y-axis "ms" 0 --> 520
bar [499, 312, 384, 364, 221, 154, 210, 193, 157, 166]
bar [433, 293, 287, 276, 216, 212, 208, 197, 164, 160]
(First series = before, second = after.)
| expected | real | |
|---|---|---|
components page switch |
446 → ~340 ms | 364 → 276 ms revisit, 344 → 259 ms first visit (−24%) |
docs page switch |
444 → ~320 ms | 384 → 287 ms revisit, 363 → 285 ms first visit (−25%) |
| Average page switch (mean over all pages) | about −20 ms | 261 → 240 ms (−8%) |
| Page switch median | small | first visit 245 → 208 ms; revisit 213 → 216 ms (unchanged: the median sits on the light pages) |
Bytes written on components |
−35 KB | 103 → 58 KB |
Notes
- The page-switch median did not move because the gain is on the two heaviest pages, which are above the median; the mean (which includes them) fell 8%. Use the mean when judging changes to page code, and the per-page table above.
case_studyrevisit went 154 → 212 ms while its first visit stayed at 149 → 148 ms; nothing in this change touches it, so it is treated as an outlier and not investigated.- Still heavy:
scrolling(433 ms),monitor(293 ms, system stats collection, inherent) andcomponents(276 ms). Thedocspage spends about 89 ms attributed in the framework’stui.tabs.build, not yet looked at.
Verdict
Removed about 80–100 ms from the two heaviest pages by removing a redundant render and a few dozen forks. No effect on the light pages, as expected.
14. Change 12: fork-free page build, process-tree kill, _tui_cache_class
What changed
lib/markup/tui_build.sh: the page-build tag handlers called_tui_build.attrinside$(…)about 90 times per page (one fork each). 83 assignments now use the existing fork-free_tui_build.attrv;pane_ofandrow_of(two forks per call) gotpane_ofvandrow_ofv;auto_idusesprintf -v; the five inline argument sites use temporaries. The last deep trace had about 6,900 forks in_tui_build.tag.button, 1,386 inbindand 1,274 inlabel, all in the cold start’s cache build.lib/markup/tui_cache.sh:_tui_cache_classrecorded each class with$(printf …)(one fork per class, 21 of the 37 ms_tui_cache_classcost on the docs page); it now concatenates the string.lib/tui.sh,_kill_process_tree: leaving the Terminal page killed the running shell’s process tree with apgrepand asleep 0.05per level;pgrepalone costs about 25 ms because it scans all of/proc. The tree is now read from/proc/PID/task/PID/children(fork-free,pgrepas fallback), all processes get SIGTERM at once, and only those still running after up to 50 ms (polled every 5 ms, zombies counted as dead by_tui_proc_running) get SIGKILL. The cost of this hid inside whichever page was visited next, which madescrollingthe slowest page.- Not a cost:
tui.tick.addshowed 12 ms of self time in the trace but costs 17 µs per call when timed directly (one listener); it is a tracing artifact.
Verified: all 22 demo and default pages build to identical state (every _TUI_*, _TA_*, _WX*, _TX* variable, compared in fresh processes against the old tui_build.sh from git) apart from timestamps, CPU counters, PIDs and temp directory names. The kill function was checked standalone: a 4-level tree 302 → 6 ms, a single process 75 → 5 ms, a SIGTERM-ignoring process 75 → 56 ms, no stray processes. The unit tests and golden frames were not run by me.
Expected vs real
Baseline: startup run 20260930-215938 (cold, warm), nav run 20260930-223251, deep run 20260930-214915 (cold trace). After: startup+nav run 20260930-225600 (8 rounds, cold traced, calibration error 6.3%).
| expected | real | |
|---|---|---|
| Cold start, ready | 5.54 s, clearly lower | 5536 → 3113 ms (−44%; samples 3062–3356) |
| Cache build (splash) | 4.18 s, much lower | 4176 → 1758 ms (−58%) |
| Cold start, attributed (traced) | lower | 3186 → 1908 ms; markup 1951 → 1157 ms, subprocesses 498 → 230 ms |
| Warm start | unchanged | 308 → 303 ms |
scrolling page switch (first / revisit) |
about −150 ms | 426 → 261 ms, 433 → 263 ms (−39%) |
docs on_visit |
about −20 ms | 83 → 61 ms (first visit), 85 → 62 ms (revisit) |
| Mean page switch (all pages) | lower | first visit 235 → 219 ms, revisit 245 → 220 ms |
| Page switch median | small | 208 → 208 ms, 216 → 207 ms |
xychart-beta
title "Cold start phases (ms), before vs after change 12"
x-axis ["validate pages", "build caches", "first page+frame", "boot+lib+term"]
y-axis "ms" 0 --> 4500
bar [1084, 4176, 135, 89]
bar [1086, 1758, 127, 91]
(First series = before, second = after.)
xychart-beta
title "Page switch, first visit (ms), before vs after change 12"
x-axis ["monitor", "componen.", "case st.", "scrolling", "docs", "widgets", "settings", "terminal", "layout", "home"]
y-axis "ms" 0 --> 450
bar [283, 259, 148, 426, 285, 211, 205, 200, 162, 164]
bar [281, 275, 265, 261, 259, 210, 202, 198, 161, 150]
(First series = before, second = after.)
Notes
- Unexplained per-page shifts, probably external load:
case_studyfirst visit is bimodal in this run: 147 ms in 3 of 8 rounds and about 266 ms in the other 5, where it was 148 ms in every round before.widgetsfirst visit shows +70 ms in 2 rounds.monitorrevisit went 293 → 360 ms (first visit unchanged at 281 ms).- A separate session visiting the same pages in order four times gave
case_study147–150 ms each time, so it does not reproduce in isolation. Nothing in this change touches those pages, andmonitorcollects live system statistics. - Do not read the
case_study,widgetsormonitorper-page changes as regressions until a repeat run confirms them.
- The scrolling gain is the terminal cleanup: the Terminal page is visited right before
scrollingin the scenario, so its process-tree kill used to be charged toscrolling. A shell ignores SIGTERM when interactive, so a grace period of up to 50 ms remains on leaving the Terminal page. - Cold-start forks that remain (traced run):
tui.cache.deps_of268 forks (dirname116×,basename58×),_tui_cache_define_goto252 (a$(printf …)per nav button),_markup_wx168,tui.cache.warm_with_spinner103,tui.cache.record88,_dbg_layout87, and theawkin_tui._calc_bounds(28 runs, 54 ms). Page validation takes 1.09 s, unchanged. - Warm start did not change, as expected: it does no page build.
Verdict
The cold start, previously the largest startup cost, dropped by 44% from removing forks in the page build; the warm start is unaffected. The Terminal-page cleanup bug was found by reading the timeline of the slowest page.
15. Change 13: remaining cold-start forks in the cache recording
What changed
Files: lib/markup/tui_cache.sh, lib/markup/tui_markup.sh, lib/widgets/tui_widgets.sh.
tui.cache.deps_ofno longer forksdirname,basenameorcd … && pwd(268 forks); it uses a new fork-free_tui_cache_dirbuilt on_tui_path_canon._tui_cache_define_gotorecords nav buttons withprintf -vinstead of$(printf …)(252 forks)._markup_wx(the widget tag handler) reads its attributes with a new fork-free_markup_attrv(168 forks, 37 call sites).warm_with_spinnertakes the clock from${EPOCHREALTIME}instead of a$(…)per poll and stops forkingdirnameper page;tui.cache.recordjoins its arrays without$(printf …);tui.cache.signaturesorts with a bash insertion sort instead ofprintf | sort;start_cachedresolves its directory with_tui_cache_dir(3 forks on every warm start).
Verified: 60 attribute lookups give identical values to _markup_attr (including entity-encoded values); _tui_cache_dir matches the old cd … && pwd on 8 path shapes; all 22 pages build and record to identical cache state (page snapshot, scripts, gotos, classes, theme, signature) against the old files from git, apart from timestamps, CPU counters, PIDs and temp directory names. The unit tests and golden frames were not run by me.
Expected vs real
Baseline: startup run 20260930-225600 (5 rounds, cold traced). After: startup run 20260930-230441 (5 rounds, cold traced).
| expected | real | |
|---|---|---|
| Forks in a cold start (traced) | about 800 fewer | 1,658 → 786 (−872) |
| Cold start, ready | 3.11 s, several hundred ms lower | 3113 → 2867 ms (−8%; samples 2844–2872) |
| Cache build (splash) | 1.76 s, lower | 1758 → 1526 ms (−13%) |
| Cold start, attributed | lower | 1908 → 1756 ms; subprocesses 230 → 154 ms, markup 1157 → 1061 ms |
| Warm start | −3 forks | 303 → 287 ms; load first page 88 → 75 ms |
xychart-beta
title "Cold start: forks and build time, before vs after change 13"
x-axis ["forks (x10)", "build caches (ms/10)", "cold ready (ms/10)"]
y-axis "scaled" 0 --> 340
bar [166, 176, 311]
bar [79, 153, 287]
(First series = before, second = after; values are scaled by 10 so three different units fit one axis.)
Notes
- Removing 872 forks saved only about 230 ms of the cache build, about 0.26 ms per fork, well under the 1 ms I assumed: most of the forks were cheap
dirnameandprintfcalls. The remaining cold start is not fork-bound any more. - What is left of the 2.87 s cold start: page validation 1.09 s (unchanged:
_tui_validate.attrs155,_tui_validate.tables126 and_tui_validate.walk101 ms attributed in the trace), the cache build 1.53 s (_tui_parse.scanalone 376 ms self,_tui.theme_parse84 ms,_tui_build.attrv59 ms), plus 0.13 s of start-up and first page. All of it is pure bash CPU, no longer processes. - Remaining forks (786):
_dbg_layout87 (debug demo page),_tui._calc_bounds50 (theawk),_tui_home.meta_save48,tui.plugin.add48,term.size47, the snapshotgreppipeline 44. - Warm start gained 16 ms, mostly in
load first page.
Verdict
Correct and low-risk, but smaller than expected: the cold start is now limited by parsing and validating markup in bash, not by process creation.
15b. Change 14: content-addressed fragment cache
What changed
lib/render/tui_rowcache.sh caches the bytes a pane or widget appends to the frame. The key is every input that produces those bytes (geometry, focus and hover state, resolved text, alignment, a style epoch bumped by every tui.style call and page style reset), so a changed input is a different key and nothing is registered or invalidated per widget. It is used by _tui._draw_pane_buf (bordered panes) and _tui._draw_widget_buf (label, button, checkbox). Texts containing ${expr} bypass it. It holds at most 4096 fragments, and TUI_ROWCACHE=0 turns it off. Counters: rowcache_hit, rowcache_miss.
Expected vs real
Bench tools/bench/page_switch.sh 40 (150x45 demo), cache off vs on, then the deep runs 20260930-231054 (before) and 20261001-183844 (after), 5 rounds each, medians.
| expected | real | |
|---|---|---|
| Render span mean, 40-switch bench | −25% | 50.6 → 36.7 ms (−27%), 80% hit rate (1354 hits, 348 misses) |
| Warm page switch in the bench (home / components / widgets) | −30% | 39 / 83 / 46 → 24 / 46 / 32 ms |
nav.revisit |
−15% | 196 → 168 ms (−14.7%) |
nav.key |
−15% | 229 → 194 ms (−15.3%) |
nav.first |
unchanged | 190 → 199 ms (+4.8%); tui.render 55.6 → 64.5 ms |
palette.close |
−30% | 51.6 → 36.3 ms (−29.6%); tui.render 41.8 → 26.4 ms, _tui._draw_pane_buf 9.0 → 2.4 ms |
resize |
−20% | 187 → 142 ms (−24.1%) |
theme.again |
−20% | 20.1 → 14.1 ms (−30.0%) |
theme.first |
small | 270 → 240 ms (−11.3%) |
| Hover, scroll, idle, startup, shutdown | unchanged | within ±1.5% |
| Bytes written per action | unchanged | unchanged for page switch, palette close, scroll |
Notes
- A first visit pays for building keys and storing fragments with no hit to show for it:
tui.render+9 ms onnav.first. Only revisits, resizes and re-renders of an unchanged page reuse fragments. - Output bytes did not change, as designed: this change skips composition, not emission. Palette close still writes 36.7 KB against the 10 KB target in
cheap-redraws-concept.md. - The drops in bytes for
click(2258 → 318),focus.nextandfocus.prev(about 2.4 KB → 0.23 KB) appear in the same run pair but are not caused by this change, since it does not alter what is emitted. They come from other changes between the two runs and were not investigated here. - Page switch is no longer dominated by rendering: in
nav.first,tui.gototakes 133 ms of whichtui.load_cached/tui.cache.replaytake about 55 ms andon_visitabout 26 ms, whiletui.renderis 64 ms. - Tests:
tests/unit/rowcache.t.sh(8 tests).tools/t_golden.sh --checkis byte-identical with the cache on and off.
Verdict
Works for what it targets: warm and repeated redraws of unchanged panes and widgets (revisit, palette close, resize, theme re-apply). It does not touch emission, so palette close output is unchanged, and it adds a small cost to first visits. Next: localised updates for overlay close (damage rect, painted-key table), which targets the unchanged 36.7 KB.
15c. Change 15: overlay dismissal replays the saved frame
What changed
tui.render keeps its frame (_TUI_BASE_FRAME). tui.modal.dismiss (esc, click outside the palette) flushes that frame instead of re-composing the page. Paints that happen while the overlay is open (clock ticks, hover, focus) are folded into the saved frame, so it stays an exact replay of the screen; overlay draws are not. An erase, restyle, resize or an oversized frame (192 KB) drops it and the next dismissal takes the old full path. Closes that run a command (enter) are unchanged. Switch: TUI_DISMISS_REPLAY=0.
Expected vs real
Runs 20261001-192440 (before the fold fix: every dismissal fell back, because the header clock repainted under the palette) and 20261001-193110 (after), palette scenario, 5 rounds, settle medians.
| before this change | fallback only | real | |
|---|---|---|---|
palette.close |
37.9 ms | 37.9 ms | 13.9 ms (−63%) |
tui.render inside the close |
26.9 ms | 26.9 ms | not called |
| bytes written on close | 36.7 KB | 36.7 KB | 36.7 KB |
palette.open, palette.type |
28 / 24 ms | 26.5 / 23.2 ms (unchanged) |
Notes
- The first version invalidated the saved frame on any foreign paint and never took effect in the profiler. The demo header clock repaints every second, so any palette session longer than a tick fell back.
- Bytes are unchanged on purpose: the whole frame is replayed (rows cannot be replayed alone, since
_tui._fill_pane_bgsets its style once for many rows). The 10 KB byte target ofcheap-redraws-concept.mdis not met; the 30 ms time target is. - What is left in the palette:
palette.open26.5 ms andpalette.type23 ms are almost all inside the key handler (_tui_input.key_event), while the overlay draw is 1.5 ms. Not investigated.
Verdict
Works for dismissals by esc and click outside. Covered by tests/unit/modal_dismiss.t.sh.
15d. Change 16: geometry and line-slice memos, and where the render time actually is
What changed
_tui._widget_posmemoises its result on every input it reads (pane rect and inset, the widget’s placement and size attributes, both valigns), so a changed attribute is a different key (_TUI_WPC, 4096 entries)._tui._vslice(the pure-bash line slicer for coloured or scrolled output panes) memoises on (line, offset, width) (_TUI_VS_MEMO, 4096 entries). A scrolled-back or re-rendered line costs one lookup.- Tried and removed: a per-pass memo of
_tui._content_rect(render −0.4%, no effect).
Measured
Untraced, headless, 150x45, 100 iterations of a full tui.render with warm caches (scratch script, not committed; HEAD = commit 3e39d31):
| page | HEAD | with Change 14 only | now |
|---|---|---|---|
| components | 54.4 ms | 30.8 ms | 29.9 ms |
| scrolling | 34.0 ms | not measured | 19.6 ms |
| home | not measured | 16.9 ms | 15.9 ms |
| output panes only, components / scrolling | 3.8 / 8.2 ms | 3.0 / 6.4 ms |
Bench page_switch.sh 40 render mean: 20.5 → 20.0 ms.
Where the time is
- Per label or button, a cache hit still costs about 300 µs: one associative-array lookup costs roughly 2-3 µs in this bash, and a hit needs about 100 of them (type, pane, geometry attributes, focus and hover compares, the cache key). Geometry memoisation removed only about 30 µs of that.
- Phases of one render of
home(15.9 ms): content-fit pre-pass 1.2, panes 2.2, widgets 10.6, outputs 0. - In the profiled page switch (
nav.revisit, 167 ms) render is 37 ms;tui.cache.replayis 54 ms (includingon_visit27 ms) and the rest is probe, click and pty overhead, which the headless bench does not have.
Verdict
Small gains, kept (the memos are exact and tested). A further large cut needs fewer lookups per widget, not faster ones: per-node display lists with dirty flags (plan 2B/4.2), indexed instead of associative arrays on the hot path, or persistent shells (3D). tui.cache.replay is the largest single item left in a page switch and has not been traced.
15e. Change 17: demo page callbacks without forks, one navbar width, SIGHUP on page exit
What changed
- New public
tui.capture VAR CMD ARGS...(VAR=$(CMD ...)without the subshell). The demo callbacks use it for every renderer, getter and chart call;date,nproc,cat,awk,sedanddf | tail | awkcalls on visit, tick and input paths are gone (monitor, docs, settings, widgets, keys, debug, debug lab, case study, components). show_all(components) and the case-study metrics tab are memoised per renderer width._tui._calc_boundsmeasures coloured output lines in bash (_tui._vwidth_v, memoised per line) instead of forking awk.- The menu column is 18% wide on every page (it was 18, 20 or 22).
_kill_process_treesends SIGHUP to what survives the first 5 ms poll: an interactive shell ignores SIGTERM, so leaving the Terminal page waited the full 50 ms and then killed it (measured 55 ms, now 11 ms).
Measured
Deep runs 20261001-195825 (before) and 20261001-201315 (after the demo changes, before the SIGHUP change), 5 rounds, settle medians:
| before | after | |
|---|---|---|
nav.key (alt+2 components, alt+7) |
201 ms | 106 ms (-47%) |
nav.first |
204 ms | 184 ms (-9%) |
nav.revisit |
168 ms | 156 ms (-7%) |
theme.first |
242 ms | 215 ms (-11%) |
| components revisit / first | 215 / 269 ms | 157 / 227 ms |
| settings revisit / first | 194 / 278 ms | 146 / 176 ms |
| monitor revisit / first | 228 / 270 ms | 186 / 238 ms |
| widgets revisit | 166 ms | 147 ms |
| docs first / revisit | 338 / 222 ms | 253 / 214 ms |
| home, layout, case study, terminal (revisit) | 119-158 ms | 109-153 ms |
| hover, focus, click, scroll, palette, resize, startup | unchanged (within 3%) |
Notes
- A trivial page costs about 120 ms to revisit:
tui.cache.replay23 ms,tui.render28 ms, the rest probe, click and settle overhead (settle minus busy is about 33 ms). Page code adds on top: monitor 57 ms ofon_visit, docs 62 ms, widgets 26 ms, scrolling 23 ms. - The scrolling page’s 211 ms contained about 50 ms of the Terminal page’s shell being killed (leaving the Terminal page); the SIGHUP change targets exactly that.
- Confirmed by run
20261001-201852(standard, 3 rounds,--scenario nav, after the SIGHUP change): scrolling revisit 211 → 163 ms and first visit 248 → 199 ms;nav.revisit156 → 146 ms andnav.first184 → 179 ms;nav.keyunchanged at 106 ms. Other pages moved by 1 to 4 ms. - What is left in page code is computation, not forks: the monitor’s chart builders (about 70% of its
on_visit), the docs page rebuilding its tabs on every visit (tui.class,tui.grid,load_document).
15f. Change 18: snapshot rewritten once, no stty per switch
Found with the new --scenario floor trace (run 20261001-211251, 3 rounds, per switch between trivial pages: tui.goto 82 ms = tui.render 56 + tui.load_cached 22 + tui.reset_ui 4).
_tui_cache_restorerewrote all 135 to 300declarelines of the snapshot in a bash loop on every replay. Untraced: restore 9.5 ms, of which theevalis 2.5 ms. The rewrite now happens once at record time. Headless loop,reset_ui+load_cachedover home, layout and case study: 34 → 15 ms per switch. Old on-disk snapshots still restore.tui.reset_uiforkedstty size(2.3 ms in the trace) on every switch; a running app keeps the size from its resize handler.- Found, not yet fixed: the fragment cache key contains
_TUI_RC_EPOCH, which every page switch bumps, so fragments never hit across pages (57_tui_rowcache.storecalls and about 5.7 ms per switch, with few hits). The key should hold the style strings instead.
15g. Change 19: fragment keys without the epoch
The floor trace (20261001-212745) still showed 57 _tui_rowcache.store calls per switch: _TUI_RC_EPOCH is bumped by every page switch and was part of every key, so no fragment was ever reused across pages. Keys now hold the resolved style strings (and no page or pane id); the menu and header panes and buttons, identical on every page since the menu width was aligned, hit on every page. Headless (24x80 fallback, so a small frame) the switch loop went 24.3 → 22.4 ms, with the cache now faster than no cache (24.4 ms) instead of equal to it; frames are byte-identical in all three modes. Confirmed in the real app by run 20261001-222623 (full, 2 rounds; floor scenario, probe spans per switch): nav.floor settle 125.2 → 93.1 ms (-26%), tui.render 58.2 → 28.3 ms, _tui._draw_pane_buf 14.6 → 3.8 ms, tui.goto 75.1 → 45.4 ms. Page switches: first visit 180 → 159 ms, revisit 152 → 135 ms (against the deep run 201315: -14% each); revisit per page home 120 → 99, layout 119 → 104, case study 109 → 95, components 157 → 136. The estimate was 10 to 15 ms; the render halved because a hit also skips composing and storing. Everything else is unchanged within noise (theme.first 240 ms is inside its 215 to 242 ms band over earlier runs); the docs revisit (233 ms against 212 to 222) did not improve and has only 2 rounds behind it.
15h. Change 20: footer, relayout and widget-visit overhead
From the floor trace 20261001-222939 (per switch, calibrated): footer overlay 4.1 ms (reverse_keys 3.4), relayout 5.2 ms, widget visits 13.7 ms with every fragment hitting.
reverse_keysmemoises its default tier by content: 1269 -> 384 us per call; a footer rebuild per switch 1.9 -> 0.9 ms.- The footer writes its row only when it changes;
draw_allskips the write and the sync frame when nothing was drawn. - The snapshot records the size it was laid out for; a replay at the same size and footer with no layout-input change by the scripts skips the relayout (counter
relayout_skipped). - The widget clip reuses the inset
_widget_posset instead of a second_tui._inset.
Headless switch loop (reset + load_cached + render, home and case study): 30.3 ms at v0.0.21 plus the epoch fix, 21.4 ms now (this includes the snapshot and cache-key changes before it). Frames are byte-identical (goldens, cache on and off); 7 new tests. Confirmed by floor run 20261001-224357 (3 rounds, probe spans per switch): settle 93.9 -> 86.3 ms, tui.goto 45.4 -> 37.8, tui.render 28.5 -> 24.4, tui.cache.replay 14.9 -> 11.3 (the relayout is gone from the trace), _tui_overlay.draw_all 3.8 -> 2.4. Over the whole day the trivial-page switch went 136.3 ms (211251) -> 86.3 ms (-37%). What is left per switch (calibrated): widget visits 10.9 ms (every fragment hits; the cost is the visit), tui.load_theme 3.0, snapshot restore 2.5, the content-fit pass about 3, output buffers 2.4, reverse_keys 1.1.
15i. Release check: deep run 20261001-230749
Full scenario, 5 rounds, idle machine, with the floor group (the earlier deep run 225547 overlapped with test runs on the same machine and was not used). Medians: page switch first visit 142.9 ms (budget 150: inside), revisit 125.1 ms (budget 100), switch by key over all ten pages 100.6 ms (budget 100), mean over all pages 137.0 ms, trivial-page floor 86.5 ms, palette close 13.7, theme first 229.6, warm start 289.7 ms. Per page, revisit: home 91, case study 86, layout 94, settings 121, widgets 124, components 125, terminal 129, scrolling 143, monitor 161, docs 188 ms. Against the v0.0.21 nav run 204153 (first 180.3, revisit 152.1, key 124.0): -21%, -18%, -19%. Noise to know: the resize median swings between 145 and 203 ms because its three targets swap between two timing modes (unchanged by this work); palette close shows two samples at 78 ms in two runs because a clock tick lands in the settle window (busy 3 ms in every sample).
15j. Change 21-22: held keys, and what stage 2 cost
New measurement first: --scenario held sends a held key at the real repeat rate (33 ms between presses, holds of 0.1 to 1 s). Per-press lag is the key sent to the first output the terminal receives; groups held.nav, held.list, held.table, held.type, held.cursor, held.tab, held.scroll, held.page, budget 50 ms, rated on the worst press.
- Change 21.
tui.focusknows its dirty set (old and new widget, plus the pane borders only when the keyboard pane changes) and draws it as one raw frame, without the row diff. - Change 22.
tui.action.focus_dirreads the widget rects from the hit index (_TUI_HZ_WR/WC/WW, filled by_tui_hit.rebuild) instead of one_tui._widget_posper focusable per press. Trace attribution, Scrolling page:focus_dir80 → 8 ms per hold action.
Real, same stage-2 build without and with the work (runs 20261002-222307 and 20261002-223527, deep, 5 rounds, 150x45, medians in ms):
| hold | app work per 1 s | bytes | per-press lag |
|---|---|---|---|
| nav bar | 478 → 47 | 49.5 → 4.3 KB | 8.5 → 4.5 |
| tab | 420 → 60 | 60.7 → 15.9 KB | 33.2 → 21.2 |
| list | – | – | 4.5 → 4.4 |
| table | – | – | 7.3 → 6.6 |
| page keys | – | – | 4.8 → 4.6 |
| cursor | – | – | 2.7 → 2.6 |
| typing | – | – | 2.5 → 4.3 (noise at this scale) |
Verdict: nav bar and tab holds improved by a factor of 10 (work) and 1.6 to 2 (lag); the other widgets are unchanged within noise. All held groups are inside the 50 ms budget except held.scroll (109 ms): it lands on a textarea, and the lag measure pairs presses that paint nothing with the next unrelated frame. That is a measurement artefact, not a slow press.
Bug found on the way: tui.set stores a value without redrawing, so a visit callback that used it on a visible checkbox or input showed stale state until hover. The demos use tui.update there.
Not improved, or worse: stage 2 against v0.0.22
Deep run 20261001-230749 (v0.0.22) against 20261002-223527. The machine was not guaranteed idle in the newer run (the demo was being edited and tested), so treat single-digit percentages as soft. Medians, ms:
| metric | v0.0.22 | now | change |
|---|---|---|---|
| Page switch, first visit (budget 150) | 142.9 | 150.9 | +6% |
| Page switch, revisit (budget 100) | 125.1 | 141.3 | +13% |
| Page switch by key | 100.6 | 107.8 | +7% |
| Trivial-page floor | 86.5 | 97.3 | +12% |
| Warm start | 289.7 | 346.6 | +20% |
| Cold start | 2825 | 3382 | +20% |
| Terminal resize | 202.8 | 144.8 | −29% |
| Wheel scroll | 18.0 | 15.7 | −13% |
| Theme switch, first use | 229.6 | 253.2 | +10% |
| Idle repaints | 2.0 /s | 2.0 /s | unchanged |
Page switches and start-up cost 6 to 20% more than at v0.0.22. The cause is the stage-2 pipeline, not the focus work: addon files are part of the cache signature and composition runs on every build (floor about 27 ms, see pages-templates-addons-refresh.md). Winning this back is the next work.
Open points:
- Palette close reads 47 ms in both A/B runs against 13 ms at v0.0.22; a clock tick inside the profiler’s settle window inflates samples. Unresolved.
held.scroll109 ms is the measurement artefact above; the lag metric needs to skip presses that paint nothing.- The
addonsandgeneratedprofiler scenarios still target the removed demo pages (now the Compose page) and measure nothing until rewritten.
15k. Change 23: pages are validated in parallel
What changed
tui.validate.files (lib/markup/tui_validate.sh) hands two or more pages to _tui_validate.parallel: one background worker per core (TUI_VALIDATE_JOBS, 1 = in the calling shell), each taking every N-th page and writing the findings and the files it read to one result file. The parent merges the files in page order, so the messages, counts and the cache/validated stamp equal a serial run; a page whose worker left no result is validated in the parent. Pages are independent (ids are per page), so no state is shared.
Measured
Micro-measurement: tui.validate.files over the 27 pages of share/demo and share/defaults in a sandboxed TUI_HOME, same 28 errors and 28 messages at every setting (the demo pages are templates and fail the same way serially).
| workers | time |
|---|---|
| 1 | 1550 ms |
| 4 | 518 ms |
| 12 | 248 ms |
Startup run 20261106-231355 (--scenario startup --deep, 5 rounds, 150x45): cold start 2.84 s (first pixel 638 ms), of which validate pages 315 ms, build caches (splash) 2.15 s, save cache 20 ms; warm start 526 ms with validate pages 4.4 ms (the stamp skips the walk).
Caveats
- There is no clean before/after pair for the cold start. The run before it (
20261106-225537) ran while about 0.5 cores were busy with other work (cold start 4.27 s), so the-34%the report prints against it is not a measure of this change. - The 1.09 s of validation in section 17 is from
20260930, with other pages and an older validator;1.09 s → 315 mscompares across five weeks. The micro-measurement above is the same-code, same-pages comparison. - Validation only runs when
cache/validatedis missing or stale (first launch, after an update, after a page edit); a warm start never pays it.
Verdict
The validation phase is no longer the largest cold-start cost: the cache-building splash (2.15 s of 2.84 s) is. The next step for cold start is that phase (_tui_parse.scan, _tui_build.attrv, _tui.theme_parse) or a shipped cache, not more validation work.
15l. Change 24: lazy cache build
What changed
tui.start_cached (lib/markup/tui_cache.sh) with TUI_CACHE_LAZY=1 (default, lib/tui_configuration.sh) builds only the first page behind the splash. The other stale pages go to a pool driver forked before the terminal is taken over: TUI_CACHE_LAZY_WORKERS workers (0 = half the cores) at renice 10, each writing one snapshot. The main loop’s tick (_tui_cache_lazy.poll) decodes one finished snapshot per call, keeps a page the app built itself in the meantime, and persists the cache when the pool is done. A page opened before its snapshot exists takes the existing tui.load_cached miss path (an in-process build).
Two defects surfaced on the way, both only visible with more than one worker: every worker’s tui.init rewrote app.meta through the one temporary name app.meta.tmp (builds failed with mv: cannot stat) and counted as a run of the app. Each process now writes app.meta.tmp.$BASHPID and a headless worker skips the run record.
Measured
Sandbox harness, 24 demo pages (not the profiler): time until the first page is cached, and until every page is built and persisted.
| workers | first page ready | all pages persisted |
|---|---|---|
| 2 | 635 ms | 4.9 s |
| 6 | 649 ms | 2.45 s |
| 12 | 619 ms | 2.0 s |
For comparison, the eager build of the same 24 pages took 1.95 s before the first frame (12 workers) and 9.1 s with one.
Profiler, --scenario startup --rounds 2 --no-trace on this checkout (startup.cold / startup.warm):
| before | now | |
|---|---|---|
| Cold start, ready (start page up; this is what the budget counts) | 2.84 s | 1.79 s |
build caches (splash) |
2.15 s | 0.63 s |
| Cache complete (background; informational, not in the budget) | – | 3.62 s |
| Warm start | 526 ms | 541 ms |
The profiler marks both moments: ready (first frame) and the new cache_complete hook event, and a start waits for the build to finish before the next start, so the warm start finds a complete cache. A quit before the pool is done persists the pages built so far and the first page is persisted at once; before this, a launch that quit early left no cache and the next “warm” start rebuilt the landing page behind the splash (a 1.49 s warm start in one run).
What the new start-up probes show
With a probe on every phase (_tui_cache_warm_prelude, _tui_cache_lazy.start, _tui_store.restore_page, …) and the gaps named, a cold start of the demo (startup, 2 rounds, untraced, a noisy machine: 1.8 to 2.1 s ready) reads, in ms: library 108, validation 303, shared prep (themes, shells) 443 to 504, first page behind the splash 645, init 36, first page load 18, first frame 138, the rest under 50 each.
- The shared prep is the largest serial piece after the splash:
_tui_cache_warm_preludeparses every stale page’s theme and builds every shell for all 24 pages before the first page is built, though the first page needs only its own. The pool’s workers inherit that work through the fork, which is why it is done in the parent. Doing only the first page’s prep in the foreground and the rest in the pool driver is the next candidate (not done). - Page builds: 24 workers, the slowest about 1 s, 13.6 s of work in total over a 3.1 s wall time: the pool is bound by the total CPU, not by one page.
Prep for the first page only (TUI_CACHE_LAZY_PREP)
Measured first, because the guess was off: the prep for all 24 pages cost 376 ms and for the first page alone 291 ms in a harness, not the 443 ms the profiler shows for everything. The first page needs the one shell that 19 of the 24 demo pages share and its theme, so most of the prep cannot move. The part that could move was mostly repeated work: tui.load_theme costs about 2.9 ms per call even for a memoized file, and the prep called it once per page (24 calls) for two distinct files. Each theme file is now loaded once, and the later pages’ prep runs in the pool driver (the pages’ workers inherit it).
Profiler, --scenario startup --rounds 3 --no-trace, cold start ready (start page up):
TUI_CACHE_LAZY_PREP=0 |
=1 |
|
|---|---|---|
| runs | 1.76, 2.35, 1.79 s | 1.78, 1.67, 1.70 s |
| median | 1.79 s | 1.70 s |
About 90 ms, which matches the 80 ms of the harness, but is inside the spread of the =0 runs (one at 2.35 s). The remaining prep of the first page is about 380 ms (a shell build of about 230 ms and the theme parse of about 130 ms); it is serial, because the first page’s build needs both. A shell the driver builds for a later page is not handed back to the app: the app builds it on the first visit of such a page (all later pages of the demo share the first page’s shell, so none does).
Validation in the background (TUI_VALIDATE_ASYNC)
_tui_validate.begin starts the check at the point where the serial gate ran, in a background process (which uses the validation workers); _tui_validate.finish waits for it right after the first page is built and before the cache is persisted or the lazy pool starts, so an invalid page never reaches the disk cache and still stops the start (--ignore-invalid-xml as before). _tui_validate.gate is begin 0 plus finish for the other callers. The worker result files are shared with the parallel pool (_tui_validate.save and merge).
Profiler, --scenario startup --rounds 3 --no-trace, cold start ready (start page up):
TUI_VALIDATE_ASYNC=0 |
=1 |
|
|---|---|---|
| runs | 1.67, 1.70, 1.71 s | 1.47, 1.45, 1.45 s |
| median | 1.70 s | 1.45 s |
validate: wait for result |
316 ms | 6.9 ms |
shared prep |
373 ms | 451 ms |
The check costs nothing on the critical path any more (the wait is 7 ms), but it competes for the cores with the first page’s prep, which got about 80 ms slower, so the net is 250 ms, not the 316 ms of the old wait. A warm start does not start a check (the stamp holds).
Logging
A timestamped xtrace of the 200 ms shell build put tui.log at about 13 ms (6.7%): there was no log level, so every tui.log.debug formatted a timestamp and opened, appended to and closed the day’s log file. Stubbing tui.log out made 6 pages take 2.11 to 2.15 s instead of 2.19 to 2.25 s. The logger now lives in lib/tui_log.sh with a level (TUI_LOG_LEVEL, default info, tui.log.level at run time); a dropped line is one call and one comparison.
Controlled A/B, 6 demo pages built one after the other, 5 interleaved runs each:
| level | mean | log lines written |
|---|---|---|
debug (the old behaviour) |
2224 ms | 599 |
info (the new default) |
2141 ms | 0 |
About 83 ms (3.7%) of CPU, about 14 ms per page; on the first page’s critical path that is roughly 25 ms. A profiler cold-start run could not show it: the machine was busy and the spread inside one setting (1.46 to 1.90 s) was larger than the effect, so there is no wall-clock cold-start number for this change.
A first click against the background build (nav.form in tools/compare)
The DABT/Textual comparison read nav.form at 250 to 265 ms (19,449 bytes) in four runs after the lazy build, against 45 to 52 ms (15,095 bytes) before. It is the first page switch after start, and the harness clicks it while the pool still builds: a page whose snapshot is not there yet is built in the main shell (tui.load_cached miss), which costs about 200 ms and writes more. It depends on how busy the machine is: compare.py --only dabt --rounds 3 gave 44.8 ms idle, and 360.6 ms with 12 busy loops running; with TUI_CACHE_LAZY=0 41.4 ms idle and 55.7 ms under the same load. The code of the slow and the fast runs was identical.
Two changes: the pool builds the pages the first page links to first, in link order (compare app: form data layout instead of data form layout), and the harness’s cold session waits for the “Page cache ready” notice before it ends, so the steps run on a complete cache (they ran in the second start, which began with a half-built cache). Under the same 12 busy loops nav.form is 67.8 ms with 13,258 bytes (a cached switch).
A user who clicks a page before its worker is done still pays the on-the-spot build (a first launch only). Waiting for a worker that is already building that page, instead of building it again, would remove it; not done.
Not measured yet
- A
--deepstartup run for this change: the table above is a 2-round, untraced run; the 1.79 s also contains validation (about 0.3 s),tui.initand the first frame. - The cost to the UI while the pool runs (input lag during the first seconds);
reniceis the only mitigation.
15m. Start-up, before and after: deep run 20261107-012455 against 20261105-234102
Command: tools/profiler/profile.sh --scenario startup --deep --compare tools/profiler/reports/20261105-234102. Now: 5 rounds, deep, 150x45, tree dirty (changes 23 and 24 and what follows are not committed), idle machine (no busy loops, load average 0.6). Before: the last run taken before the start-up work, the full scenario, 2 rounds, 81bac6b. Medians.
| before | now | change | |
|---|---|---|---|
| Cold start, ready (start page up; what the budget counts) | 4297 ms | 1401 ms | -67% |
| Cold start, first pixel | 2101 ms | 381 ms | -82% |
| Warm start, ready | 517 ms | 521 ms | +1% |
| Cache complete (background; informational, not in the budget) | (built before the first frame) | 3185 ms |
Cold start phases (ms):
| phase | before | now |
|---|---|---|
| bash + script boot, load library | 111 | 112 |
| validate pages (serial, before everything) | 1780 | 3.3 start + 6.6 wait |
| shared prep (themes, shells), first page only | inside the splash | 431 (inside the splash span) |
| build caches (splash): all 24 pages / the first page | 2151 | 990 |
| save cache | 19 | 3.5 |
| start background build | – | 4.3 |
| init terminal, load first page | 44 | 47 |
| first frame | 115 | 144 |
| not covered by a probe | 81 |
The background build: 24 workers, the slowest 844 ms, 12.2 s of work in total, 1.9 s of wall time for the builds themselves; the whole pool is done 3.2 s after spawn, 1.8 s after the app is usable.
How to read it:
- The baseline is not clean. The report warns that it ran with about 0.7 cores busy, so its 4297 ms (and the 1780 ms of validation in it: a clean serial run measured 1.09 s on 2026-09-30) are inflated. The nearest clean figure for the same code with only the parallel validation in front of it is 2836 ms (run
20261106-231355, 5 rounds). The honest saving of the lazy build and what came after it is therefore 2836 to 1401 ms, -51%; the -67% includes load noise in the baseline. - Where the saving came from, each measured on its own (details in 15k and 15l):
| change | measured effect on the cold start |
|---|---|
parallel validation (TUI_VALIDATE_JOBS) |
27 pages: 1550 ms to 248 ms |
lazy build, first page behind the splash (TUI_CACHE_LAZY) |
2.84 to 1.79 s |
prep of the first page only, each theme loaded once (TUI_CACHE_LAZY_PREP) |
1.79 to 1.70 s (median of 3; inside the spread of the old runs) |
validation in the background (TUI_VALIDATE_ASYNC) |
1.70 to 1.45 s (3 rounds each) |
log level info (TUI_LOG_LEVEL) |
about 25 ms of CPU on the first page’s path, 83 ms per 6 pages; no wall-clock figure |
| build order by links; harness waits for the cache | not a start-up number: nav.form of the comparison under load, 361 to 68 ms |
- What is left on the cold path (1401 ms): the library load (108 ms), the first page’s prep and build behind the splash (990 ms: shell build about 200 ms, themes about 90 ms, the page’s own build), the first frame (144 ms), init and first-page load (47 ms), and 81 ms no probe covers. The first page’s prep is serial because its build needs both.
- Warm start did not move (517 to 521 ms): library 108, load cache 91, first page 75, first frame 115. The cache load (all pages at once, 11 files each) is the next candidate there; it was not touched.
- Background cost: the first seconds of a first launch run 6 workers at nice 10; a click on a page whose worker is not done yet builds that page in the foreground (about 200 ms). The profiler does not measure input lag during the build.
16. Standing against the budgets
Budgets are the perceived-instant lines in tools/profiler/scenarios/demo.py. Baseline is the first deep run (20260930-193435); current is the latest deep run (20260930-214915). Theme switching and alt+2 are compared with their first measured values where they exist (the baseline alt+2 was a no-op).
| interaction | budget | baseline | current | state |
|---|---|---|---|---|
| Cold start | 5000 ms | 5836 ms | 2867 ms (run 230441) |
within |
| Warm start | 1000 ms | 857 ms | 287 ms (run 230441) |
within |
| Page switch, first visit | 150 ms | 356 ms | 208 ms (run 225600) |
over (1.4×) |
| Page switch, revisit | 100 ms | 370 ms | 207 ms (run 225600) |
over (2.1×) |
| Page switch by key (alt+2) | 100 ms | 10.9 ms | 368 ms | over (3.7×) |
| Theme switch, first use | 250 ms | – | 306 ms | over (1.2×) |
| Theme switch, repeat | 150 ms | – | 16.1 ms | within |
| Focus next | 50.0 ms | 3.05 ms | 3.32 ms | within |
| Mouse hover | 30.0 ms | 9.45 ms | 9.56 ms | within |
| Mouse click | 100 ms | 11.4 ms | 11.1 ms | within |
| Wheel scroll, single | 60.0 ms | 64.1 ms | 18.3 ms | within |
| Command palette close | 100 ms | 143 ms | 53 ms (run 221040) |
within |
| Terminal resize | 250 ms | 260 ms | 91 ms (noisy, run 221040) |
within |
| Idle repaints | 2 per s | 20.7 per s | 2.0 per s, 2.4% CPU | at budget |
17. What the data says about the next step
From the latest runs (deep run 20260930-214915 for interactions, startup run 20260930-215938 for start-up; calibration error 6.5%):
- The
statand read forks are done (change 8): warm start 308 ms, almost no subprocesses left in start-up. - Cold start is 2.87 s after changes 12 and 13 (was 5.54 s), of which 1.09 s is validating every page (
_tui_validate.attrs,tables,walk) and 1.53 s the cache-building splash (_tui_parse.scan376 ms self,_tui.theme_parse84 ms). It is no longer fork-bound (786 forks left, about 0.26 ms each); further gains need a cheaper parse and validation. - Layout dropped from 42 to 26 ms per page switch (change 9). The largest functions left in the draw path are
_tui._draw_widget_buf(61 ms self in the traced run),_tui._inset(58),_tui._widget_pos(55) and_tui.emit_goto(45), with⟨fork⟩at 63 ms (mostly page code and the_calc_boundsawk). - Page code (
on_visit):docs175–190 ms,components163–168 ms,monitor92–99 ms, and thecomponentsrender is 200 ms for a 103 KB frame.show_allbuilds its output with 13 command substitutions. - Forks left in input paths:
tui.theme.list(tui_api.sh:671) on palette open (3 forks + 1 program), 1 fork per mouse click.
Order:
- Remaining page code and framework work on heavy pages:
docsspends about 89 ms attributed intui.tabs.build,monitor~93 ms collecting system stats (inherent),scrolling433 ms (output rendering of 300 lines), andcomponents276 ms. Change 11 already removed the duplicate render and the docs forks. Page script re-sourcing (_tui_cache_source, 16.4 ms inclusive),tui.tick.add(12 ms self with one listener, unexplained) andpgrep(6.5 ms) are next;_tui.emit_goto(8.6 ms) is the largest remaining draw-path function. - Page code: read
on_docu_visit,on_monitor_visitand thecomponentsrender. - Cold start: the page validation (1.08 s) and parse/build cost; a parallel build or a cheaper parse. (Done in changes 23 to 24: 4.3 s to 1.4 s, section 15m.)
- Remaining forks:
tui.theme.liston palette open,_tui._calc_boundsawk. - Dirty flags and one draw per tick stay demoted (the extra frames are cheap).
Deferred: _style_v baking and a per-theme snapshot (about 40 ms inside a rare event), re-capturing the snapshot after a re-bake with a theme overlay active, and the shutdown increase (31 → 54 ms).
18. Method notes and caveats
Run-to-run stability (8-round nav run, 20260930-213033)
- Page revisit is very stable. Per page, the median coefficient of variation across the 8 rounds is 0.8% (most pages 0.5–1.2%; one page shows 10%). Round medians are 261–270 ms (±2%). Differences of a few ms between runs are real signal for this scenario.
- First visit is noisier. Per-page median CV 7.5% (range 0.5–18%), round medians 253–291 ms (±7%). The first visit of a page includes one-time work, so compare first-visit numbers only across runs with several rounds.
- The large overall spread is the page mix, not instability. Overall CV is 33% (183–529 ms) because the scripted session visits cheap and heavy pages; pages repeat their own value from round to round.
- Pane-drawing milliseconds were unstable across earlier runs (see change 4 notes) but agree across the last three (11.7–12.0 ms).
nav.key(alt+2) is bimodal, and the median misleads. It alternates between 10.9 ms (nothing happens) and ~67 ms (a 687 B frame is written) and in this run strictly every other round (39 ms median of 10.9 / 67.4). It also appears in 4 of the 12 earlier runs (for example[11, 11, 74, 11, 11]). In all 13 runs the key never navigates. Cause not investigated; the 687 B frame also shows up in click results, so it may be a hover or status repaint hitting the key’s window.
Other notes
- Baseline and after-runs differ in round counts (5 vs 3–4) and, for the after-runs, in scenario scope. Medians are compared, not means, and differences under ~3% are treated as noise.
- Attribution numbers exclude blocking time (input waits, debounce) from the trace-to-real scale factor after a calibration fix on 2026-09-30; wall-time numbers are unaffected by it.
- The memo and
_tui._vsliceboth live in uncommitted changes inlib/tui.sh; the_vsliceoutput equivalence was tested in isolation and through matching byte counts, not through the full unit test suite. - Update this document after each optimisation: add the change, expected vs real, and refresh the standing table in section 16.
19. Running comparison per revision
Each column is the state of the code after that change (changes that were measured together share a column: c1-2 = changes 1 and 2, c3-4, c6-7; the others are one change each). The values are the medians from the run closest to that revision, all in the sections above; a value in parentheses was not measured in that step’s run and is carried forward from the previous one, and – means the metric did not exist yet. The trend column is a sparkline of the carried series (low → high within each row), and the last column is the change from the first to the last value.
| metric | base | c1-2 | c3-4 | c5 | c6-7 | c8 | c9 | c10 | c11 | c12 | c13 | trend | total |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Page switch, first visit (median) (ms) | 356 | 320 | 295 | 260 | 228 | (228) | 201 | 245 | 208 | 208 | (208) | █▆▅▄▂▂▁▃▁▁▁ |
-42% |
| Page switch, revisit (median) (ms) | 370 | 338 | 325 | 264 | 225 | (225) | 216 | 213 | 216 | 207 | (207) | █▇▆▃▂▂▁▁▁▁▁ |
-44% |
| Page switch, mean of all pages (ms) | 425 | 372 | 346 | 304 | 274 | (274) | 250 | 261 | 240 | 219 | (219) | █▆▅▄▃▃▂▂▂▁▁ |
-48% |
| Palette close (ms) | 143 | 88.1 | 61.8 | (61.8) | 57.5 | (57.5) | 53.1 | (53.1) | (53.1) | (53.1) | (53.1) | █▄▂▂▁▁▁▁▁▁▁ |
-63% |
| Wheel scroll, single (ms) | 64.1 | 58.6 | 58.7 | (58.7) | 18.3 | (18.3) | (18.3) | (18.3) | (18.3) | (18.3) | (18.3) | █▇▇▇▁▁▁▁▁▁▁ |
-72% |
| Mouse hover (ms) | 9.4 | 9.4 | 9.6 | (9.6) | 9.6 | (9.6) | (9.6) | 8.2 | (8.2) | (8.2) | (8.2) | ▇▇█████▁▁▁▁ |
-14% |
| Mouse click (ms) | 11.4 | 11.2 | 11.6 | (11.6) | 11.1 | (11.1) | (11.1) | 10.2 | (10.2) | (10.2) | (10.2) | ▇▆██▅▅▅▁▁▁▁ |
-11% |
| Terminal resize (noisy) (ms) | 260 | 157 | 207 | (207) | 219 | (219) | 90.9 | (90.9) | (90.9) | (90.9) | (90.9) | █▄▆▆▆▆▁▁▁▁▁ |
-65% |
| Idle repaints (/s) | 20.7 | 20.2 | 2.0 | (2.0) | 2.0 | (2.0) | (2.0) | (2.0) | (2.0) | (2.0) | (2.0) | ██▁▁▁▁▁▁▁▁▁ |
-90% |
| Warm start (ms) | 857 | 861 | 850 | (850) | 823 | 308 | (308) | (308) | (308) | 303 | 287 | █████▁▁▁▁▁▁ |
-66% |
| Cold start (ms) | 5836 | 5678 | 5610 | (5610) | 5597 | 5536 | (5536) | (5536) | (5536) | 3113 | 2867 | ██▇▇▇▇▇▇▇▂▁ |
-51% |
| Theme switch, first use (ms) | – | 380 | 349 | (349) | 306 | (306) | (306) | (306) | (306) | (306) | (306) | ` █▅▅▁▁▁▁▁▁▁` | -19% |
Note: the change-10 first-visit value (245 ms) is the known first-visit swing, not a regression (its attributed time did not change; see change 10). Reading guide: the page-switch rows are the ones to watch for user impact; the mean row is the most sensitive to page-code changes because the median sits on the light pages. Idle repaints is frames per second with nothing happening. Resize is shown for completeness but its three targets swing between runs.
Held keys (change 21-22)
Separate table: the held groups did not exist before this change. Medians of per-press lag (ms), same stage-2 build.
| group | before | after |
|---|---|---|
held.nav |
8.5 | 4.5 |
held.tab |
33.2 | 21.2 |
held.list |
4.5 | 4.4 |
held.table |
7.3 | 6.6 |
held.page |
4.8 | 4.6 |
held.cursor |
2.7 | 2.6 |
held.type |
2.5 | 4.3 (noise) |
Charts
xychart-beta
title "Page switch (ms): first-visit median, revisit median, mean of all pages"
x-axis ["base", "c1-2", "c3-4", "c5", "c6-7", "c8", "c9", "c10", "c11", "c12", "c13"]
y-axis "ms" 0 --> 440
line [355.5, 320.1, 295.1, 260.2, 227.5, 227.5, 201.0, 244.6, 207.7, 207.7, 207.7]
line [370.4, 338.5, 325.0, 264.5, 225.2, 225.2, 215.9, 212.7, 216.0, 207.4, 207.4]
line [424.8, 372.1, 345.8, 303.6, 273.6, 273.6, 249.9, 261.3, 239.9, 219.2, 219.2]
(Series in order: first visit, revisit, mean.)
xychart-beta
title "Palette close and wheel scroll (ms)"
x-axis ["base", "c1-2", "c3-4", "c5", "c6-7", "c8", "c9", "c10", "c11", "c12", "c13"]
y-axis "ms" 0 --> 160
line [142.7, 88.1, 61.8, 61.8, 57.5, 57.5, 53.1, 53.1, 53.1, 53.1, 53.1]
line [64.1, 58.6, 58.7, 58.7, 18.3, 18.3, 18.3, 18.3, 18.3, 18.3, 18.3]
(Series in order: palette close, wheel scroll single.)
xychart-beta
title "Warm start (ms)"
x-axis ["base", "c1-2", "c3-4", "c5", "c6-7", "c8", "c9", "c10", "c11", "c12", "c13"]
y-axis "ms" 0 --> 900
line [856.7, 861.1, 849.6, 849.6, 822.9, 307.6, 307.6, 307.6, 307.6, 303.0, 287.0]
xychart-beta
title "Cold start (ms)"
x-axis ["base", "c1-2", "c3-4", "c5", "c6-7", "c8", "c9", "c10", "c11", "c12", "c13"]
y-axis "ms" 0 --> 6000
line [5836.1, 5677.8, 5610.3, 5610.3, 5596.6, 5536.1, 5536.1, 5536.1, 5536.1, 3113.3, 2867.0]
xychart-beta
title "Idle repaints (frames per second)"
x-axis ["base", "c1-2", "c3-4", "c5", "c6-7", "c8", "c9", "c10", "c11", "c12", "c13"]
y-axis "frames/s" 0 --> 25
line [20.7, 20.2, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
xychart-beta
title "Hover and click (ms)"
x-axis ["base", "c1-2", "c3-4", "c5", "c6-7", "c8", "c9", "c10", "c11", "c12", "c13"]
y-axis "ms" 0 --> 14
line [9.4, 9.4, 9.6, 9.6, 9.6, 9.6, 9.6, 8.2, 8.2, 8.2, 8.2]
line [11.4, 11.2, 11.6, 11.6, 11.1, 11.1, 11.1, 10.2, 10.2, 10.2, 10.2]
(Series in order: hover, click.)
Update this section together with the matching change section after each optimisation: add a column and a row value per metric that was re-measured.
