From 9da21771eb11287d455f40a98c5b24e97c48a0f9 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 11 Mar 2026 12:35:48 +0300 Subject: [PATCH] cgen: the gc makes the apps run slow at first (fixes #24827) --- vlib/v/gen/c/cmain.v | 78 ++++++++++++------- .../boehm_default_startup.c.must_have | 2 + .../v/gen/c/testdata/boehm_default_startup.vv | 2 + 3 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 vlib/v/gen/c/testdata/boehm_default_startup.c.must_have create mode 100644 vlib/v/gen/c/testdata/boehm_default_startup.vv diff --git a/vlib/v/gen/c/cmain.v b/vlib/v/gen/c/cmain.v index f162d84c6..1c0caf9ee 100644 --- a/vlib/v/gen/c/cmain.v +++ b/vlib/v/gen/c/cmain.v @@ -148,32 +148,26 @@ fn (mut g Gen) gen_c_main_function_header() { } } -fn (mut g Gen) gen_boehm_gc_init() { - g.writeln('#if defined(_VGCBOEHM)') - if g.pref.gc_mode == .boehm_leak { - g.writeln('\tGC_set_find_leak(1);') - } - if g.pref.os == .linux && !g.pref.no_builtin { - g.writeln('\tbool __v_gc_debugger_workaround = builtin__gc_prepare_for_debugger_init();') - } - g.writeln('\tGC_set_pages_executable(0);') - if g.pref.use_coroutines { - g.writeln('\tGC_allow_register_threads();') - } - g.writeln('\tGC_INIT();') - if g.pref.os == .linux && !g.pref.no_builtin { - g.writeln('\tbuiltin__gc_restore_roots_after_debugger_init(__v_gc_debugger_workaround);') - } - if g.pref.gc_mode in [.boehm_incr, .boehm_incr_opt] { - g.writeln('\tGC_enable_incremental();') - } - g.writeln('#endif') -} - fn (mut g Gen) gen_c_main_header() { g.gen_c_main_function_header() if g.pref.gc_mode in [.boehm_full, .boehm_incr, .boehm_full_opt, .boehm_incr_opt, .boehm_leak] { - g.gen_boehm_gc_init() + g.writeln('#if defined(_VGCBOEHM)') + if g.pref.gc_mode == .boehm_leak { + g.writeln('\tGC_set_find_leak(1);') + } + g.writeln('\tGC_set_pages_executable(0);') + if g.pref.gc_mode in [.boehm_full_opt, .boehm_incr_opt] { + g.writeln('\tGC_set_free_space_divisor(2);') + } + if g.pref.use_coroutines { + g.writeln('\tGC_allow_register_threads();') + } + g.writeln('\tGC_INIT();') + + if g.pref.gc_mode in [.boehm_incr, .boehm_incr_opt] { + g.writeln('\tGC_enable_incremental();') + } + g.writeln('#endif') } if g.pref.gc_mode == .vgc { g.writeln('\tbuiltin__vgc_init();') @@ -222,7 +216,19 @@ sapp_desc sokol_main(int argc, char* argv[]) { g.gen_c_main_trace_calls_hook() if g.pref.gc_mode in [.boehm_full, .boehm_incr, .boehm_full_opt, .boehm_incr_opt, .boehm_leak] { - g.gen_boehm_gc_init() + g.writeln('#if defined(_VGCBOEHM)') + if g.pref.gc_mode == .boehm_leak { + g.writeln('\tGC_set_find_leak(1);') + } + g.writeln('\tGC_set_pages_executable(0);') + if g.pref.gc_mode in [.boehm_full_opt, .boehm_incr_opt] { + g.writeln('\tGC_set_free_space_divisor(2);') + } + g.writeln('\tGC_INIT();') + if g.pref.gc_mode in [.boehm_incr, .boehm_incr_opt] { + g.writeln('\tGC_enable_incremental();') + } + g.writeln('#endif') } if g.pref.gc_mode == .vgc { g.writeln('\tbuiltin__vgc_init();') @@ -299,7 +305,22 @@ pub fn (mut g Gen) gen_c_main_for_tests() { g.writeln('') g.gen_c_main_function_header() if g.pref.gc_mode in [.boehm_full, .boehm_incr, .boehm_full_opt, .boehm_incr_opt, .boehm_leak] { - g.gen_boehm_gc_init() + g.writeln('#if defined(_VGCBOEHM)') + if g.pref.gc_mode == .boehm_leak { + g.writeln('\tGC_set_find_leak(1);') + } + g.writeln('\tGC_set_pages_executable(0);') + if g.pref.gc_mode in [.boehm_full_opt, .boehm_incr_opt] { + g.writeln('\tGC_set_free_space_divisor(2);') + } + if g.pref.use_coroutines { + g.writeln('\tGC_allow_register_threads();') + } + g.writeln('\tGC_INIT();') + if g.pref.gc_mode in [.boehm_incr, .boehm_incr_opt] { + g.writeln('\tGC_enable_incremental();') + } + g.writeln('#endif') } if g.pref.gc_mode == .vgc { g.writeln('\tbuiltin__vgc_init();') @@ -398,12 +419,17 @@ pub fn (mut g Gen) gen_c_main_trace_calls_hook() { // gen_dll_main create DllMain() for windows .dll. pub fn (mut g Gen) gen_dll_main() { + opt_gc_setup := if g.pref.gc_mode in [.boehm_full_opt, .boehm_incr_opt] { + '\t\t\tGC_set_free_space_divisor(2);\n' + } else { + '' + } g.writeln('VV_EXP BOOL DllMain(HINSTANCE hinst,DWORD fdwReason,LPVOID lpvReserved) { switch (fdwReason) { case DLL_PROCESS_ATTACH : { #if defined(_VGCBOEHM) GC_set_pages_executable(0); - GC_INIT(); +${opt_gc_setup} GC_INIT(); #endif _vinit_caller(); break; diff --git a/vlib/v/gen/c/testdata/boehm_default_startup.c.must_have b/vlib/v/gen/c/testdata/boehm_default_startup.c.must_have new file mode 100644 index 000000000..1122b4253 --- /dev/null +++ b/vlib/v/gen/c/testdata/boehm_default_startup.c.must_have @@ -0,0 +1,2 @@ +GC_set_free_space_divisor(2); +GC_INIT(); diff --git a/vlib/v/gen/c/testdata/boehm_default_startup.vv b/vlib/v/gen/c/testdata/boehm_default_startup.vv new file mode 100644 index 000000000..9f25395d5 --- /dev/null +++ b/vlib/v/gen/c/testdata/boehm_default_startup.vv @@ -0,0 +1,2 @@ +// vtest vflags: -gc boehm +fn main() {} -- 2.39.5