@@ -48,12 +48,20 @@ pub fn add_configuration(cfg: &mut Cfg, sess: &mut Session, codegen_backend: &dy
48
48
}
49
49
}
50
50
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
+ } )
57
65
}
58
66
59
67
pub ( 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>(
66
74
// the parallel compiler, in particular to ensure there is no accidental
67
75
// sharing of data between the main thread and the compilation thread
68
76
// (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 ( ) ) ;
73
78
74
79
// We build the session globals and run `f` on the spawned thread, because
75
80
// `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>(
120
125
} ) ;
121
126
}
122
127
123
- let mut builder = rayon:: ThreadPoolBuilder :: new ( )
128
+ let builder = rayon:: ThreadPoolBuilder :: new ( )
124
129
. thread_name ( |_| "rustc" . to_string ( ) )
125
130
. acquire_thread_handler ( jobserver:: acquire_thread)
126
131
. 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>(
144
149
on_panic. disable ( ) ;
145
150
} )
146
151
. unwrap ( ) ;
147
- } ) ;
148
- if let Some ( size) = get_stack_size ( ) {
149
- builder = builder. stack_size ( size) ;
150
- }
152
+ } )
153
+ . stack_size ( init_stack_size ( ) ) ;
151
154
152
155
// We create the session globals on the main thread, then create the thread
153
156
// pool. Upon creation, each worker thread created gets a copy of the
0 commit comments