Skip to content

perf(plugins,restore): sleep unused plugins while active and debounce restore.persist - #2559

Merged
Hona merged 2 commits into
devfrom
perf/plugin-dormancy-and-persist
Dec 6, 2026
Merged

Hona merged 2 commits into
devfrom
perf/plugin-dormancy-and-persist

Conversation

@Hona

@Hona Hona commented Dec 6, 2026

Copy link
Copy Markdown
Member

Fixes two active-session overhead sources seen in live v0.0.0-dev-543 traces (opencode-perf-20261206-043341, 043755, 044001; runningPluginCount: 16 and lastRestReclaim: null in all three) and removes four small monotonic allocations.

Root Cause

  • Plugins never slept while active: dormant::sleep_unused ran only from App::on_rest (10 s with zero frames on Home with zero tabs). During continuous 24/7 work, all 16 bundled PsychoScript VMs stayed resident indefinitely.
  • restore.persist ran up to 102×/sec (277× in 13.3 s, 91.5 ms total, max 1.233 ms in 043755; 81× in 12.6 s in 044001): 270 of the 277 hits occurred across a 2.65 s (305-frame) burst of input frames (pollN: 2, avg 331 µs). poll_restore_versioned had zero debounce, so every RestoreVersions bump (side/bottom/split/sidebar resize drags, composer typing, or session updates) immediately cloned open tabs, built serde_json::Value trees for side, and ran side_stamp_where(&is_rest) across every historical session.

Changes

1. Periodic active-window plugin dormancy (apps/desktop/src/plugin_host/runtime/dormant.rs)

  • Added explicit SweepPhase (Idle | Armed { due }) and Warmth (Warm { since, cooldown } | Slept { at, cooldown }) state machines stored in Cell/RefCell so polls never dirty Tracked<PluginHost>.
  • Piggybacks on dormant::poll every 60 s with zero idle wake timers (App::on_rest remains undisturbed). Sleeps at most 1 unused, non-busy, cold (>= 120 s) bundled plugin VM per interval and runs purge_free_pages_soft() on native targets.
  • Anti-ping-pong backoff: if a plugin re-wakes within 15 s of active sleep (e.g., an on-screen code block requesting want_now), its cooldown doubles up to 960 s.
  • Expanded busy() and added shown_in_window() to protect open perf monitors, visible debug bar, active-route slots/pills/open side & bottom panel tabs/narrow drawer, New tab dynamic launchers, file_viewers when file is open, titlebar_items, open aux windows, live embeds, host prompt dialogs, Plugin Inspector targets, in-flight MCP tool calls, pending requests, active plugin themes, active model badges / Go usage, and remembered/listed/pending server sources (ssh/wsl).
  • Interned plugin.<id>.woke perf-mark strings in WAKE_MARKS instead of calling Box::leak on every wake.
stateDiagram-v2
    [*] --> Warm: Started (cooldown = 120s)
    Warm --> Warm: InUse (refresh since)
    Warm --> Warm: IdleCheck (elapsed < cooldown)
    Warm --> Slept: IdleCheck (elapsed >= cooldown, 1/sweep)
    Slept --> Warm: Started within 15s (cooldown *= 2, max 960s)
    Slept --> Warm: Started after 15s (cooldown = 120s)
Loading

2. Debounced restore.persist (features/restore/, apps/desktop/src/workspace/lifecycle/restore.rs)

  • Added explicit PersistDebounce (Clean | Pending { seen, first_dirty, due, wake_armed }) in features/restore/src/state.rs (stored in Cell, zero RESTORE slot bumps).
  • poll_restore_debounced coalesces scheduled restore.persist ticks with a 1.0 s trailing settle delay and 2.0 s max wait while keeping initial/hydration flushes, crash-recovery set_active_exit_session, and draft blob upload/arrival checks immediate.
  • Window hide (tick::visibility), save_on_close, and save_on_quit still call immediate poll_restore, which steps PersistDebounceEvent::Flushed and writes synchronously with zero lost state.
  • Guarded idle AtomicBool::swap(false, AcqRel) checks behind load(Ordering::Acquire) in copies.rs, draft_doc.rs, and undo.rs.
stateDiagram-v2
    [*] --> Clean
    Clean --> FlushNow: First poll or hydration change
    Clean --> Pending: Version changed (due = now + 1s)
    Pending --> Pending: Changed (due = min(now + 1s, first + 2s))
    Pending --> FlushNow: now >= due
    Pending --> Clean: Flushed (hide/close/quit) or matched
    FlushNow --> Clean
Loading

3. Bounded maps & cache audit

  • features/restore/src/parked.rs & lib.rs: Skip cloning empty no-history TextEdits into Restore::edits on route switches/closes and prune non-draft undo-only entries past MAX_KEPT_EDITS = 32.
  • features/restore/src/draft_doc.rs: Prune dead Weak<[u8]> entries from Blobs::ids on insert once len >= 64.
  • features/restore/src/preview.rs: Retain only active/open-tab entries in Previews once len > 64.
  • Audited and left untouched (already bounded): session_links/mod.rs (50 × 20 LRU/FIFO), code_node.rs (64 entries / 4 MiB), custom_diff.rs (64 docs / 16 MiB per plugin, reaped on VM stop/reload).

@Hona
Hona merged commit 97f572d into dev Dec 6, 2026
8 checks passed
@Hona
Hona deleted the perf/plugin-dormancy-and-persist branch December 6, 2026 05:59
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