From 4dbc182f4c41053605ad73e830b63c2a918e2f0a Mon Sep 17 00:00:00 2001 From: fxfxfx123 <93531292+fxfxfx123@users.noreply.github.com> Date: Sat, 4 Jul 2026 11:51:30 +0800 Subject: [PATCH 1/3] fix(mem): enable arena purging and lower RAM fraction - Set mi_option_arena_purge_mult=1 (default 10) so arenas are purged aggressively without extra delay - Set mi_option_page_reclaim_on_free=1 to reclaim pages from exited worker thread heaps - Lower DEFAULT_RAM_FRACTION from 0.5 to 0.25 to reduce memory budget Signed-off-by: fxfxfx123 <93531292+fxfxfx123@users.noreply.github.com> --- src/foundation/mem.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/foundation/mem.c b/src/foundation/mem.c index 46494aad2..bb8c08843 100644 --- a/src/foundation/mem.c +++ b/src/foundation/mem.c @@ -13,7 +13,7 @@ #include "foundation/constants.h" #define MAX_RAM_FRACTION 1.0 -#define DEFAULT_RAM_FRACTION 0.5 +#define DEFAULT_RAM_FRACTION 0.25 #include #include #include @@ -122,6 +122,8 @@ void cbm_mem_init(double ram_fraction) { mi_option_set(mi_option_arena_eager_commit, 0); mi_option_set(mi_option_purge_decommits, SKIP_ONE); mi_option_set(mi_option_purge_delay, 0); /* immediate purge, no 1s delay */ + mi_option_set(mi_option_arena_purge_mult, 1); /* purge arenas aggressively, not 10x delay */ + mi_option_set(mi_option_page_reclaim_on_free, 1); /* reclaim pages from abandoned worker threads */ /* CBM_MEM_BUDGET_MB env override (memory analogue of CBM_WORKERS). * Lets users cap the budget directly without an enclosing cgroup — From 1aa82f9d8724de5983bfc1099d5967a2db243ee3 Mon Sep 17 00:00:00 2001 From: fxfxfx123 <93531292+fxfxfx123@users.noreply.github.com> Date: Sat, 4 Jul 2026 11:51:53 +0800 Subject: [PATCH 2/3] fix(system_info): cap initial worker count at 8 Memory scales linearly with worker count (each gets its own mimalloc arena + 8MB stack). Diminishing returns past 8 workers. On a 20-core CPU this reduces peak memory by up to 60% with negligible speed loss. Signed-off-by: fxfxfx123 <93531292+fxfxfx123@users.noreply.github.com> --- src/foundation/system_info.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/foundation/system_info.c b/src/foundation/system_info.c index 21e0a9cb3..5ab545183 100644 --- a/src/foundation/system_info.c +++ b/src/foundation/system_info.c @@ -298,7 +298,8 @@ int cbm_default_worker_count(bool initial) { cbm_system_info_t info = cbm_system_info(); if (initial) { /* Use all cores for initial indexing — user is waiting */ - return info.total_cores; + int cap = info.total_cores > 8 ? 8 : info.total_cores; + return cap; } /* Incremental: leave headroom for user's apps */ int workers = info.perf_cores - SKIP_ONE; From c0fee699b3e769fce5290760a91b88d0cac39020 Mon Sep 17 00:00:00 2001 From: fxfxfx123 <93531292+fxfxfx123@users.noreply.github.com> Date: Sat, 4 Jul 2026 12:01:12 +0800 Subject: [PATCH 3/3] style(mem): fix clang-format violations Move inline trailing comments to preceding lines to match project style and satisfy clang-format-20. Signed-off-by: fxfxfx123 <93531292+fxfxfx123@users.noreply.github.com> --- src/foundation/mem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/foundation/mem.c b/src/foundation/mem.c index bb8c08843..d1202e06d 100644 --- a/src/foundation/mem.c +++ b/src/foundation/mem.c @@ -122,8 +122,10 @@ void cbm_mem_init(double ram_fraction) { mi_option_set(mi_option_arena_eager_commit, 0); mi_option_set(mi_option_purge_decommits, SKIP_ONE); mi_option_set(mi_option_purge_delay, 0); /* immediate purge, no 1s delay */ - mi_option_set(mi_option_arena_purge_mult, 1); /* purge arenas aggressively, not 10x delay */ - mi_option_set(mi_option_page_reclaim_on_free, 1); /* reclaim pages from abandoned worker threads */ + /* Purge arenas aggressively (default multiplier is 10x purge_delay). */ + mi_option_set(mi_option_arena_purge_mult, 1); + /* Reclaim pages from exited worker thread heaps on free. */ + mi_option_set(mi_option_page_reclaim_on_free, 1); /* CBM_MEM_BUDGET_MB env override (memory analogue of CBM_WORKERS). * Lets users cap the budget directly without an enclosing cgroup —