Skip to content

Optimize main-turn allocation churn (per-turn full-conversation re-marshal + chunk decode + render) #157

Closed

Description

@JAORMX

Summary

Live monitoring showed the allocation rate is dominated by the main agent's own turns, not by subagent fan-out. During a main synthesis turn the GC rate burst to ~8.7 GC/s (≈131 GCs in one 15 s window), with ~4 GC/s on other main turns — and crucially these spikes occurred with active_runs{role=subagent} = 0, so it is not the children. Allocation rate jumped from ~2 MB/s idle to 40–53 MB/s during these turns.

Where the allocations come from

With store:false, every turn re-marshals the full accumulated conversation to the provider format; on top of that each turn does streaming chunk-decode and (mecatui-side) scrollback render. All three grow with conversation length, which is why the spikes land on the late, large main turns (synthesis over many prior subagent results).

Candidate hot paths to profile first:

Important caveat (priority)

This is latent optimization, not a live problem: GC pauses stayed ≤ 262 µs p99 and GCCPUFraction ≈ 0.0005 (0.05% of CPU) throughout — GC absorbs the churn fine, and the session is model-bound (8.8% CPU). Reducing allocations will not speed up a normal session; the payoff is lower GC churn / steadier inter-token behavior on very long sessions. Treat as polish behind the correctness work (#152/#153).

Approach

Drive it from the offline harness, not live monitoring (live is non-deterministic and model-dominated):

  • task bench (+ the allocs-gate) and task perf:scenarios
  • memprofile → go tool pprof → benchstat A/B (the perf-optimization skill)
  • keep pure-perf changes byte-identical; mutation-test any cache guards

Acceptance

  • A ranked allocs/op attribution for the main-turn path (marshal vs decode vs render).
  • A benchstat-proven allocs/op reduction on the targeted site(s) with no behavior change.
  • A regression benchmark guarding the win.

Found via live perf monitoring (the alloc-rate spike correlated to sRun=0 main turns).

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions