We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 3bca723 + ca2d2fa commit 7a3d1a5Copy full SHA for 7a3d1a5
library/core/src/lazy.rs
@@ -214,7 +214,16 @@ impl<T> OnceCell<T> {
214
if let Some(val) = self.get() {
215
return Ok(val);
216
}
217
- let val = f()?;
+ /// Avoid inlining the initialization closure into the common path that fetches
218
+ /// the already initialized value
219
+ #[cold]
220
+ fn outlined_call<F, T, E>(f: F) -> Result<T, E>
221
+ where
222
+ F: FnOnce() -> Result<T, E>,
223
+ {
224
+ f()
225
+ }
226
+ let val = outlined_call(f)?;
227
// Note that *some* forms of reentrant initialization might lead to
228
// UB (see `reentrant_init` test). I believe that just removing this
229
// `assert`, while keeping `set/get` would be sound, but it seems
0 commit comments