@@ -48,12 +48,20 @@ pub fn add_configuration(cfg: &mut Cfg, sess: &mut Session, codegen_backend: &dy
4848 }
4949}
5050
51- const STACK_SIZE : usize = 8 * 1024 * 1024 ;
52-
53- fn get_stack_size ( ) -> Option < usize > {
54- // FIXME: Hacks on hacks. If the env is trying to override the stack size
55- // then *don't* set it explicitly.
56- env:: var_os ( "RUST_MIN_STACK" ) . is_none ( ) . then_some ( STACK_SIZE )
51+ static STACK_SIZE : OnceLock < usize > = OnceLock :: new ( ) ;
52+ const DEFAULT_STACK_SIZE : usize = 8 * 1024 * 1024 ;
53+
54+ fn init_stack_size ( ) -> usize {
55+ // Obey the environment setting or default
56+ * STACK_SIZE . get_or_init ( || {
57+ env:: var_os ( "RUST_MIN_STACK" )
58+ . map ( |os_str| os_str. to_string_lossy ( ) . into_owned ( ) )
59+ // ignore if it is set to nothing
60+ . filter ( |s| s. trim ( ) != "" )
61+ . map ( |s| s. trim ( ) . parse :: < usize > ( ) . unwrap ( ) )
62+ // otherwise pick a consistent default
63+ . unwrap_or ( DEFAULT_STACK_SIZE )
64+ } )
5765}
5866
5967pub ( crate ) fn run_in_thread_with_globals < F : FnOnce ( ) -> R + Send , R : Send > (
@@ -66,10 +74,7 @@ pub(crate) fn run_in_thread_with_globals<F: FnOnce() -> R + Send, R: Send>(
6674 // the parallel compiler, in particular to ensure there is no accidental
6775 // sharing of data between the main thread and the compilation thread
6876 // (which might cause problems for the parallel compiler).
69- let mut builder = thread:: Builder :: new ( ) . name ( "rustc" . to_string ( ) ) ;
70- if let Some ( size) = get_stack_size ( ) {
71- builder = builder. stack_size ( size) ;
72- }
77+ let builder = thread:: Builder :: new ( ) . name ( "rustc" . to_string ( ) ) . stack_size ( init_stack_size ( ) ) ;
7378
7479 // We build the session globals and run `f` on the spawned thread, because
7580 // `SessionGlobals` does not impl `Send` in the non-parallel compiler.
@@ -120,7 +125,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
120125 } ) ;
121126 }
122127
123- let mut builder = rayon:: ThreadPoolBuilder :: new ( )
128+ let builder = rayon:: ThreadPoolBuilder :: new ( )
124129 . thread_name ( |_| "rustc" . to_string ( ) )
125130 . acquire_thread_handler ( jobserver:: acquire_thread)
126131 . release_thread_handler ( jobserver:: release_thread)
@@ -144,10 +149,8 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
144149 on_panic. disable ( ) ;
145150 } )
146151 . unwrap ( ) ;
147- } ) ;
148- if let Some ( size) = get_stack_size ( ) {
149- builder = builder. stack_size ( size) ;
150- }
152+ } )
153+ . stack_size ( init_stack_size ( ) ) ;
151154
152155 // We create the session globals on the main thread, then create the thread
153156 // pool. Upon creation, each worker thread created gets a copy of the
0 commit comments