@@ -300,7 +300,36 @@ void cbm_mem_init_with_cap(double ram_fraction, size_t hard_cap_bytes) {
300300 }
301301#endif
302302
303+ /* Lazy arena commit costs address-space FRAGMENTATION, and on Linux we now
304+ * pay it for every allocation in the process. mimalloc commits a range with
305+ * mprotect(PROT_READ|PROT_WRITE) over a sub-range of a PROT_NONE reservation
306+ * (prim/unix/prim.c), and each partial commit SPLITS the reserved VMA. While
307+ * mimalloc only served the bound populations (sqlite, tree_sitter) that was a
308+ * handful of mappings. Since #1360 routed ordinary malloc/new through
309+ * mimalloc on Linux, an index worker peaked at ~22k mappings against
310+ * v0.9.0's 10 (Go corpus, 18 workers). The count tracks CONCURRENCY, not
311+ * corpus size — sampled mid-run it was 999 at 1 worker, 8460 at 4 and 11965
312+ * at 18, while v0.9.0 stayed at 10 regardless — so every worker thread
313+ * fragments the address space independently.
314+ *
315+ * Two consequences, both reported as #1654 on a 96-CPU/376 GB host: the
316+ * mmap/mprotect churn serialises on the kernel's per-process mmap_lock (the
317+ * reporter saw 1.2% of extraction in 45 minutes, where v0.9.0 finished the
318+ * tree in ~13), and the VMA count climbs toward vm.max_map_count, after
319+ * which mmap fails for ANY size — hence mimalloc reporting it "cannot
320+ * allocate" 10 KB while `free -g` still showed 246 GB available.
321+ *
322+ * mimalloc's own default is 2, meaning "eager-commit arenas only on an OS
323+ * with overcommit (i.e. linux)" — precisely because commit is free there
324+ * until the pages are touched. Overriding it to 0 opted Linux out of the
325+ * default written for it. Restore the default on Linux; keep the lazy
326+ * setting everywhere else, where commit is NOT free and the upfront-memory
327+ * reason for it still holds (Windows especially — see #581). */
328+ #if defined(__linux__ )
329+ mem_option_set_verified (mi_option_arena_eager_commit , 2 , "arena_eager_commit" );
330+ #else
303331 mem_option_set_verified (mi_option_arena_eager_commit , 0 , "arena_eager_commit" );
332+ #endif
304333 mem_option_set_verified (mi_option_purge_decommits , SKIP_ONE , "purge_decommits" );
305334 mem_option_set_verified (mi_option_purge_delay , 0 , "purge_delay" ); /* immediate */
306335 /* v3 (#832): reclaim abandoned pages on ANY thread's free (=1), restoring the
0 commit comments