Skip to content

perf(memory): add 24/7 active mimalloc purge and staggered plugin GC - #2558

Merged
Hona merged 2 commits into
devfrom
perf/active-24x7-memory-reclaim
Dec 6, 2026
Merged

Hona merged 2 commits into
devfrom
perf/active-24x7-memory-reclaim

Conversation

@Hona

@Hona Hona commented Dec 6, 2026

Copy link
Copy Markdown
Member

Returns unused mimalloc arena pages to the OS and runs staggered single-VM PsychoScript JS GC during continuous 24/7 active streaming, when App::on_rest (10 s with zero frames) never fires.

Live evidence (opencode-desktop.exe v0.0.0-dev-543, rec_7e009c)

  • Page-by-page QueryWorkingSetEx PTE scan measured 409.5 MB commit / 369.7 MB resident in mimalloc 1 GiB arenas against ~186 MB Heap live (~184 MB resident / ~224 MB commit unpurged, 2.0x live heap).
  • At t = 166 -> 170 s, Heap live spiked +22.1 MB and dropped -23.2 MB, while RSS increased +1.9 MB (0 MB returned to Windows): lastRestReclaim stayed null for 7 minutes, the 30 s active sweep only called mi_collect(false) when entries were removed, plugin JS GC ran only on on_rest, and parked plugin-worker-* threads kept thread-local heaps uncollected.

Changes (explicit state machines, zero new App fields)

  • Post-drop and ratio-aware active mimalloc purge (apps/desktop/src/shell/memory_reclaim.rs):
    • Added PurgeTracker (Unprimed / Tracking { peak_live, soft_sweeps } / Uncounted { soft_sweeps }) inside MemorySweep::Scheduled (automatically cleared on Rested, zero edits to kernel.rs).
    • Tracks intra-sweep procwatch::alloc::live_bytes() peaks via two relaxed atomic loads with 1 MiB quantization hysteresis so transient spikes between 30 s ticks (such as +22.1 MB -> -23.2 MB in 4 s) are captured without touching the SWEEP slot on small allocations.
    • On every 30 s active sweep (after eviction and single-VM plugin GC): runs purge_free_pages_soft() (mi_collect(false)) unconditionally when a forced purge is not due, or collect_thread_heap() (mi_collect(true) — visits full pages and purges all free arena slices back to the OS) when live heap dropped >= 8 MiB from peak, every 60 s (2 sweeps) while OS retained memory (max(private_commit, footprint, resident)) exceeds live heap by >= 128 MiB, or every 120 s (4 sweeps) unconditionally.
    • Queries procwatch::usage() only when needs_os_retained is true (skipped on sweeps 1, 4, and drop-triggered sweeps).
    • Resets PurgeTracker via SweepEvent::ExternalPurge on on_tab_closed after its immediate purge_free_pages().
    • Keeps synchronous Windows HeapCompact(GetProcessHeap(), 0) strictly on App::on_rest and on_tab_closed to avoid locking the Windows process heap on active frames (preserving PR perf(stream,pages,reclaim): gate step reloads, coalesce SSE deltas, bound poll slices, soften mi_collect, dedupe wakes #2496).
  • Staggered incremental PsychoScript JS GC (apps/desktop/src/plugin_host/runtime/memory.rs, apps/desktop/src/plugin_host/feature.rs):
    • Added explicit GcCursor (Idle / WindowHalf { index } / AppHalf { index }) slot GC_CURSOR registered in PluginHostFeature::register.
    • Added collect_garbage_one(app) / PluginHostFeature::step_active_gc(s) to collect 1 running plugin VM per 30 s active sweep (skipping dormant plugins and reading Tracked::untracked(&app.plugins)).
    • Preserved full all-VM collect_garbage(app) on App::on_rest and reset GC_CURSOR via reset_gc_cursor(s).
  • Plugin worker-thread idle heap collection (crates/opencode-plugin-runtime/src/tasks.rs, apps/desktop/src/lib.rs):
    • Replaced Mutex<Receiver<Job>> (which held the mutex across sleeping recv(), preventing sibling workers from draining/collecting before parking) with (Mutex<VecDeque<Job>>, Condvar) and an explicit WorkerState (Clean / Dirty) machine.
    • Added configure_on_idle(fn()) (wired from commit_pages_on_demand() to collect_thread_heap, keeping zero unsafe in opencode-plugin-runtime) invoked after the queue empties with the mutex released, right before parking on the condvar.
    • Kept mi_option_purge_delay = 250 ms, page_reclaim_on_free = 1, and page_commit_on_demand = 1 verified against vendor/libmimalloc-sys/c_src/mimalloc/v3/include/mimalloc.h (lines 455, 475, 480) and v3/src/options.c:149 (arena_purge_mult = 1).
  • Animation framerate unchanged: Spinners and shimmers remain at full native Hz.

Verification

  • pwsh scripts/guardrails.ps1: 12/12 passed (23.2s).
  • cargo check -p opencode-desktop --no-default-features --features knobs: exit 0, 0 warnings in touched files.
  • cargo check -p opencode-web --target wasm32-unknown-unknown: exit 0, 0 new warnings in touched files (2 pre-existing wasm32 unused warnings in untouched lines of apps/desktop/src/plugin_host/feature.rs).
  • bun scripts/web-clock.ts: 0 hits in touched files (1 pre-existing hit in untouched tab_profile.rs:5).
  • git diff --check: clean.
  • No automated tests added per AGENTS.md ("No tests").

File line counts (all <= 800)

  • apps/desktop/src/shell/memory_reclaim.rs: 672
  • apps/desktop/src/lib.rs: 657
  • apps/desktop/src/plugin_host/feature.rs: 388
  • apps/desktop/src/plugin_host/runtime/memory.rs: 348
  • crates/opencode-plugin-runtime/src/tasks.rs: 160

@Hona
Hona merged commit 346c930 into dev Dec 6, 2026
8 checks passed
@Hona
Hona deleted the perf/active-24x7-memory-reclaim branch December 6, 2026 05:43
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