Skip to content

mecatui: streaming render re-joins whole scrollback every token (O(scrollback) alloc churn, ~475 MB/s / 13 GC/s) #80

Closed

Description

@JAORMX

Summary

During active streaming, mecatui allocates at hundreds of MB/s and triggers GC many times per second, because it re-joins the entire conversation scrollback and re-runs viewport.SetContent on every streamed token. The cost is O(scrollback-bytes × token-rate), so it worsens as the conversation grows. Idle is ~0.3 MB/s, so this is purely a streaming-render hot-path problem (GC pressure, not a leak).

Measurements (live, via /debug/pprof)

Sampled a real session (deepseek/deepseek-v4-flash over OpenRouter):

State Alloc rate GC rate
Idle ~0.3 MB/s 0/s
Streaming, early turns ~20–90 MB/s <1–3/s
Streaming, long-text turn at deep scrollback ~475 MB/s ~13/s
Streaming burst, re-confirmed at depth 188 MB/s peak —

Allocation profile (alloc_space) over ~10 min — top of the churn is the render path, not the agent core:

  • strings.(*Builder).WriteString — 1.11 GB (31%), almost entirely from lipgloss.alignTextHorizontal (41%) + lipgloss.Style.Render (37%) + ui.(*renderer).renderConversation (6.8%)
  • ui.Model.Update — cum 0.82 GB (23%); Model.Update:169 (model = mm) alone is 221 MB flat
  • bytes.growSlice 0.50 GB

The churn is bursty — it spikes during long assistant-text streaming and is cheap on tool-heavy turns.

Root cause

cmd/mecatui/ui/render.go renderConversation has a join fast-path (r.joinValid && r.blockRenders == before && ...) that reuses the joined string only if no block re-rendered this frame. During streaming, the live tail block re-renders on every token → blockRenders bumps → fast path misses → the strings.Builder re-joins all N blocks every token, then vp.SetContent re-splits/measures every line. Settled-block caching (blockCache) doesn't help because the join dominates. (This is the SetContent O(scrollback) follow-up that was previously flagged.)

Compounded by the large Model value struct boxing into the tea.Model interface on every Update (the 221 MB flat at Model.Update:169) — every streamed token / event / tick heap-allocates the whole model.

Direction (not prescriptive)

  • Incremental join: keep a cached joined prefix of settled blocks; only re-render/append the live tail, rather than re-joining all blocks every frame.
  • Avoid re-SetContent-ing the full scrollback when only the tail changed.
  • Reduce the per-Update Model heap-escape.
  • Keep pure-perf changes byte-identical and mutation-test any new cache guard (per the perf skill discipline). The existing scrollback_bench_test.go is the regression anchor.

Filed from a live debugging session.

Activity

  1. added
    tui
    on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    tui

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions