Repository navigation
Description
Summary
Model.View() recomputes m.vp.View() (cmd/mecatui/ui/view.go:96, the default body branch) on every Bubble Tea frame, with no cache. The bubbles viewport's View() (charm.land/bubbles/[email protected]/viewport/viewport.go:744-748) re-runs a lipgloss Width().Height().Render(strings.Join(visibleLines, "\n")) on each call, which re-measures every grapheme cluster's display width from scratch. During a long reasoning turn the footer spinner ticks ~10×/s and each tick forces a full Model.View() → full viewport re-pad, even though the viewport body is byte-identical frame-to-frame. The result is wasted CPU and visible render lag ("text appears slowly") while the model thinks.
This is the residual the update.go:2474 comment foresaw — #80 cached the content assembly (the SetContentLines incremental prefix); this issue is about caching the viewport's rendered output.
Evidence (live, from a running mecatui --perf --perf-mcp)
15s CPU profile captured during active GLM-5.2 streaming (pprof on :9099):
1.31s 50.00% github.com/stacklok/mecatl/cmd/mecatui/ui.Model.View
1.16s 44.27% charm.land/bubbles/v2/viewport.Model.View (viewport.go:746: Render(Join(visibleLines)))
0.71s 27.10% charm.land/lipgloss/v2.Wrap
0.60s 22.90% github.com/charmbracelet/x/ansi.stringWidth
Flat (where CPU actually burns):
190ms uax29/graphemes.Iterator.First | grapheme segmentation
120ms uax29/graphemes.Iterator.Next |
60ms ansi.stringWidth | display-width measurement
110ms runtime.mallocgc | a fresh padded string built per frame
Total CPU was only ~17.5% (not saturated) — so the felt slowness is per-frame latency/wasted work on the single render goroutine, not throughput. Tokens themselves arrive fast (mecatl_inter_token_seconds mean ≈ 6.5ms); reasoning.delta events outnumber message.delta ~7.5:1, so the spinner-driven static-content frames dominate a turn.
Root cause
func (m Model) View() tea.View(value receiver) callsm.vp.View()unconditionally in itsdefaultbranch. There is no memo of the result.- Bubble Tea calls
View()on every flushed frame. WhilespinnerVisible()is true (exactly the long-think window),spinner.TickMsg→m.sp.Updatere-arms the tick and produces a new model → a full re-render. The footer glyph is all that changed, but the whole viewport re-pads. - The existing caches don't cover this:
renderer.cache/renderer.blockCache(mecatui: streaming render re-joins whole scrollback every token (O(scrollback) alloc churn, ~475 MB/s / 13 GC/s) #80) memoize markdown→styled lines fed into the viewport viaSetContentLines; the viewport's ownView()lipgloss pad overvisibleLines()is recomputed every frame regardless.
Proposed fix
Memoize the m.vp.View() output string, invalidated only when the rendered result can actually change. Because View() has a value receiver, the cache must live behind a pointer (mirror the established m.rend *renderer pattern, which already holds the map-backed caches).
Cache key / invalidation inputs (all already funnel through known sites):
- content —
refreshView()(them.viewDirty/render-tick path;SetContent/SetContentLinesatupdate.go:2488,2528,2374) - scroll offset —
ScrollUp/ScrollDown(update.go:2270/2272),GotoTop/GotoBottom(2448/2450/2490/2530),m.vp.Updatescroll (1963/2452) - geometry —
SetWidth(update.go:779),SetHeight(877)
A monotonically-bumped version counter (or a single vpViewDirty flag) set at those sites, cleared after a cached render, is sufficient. Spinner-only / cursor-blink / no-change frames then reuse the cached body and only the footer re-renders.
Scope guard: this is the only branch in question — overlay branches (m.ask/m.models/… in view.go:66-86) already short-circuit body before the default, so they're unaffected.
Why a new benchmark is needed (don't trust the existing one)
The current streaming bench (scrollback_bench_test.go, scrollback_revise_determinism_test.go) is all-miss by construction — the live tail re-renders every token, so a vp.View() memo never hits there and the bench will show ~no win. That's the "all-miss benchmark hides the win" pitfall. Add a steady-state bench: static viewport content + N spinner/no-content-change frames (the real long-think shape), where the memo should drop allocs/op and ns/op to ~the footer-only cost.
Acceptance criteria
-
m.vp.View()is served from cache on frames where content, scroll offset, and geometry are all unchanged. - Steady-state (spinner-only) bench shows
allocs/op+B/opdrop with a significant benchstat delta; the existing streaming bench does not regress. - Byte-identical output:
task test:goldengreen with zero golden diffs (a plaingo test ./cmd/mecatui/...against committed goldens, not the-updaterefresh). - The cache key is mutation-tested: break each invalidation site (content/scroll/resize), confirm a test/golden goes red, restore — proving the guard isn't vacuous.
- No new exported
engine/surface (TUI-only;api-compatgate untouched).
Notes / provenance
- Observed on a running
mecatui -yolo --perf --perf-mcp(repo7d4cc438), model GLM-5.2 via Z.ai, mid PR-review. - Part of the felt slowness on that instance is the
--perfflight recorder (continuousruntime/trace) adding scheduling overhead — that's only present under--perfand absent in a normal binary; it is not part of this fix, but worth keeping in mind when reproducing. - Follows mecatui: streaming render re-joins whole scrollback every token (O(scrollback) alloc churn, ~475 MB/s / 13 GC/s) #80 (closed) — content-assembly cache. This is the render-output cache the
update.go:2474comment deferred as out of scope there.
Severity: enhancement / perf. User-visible as render lag during reasoning-heavy turns.
Activity
perf(mecatui): memoize vp.View() output to skip lipgloss re-pad on sp…
perf(mecatui): wire vpView bench into perf gate + per-handler invalid…
perf(mecatui): fix vacuous vpView bench + cover height/reset paths (#139