From cb7eeedcf515457e3418be06d4351fa87e37f58a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 15 Apr 2026 04:44:02 +0300 Subject: [PATCH] pref: fix setstacksize test failure across bit widths (fixes #24394) --- vlib/v/gen/c/testdata/spawn_stack_nix.c.must_have | 1 + vlib/v/gen/c/testdata/spawn_stack_nix.vv | 1 + vlib/v/help/build/build-c.txt | 6 ++++-- vlib/v/pref/default.v | 10 ++++++++++ vlib/v/pref/pref.v | 4 +++- 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/vlib/v/gen/c/testdata/spawn_stack_nix.c.must_have b/vlib/v/gen/c/testdata/spawn_stack_nix.c.must_have index 6ac06ec68..f2fe79ed9 100644 --- a/vlib/v/gen/c/testdata/spawn_stack_nix.c.must_have +++ b/vlib/v/gen/c/testdata/spawn_stack_nix.c.must_have @@ -1,2 +1,3 @@ +pthread_attr_setstacksize(&thread__t1_attributes, 2097152); // fn: main.my_print1 pthread_attr_setstacksize(&thread__t3_attributes, 65536); // fn: main.my_print2 pthread_attr_setstacksize(&thread__t5_attributes, 131072); // fn: main.my_print3 diff --git a/vlib/v/gen/c/testdata/spawn_stack_nix.vv b/vlib/v/gen/c/testdata/spawn_stack_nix.vv index 3160898f4..5c9e99e6a 100644 --- a/vlib/v/gen/c/testdata/spawn_stack_nix.vv +++ b/vlib/v/gen/c/testdata/spawn_stack_nix.vv @@ -1,3 +1,4 @@ +// vtest vflags: -arch i386 module main fn main() { diff --git a/vlib/v/help/build/build-c.txt b/vlib/v/help/build/build-c.txt index 245d5099f..80048b868 100644 --- a/vlib/v/help/build/build-c.txt +++ b/vlib/v/help/build/build-c.txt @@ -382,8 +382,10 @@ see also `v help build`. -thread-stack-size 4194304 Set the thread stack size to 4MB. Use multiples of 4096. - The default is 8MB, which is enough for compiling V programs, with deeply - nested expressions (~40 levels). + The default is 8MB on 64-bit targets and 2MB on 32-bit targets. + 8MB is enough for compiling V programs, with deeply nested + expressions (~40 levels), while 32-bit targets use a smaller default + to reduce per-thread memory usage. It may need to be increased, if you are getting stack overflow errors for deeply recursive programs like some of the stages of the V compiler itself, that use relatively few threads. diff --git a/vlib/v/pref/default.v b/vlib/v/pref/default.v index aa6f8ef10..856510581 100644 --- a/vlib/v/pref/default.v +++ b/vlib/v/pref/default.v @@ -14,6 +14,13 @@ pub fn new_preferences() &Preferences { return p } +fn (p &Preferences) default_thread_stack_size() int { + return match p.arch { + .arm32, .rv32, .i386, .ppc, .wasm32 { 2 * 1024 * 1024 } + else { 8 * 1024 * 1024 } + } +} + fn (mut p Preferences) expand_lookup_paths() { if p.vroot == '' { // Location of all vlib files @@ -232,6 +239,9 @@ pub fn (mut p Preferences) fill_with_defaults() { } p.find_cc_if_cross_compiling() p.resolve_default_arch() + if !p.thread_stack_size_set_by_flag { + p.thread_stack_size = p.default_thread_stack_size() + } p.ccompiler_type = cc_from_string(p.ccompiler) p.disable_tcc_shared_backtraces() p.is_test = p.path.ends_with('_test.v') || p.path.ends_with('_test.vv') diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index a1f86588f..2b1b10104 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -255,7 +255,8 @@ pub mut: fast_math bool // -fast-math will pass either -ffast-math or /fp:fast (for msvc) to the C backend // checker settings: checker_match_exhaustive_cutoff_limit int = 12 - thread_stack_size int = 8388608 // Change with `-thread-stack-size 4194304`. Note: on macos it was 524288, which is too small for more complex programs with many nested callexprs. + thread_stack_size int = 8388608 // Change with `-thread-stack-size 4194304`. The final default is adjusted in fill_with_defaults() based on the target architecture. + thread_stack_size_set_by_flag bool // wasm settings: wasm_stack_top int = 1024 + (16 * 1024) // stack size for webassembly backend wasm_validate bool // validate webassembly code, by calling `wasm-validate` @@ -989,6 +990,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin '-thread-stack-size' { res.thread_stack_size = cmdline.option(args[i..], arg, res.thread_stack_size.str()).int() + res.thread_stack_size_set_by_flag = true i++ } '-cc' { -- 2.39.5