Repository navigation
perf(memory): add 24/7 active mimalloc purge and staggered plugin GC - #2558
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Returns unused
mimallocarena pages to the OS and runs staggered single-VM PsychoScript JS GC during continuous 24/7 active streaming, whenApp::on_rest(10 s with zero frames) never fires.Live evidence (
opencode-desktop.exev0.0.0-dev-543,rec_7e009c)QueryWorkingSetExPTE scan measured409.5 MBcommit /369.7 MBresident inmimalloc1 GiB arenas against~186 MBHeap live(~184 MBresident /~224 MBcommit unpurged,2.0xlive heap).t = 166 -> 170 s,Heap livespiked+22.1 MBand dropped-23.2 MB, while RSS increased+1.9 MB(0 MBreturned to Windows):lastRestReclaimstayednullfor 7 minutes, the 30 s active sweep only calledmi_collect(false)when entries were removed, plugin JS GC ran only onon_rest, and parkedplugin-worker-*threads kept thread-local heaps uncollected.Changes (explicit state machines, zero new
Appfields)mimallocpurge (apps/desktop/src/shell/memory_reclaim.rs):PurgeTracker(Unprimed/Tracking { peak_live, soft_sweeps }/Uncounted { soft_sweeps }) insideMemorySweep::Scheduled(automatically cleared onRested, zero edits tokernel.rs).procwatch::alloc::live_bytes()peaks via two relaxed atomic loads with1 MiBquantization hysteresis so transient spikes between 30 s ticks (such as+22.1 MB -> -23.2 MBin 4 s) are captured without touching theSWEEPslot on small allocations.purge_free_pages_soft()(mi_collect(false)) unconditionally when a forced purge is not due, orcollect_thread_heap()(mi_collect(true)— visits full pages and purges all free arena slices back to the OS) when live heap dropped>= 8 MiBfrom 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.procwatch::usage()only whenneeds_os_retainedis true (skipped on sweeps 1, 4, and drop-triggered sweeps).PurgeTrackerviaSweepEvent::ExternalPurgeonon_tab_closedafter its immediatepurge_free_pages().HeapCompact(GetProcessHeap(), 0)strictly onApp::on_restandon_tab_closedto 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).apps/desktop/src/plugin_host/runtime/memory.rs,apps/desktop/src/plugin_host/feature.rs):GcCursor(Idle/WindowHalf { index }/AppHalf { index }) slotGC_CURSORregistered inPluginHostFeature::register.collect_garbage_one(app)/PluginHostFeature::step_active_gc(s)to collect 1 running plugin VM per 30 s active sweep (skipping dormant plugins and readingTracked::untracked(&app.plugins)).collect_garbage(app)onApp::on_restand resetGC_CURSORviareset_gc_cursor(s).crates/opencode-plugin-runtime/src/tasks.rs,apps/desktop/src/lib.rs):Mutex<Receiver<Job>>(which held the mutex across sleepingrecv(), preventing sibling workers from draining/collecting before parking) with(Mutex<VecDeque<Job>>, Condvar)and an explicitWorkerState(Clean/Dirty) machine.configure_on_idle(fn())(wired fromcommit_pages_on_demand()tocollect_thread_heap, keeping zerounsafeinopencode-plugin-runtime) invoked after the queue empties with the mutex released, right before parking on the condvar.mi_option_purge_delay = 250ms,page_reclaim_on_free = 1, andpage_commit_on_demand = 1verified againstvendor/libmimalloc-sys/c_src/mimalloc/v3/include/mimalloc.h(lines 455, 475, 480) andv3/src/options.c:149(arena_purge_mult = 1).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-existingwasm32unused warnings in untouched lines ofapps/desktop/src/plugin_host/feature.rs).bun scripts/web-clock.ts: 0 hits in touched files (1 pre-existing hit in untouchedtab_profile.rs:5).git diff --check: clean.AGENTS.md("No tests").File line counts (all
<= 800)apps/desktop/src/shell/memory_reclaim.rs: 672apps/desktop/src/lib.rs: 657apps/desktop/src/plugin_host/feature.rs: 388apps/desktop/src/plugin_host/runtime/memory.rs: 348crates/opencode-plugin-runtime/src/tasks.rs: 160