Skip to content

perf(timeline,session-view): eliminate per-frame heap allocation churn during streaming - #2564

Merged
Hona merged 3 commits into
devfrom
perf/reduce-frame-alloc-churn
Dec 6, 2026
Merged

Hona merged 3 commits into
devfrom
perf/reduce-frame-alloc-churn

Conversation

@Hona

@Hona Hona commented Dec 6, 2026

Copy link
Copy Markdown
Member

Summary

Eliminates per-frame heap allocations, deep JSON clones, and spurious Tracked invalidations across crates/session-view, features/timeline-extras, and apps/desktop/src/shell/view/{session,composer}.rs during 165 Hz streaming while preserving full animation framerate and zero visual changes.

Live trace motivation (opencode-desktop.exe v0.0.0-dev-543)

  • 043341: meanAllocBytes = 782 KB/f (1,570 / 2,000 view frames without plugin surfaces averaged 1,245 KB/f, 4,085 allocs/f, p50 = 1,438 KB/f), Memo 80.7%, Paint 72.8%, spike 17.3 MB (54,284 allocs).
  • 043755: meanAllocBytes = 603 KB/f (view frames without plugin surfaces averaged 1,184 KB/f, 5,994 allocs/f), Memo 86.1%, Paint 71.4%, spike 16.2 MB (63,907 allocs).
  • 044001: meanAllocBytes = 215 KB/f (view frames without plugin surfaces averaged 875 KB/f, 4,016 allocs/f, spikes up to 2.43 MB/f), Memo 64.5%, Paint 58.3%.

What changed

1. Memoize TurnDiff, TurnDivider, and settled Compaction rows + drop FileDiff.Info deep clones

  • crates/session-view/src/tools/files.rs, tools/mod.rs, turn_diff.rs: Added tools::shut_files borrowing &[serde_json::Value] directly so turn_diff::view no longer deep-clones FileDiff.Info JSON (d.to_vec() / .take(LIMIT).cloned()) into a synthetic serde_json::json!({ "files": files }) object on every frame. Added turn_diff::state_stamp (allocation-free FxHasher over summary counts and visible file patches) and turn_diff::view_state to reuse the fetched State.
  • crates/session-view/src/timeline/row_build.rs, compaction.rs: Memoized RowContent::TurnDiff, RowContent::TurnDivider, and settled (status != Running) RowContent::Compaction (via compaction::memo_stamp) so unchanged diffs, dividers, and expanded compaction Markdown summaries hit rune::memo across streaming frames.

2. Stop spurious 165 Hz Tracked clock & drip invalidations

  • crates/session-view/src/jump.rs: Deferred s.clock() in Look::show / show_look behind impl FnOnce() -> f64 after the shown == was.shown && rise == was.rise early return, stopping over_scroll from reading Tracked<f64> on every steady frame. Replaced entry(id.to_owned()) with a read-first get(id) lookup.
  • crates/session-view/src/compaction.rs: Switched active compaction divider phase anchoring (quiet_view, light) from tracked s.clock() / s.now_ms() to s.working_state().poll_clock() and s.now_ms_untracked() (matching working::label_str). Loop animations remain at full 165 Hz in Rune's compositor.
  • crates/session-view/src/line_reveal.rs: Moved internal bookkeeping (drip, poll, pending, seen) to RefCell and touch s.line_hold_mut() only when visible pacing/draining state actually changes, preventing sub-word cursor ticks and non-text transcript updates from invalidating timeline-fade. Eliminated per-frame PartKey / sid / message_id clones on existing drip entries, reused pending capacity in place, and replaced per-poll HashSet<(usize, usize)> with a pre-sized Vec.

3. Eliminate per-frame row memo-key & lookup allocations

  • crates/session-view/src/timeline/row_build.rs:
    • Pass self.key.as_str() to rune::memo instead of self.key.clone().
    • Remove dead prompt_meta calls in User / Pending rows (user_bubble ignores meta); removes turn-wide scans, ModelRef clones, and Vec<(String, bool)> allocations twice per user row per frame (row_height::shape + row_build).
    • TurnText: use copied::stamp(s), group_hash::code_wrap_stamp(...), borrowed viewing.id.as_str(), and parts::has_reply_hover(...) (instead of format!("reply-hover-{id}")).
    • Turn::Group (collapsed): drop redundant s.expanded().stamp("r-") (thought-only groups already hash their own open/shutting; non-thought groups only read r- when open/shutting), preventing thought toggles from invalidating unrelated collapsed groups.
    • Turn::Group (open), Turn::Thought, Turn::Calls, Turn::Subagent, Turn::Question: reuse already-materialized Step / Vec<Step> inside memo_row closures instead of reconstructing twice on miss; inspect only at.first() for closed Thought rows (zero Vec<Step> allocation); pass None for group_key on standalone Calls rows.
    • TurnFooter / TurnTail: use parts::reply_meta_stamp (borrowing a.model.as_ref() and hashing raw timestamps/TPS without formatting tooltip/duration strings or allocating a Vec), copied::stamp(s), and copied::is_copied_prefixed(s, "copy-", copy_id).
  • crates/session-view/src/timeline/group_hash.rs: Replaced (u64, String) LRU lookup with borrowable (u64, &str) (with_entry, zero t.session.clone() per row per frame and zero .expect()), updated first_id/last_id in place on invalidate_from, replaced format!("{:?}", n.kind) with zero-alloc hash_notice_kind, and switched tools_aux to stamp_parts + copied::stamp.
  • crates/session-view/src/timeline/turn_cache.rs: Replaced (u64, String, String) LRU lookups with borrowable (u64, &str, &str).
  • crates/session-view/src/timeline/wrap.rs, fold.rs, disclosure.rs, copied.rs, recall.rs, timeline/mod.rs, timeline/parts.rs, timeline/row_height.rs:
    • RowSpec::disclosure returns Cow<'_, str> (zero alloc for c-* / fold).
    • Added allocation-free stack-buffer Disclosures::get_prefixed / get_parts, stamp_parts, any_shutting_parts, none_touched, and exported fold::KEY_PREFIX.
    • Added allocation-free copied::stamp, copied::stamp_containing, copied::is_copied_prefixed, and copied::is_empty (also used in tools::cached_with).
    • Recall::marks checks get(session) before allocating session.to_owned().
    • parts::footer_text checks the message's own trailing text part first and scans forward t.messages[message + 1..] to short-circuit earlier turns immediately.
    • timeline::turn_of clears and reuses Vec<usize> capacity on cache rebuild.

4. Eliminate cache/helper cloning in turns, sources_chip, timeline-extras, and shell/view

  • crates/session-view/src/turns/codemode/cache.rs: Borrowable (&str, bool, bool) lookup key (zero id.to_owned() on hit) and length + head/tail 32-byte attachment hashing instead of hashing full Arc<[u8]> image payloads.
  • crates/session-view/src/turns/labels.rs: Check SEEN_GROUPS.contains(key) before allocating key.to_owned().
  • crates/session-view/src/sources_chip.rs: Return impl Iterator<Item = &str> from results, hash normalized URL parts without allocating in hash_assistant_sources, avoid calling page(url) twice for webfetch, and stop collecting sites past 3.
  • features/timeline-extras/src/todos.rs: Store Rc<[Todo]> in PLAN cache and update cached ver / seen_ver in place without reallocating id / revert or deep-cloning Vec<Todo> on every streaming poll/frame; defer id.to_owned() in pill until a pill renders; compute panel_width once in build_panel_content.
  • features/timeline-extras/src/agent_rows/mod.rs: Replace intermediate Vec<&String> allocation in steps_in with a fixed [&str; KEEP] stack ring.
  • apps/desktop/src/shell/view/session.rs: Pass borrowed Option<&str> for opening error and highlighted row into timeline-fade region inputs; construct pills() in a single pre-sized Vec<El> (zero alloc when empty).
  • apps/desktop/src/shell/view/composer.rs: Replace per-step vec![30.0, 0.0, 28.0] allocations in folds() with stack (sum, count) accumulators.

Verification

  • git diff --check: clean
  • pwsh scripts/guardrails.ps1: 12/12 passed (all 24 touched .rs files <= 800 lines; largest is crates/session-view/src/timeline/row_build.rs at 797 lines; zero tests; zero new .cursor(...); zero std::time::Instant / SystemTime::now)
  • cargo clippy --no-deps -p opencode-feature-timeline-extras -- -D warnings: pass (0 warnings)
  • cargo check -p session-view --examples: pass (0 warnings in session-view)
  • cargo check -p opencode-desktop --no-default-features --features knobs: pass (0 warnings in touched files)
  • cargo check -p opencode-web --target wasm32-unknown-unknown: pass (0 warnings in touched files)

Hona added 3 commits December 6, 2026 15:43
…c-churn

# Conflicts:
#	crates/session-view/src/recall.rs
#	crates/session-view/src/timeline/row_height.rs
@Hona
Hona merged commit 5489bd8 into dev Dec 6, 2026
8 checks passed
@Hona
Hona deleted the perf/reduce-frame-alloc-churn branch December 6, 2026 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant