about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--library/core/src/lazy.rs11
1 files changed, 10 insertions, 1 deletions
diff --git a/library/core/src/lazy.rs b/library/core/src/lazy.rs
index 2c517371c2c..e6bea462fa9 100644
--- a/library/core/src/lazy.rs
+++ b/library/core/src/lazy.rs
@@ -214,7 +214,16 @@ impl<T> OnceCell<T> {
         if let Some(val) = self.get() {
             return Ok(val);
         }
-        let val = f()?;
+        /// Avoid inlining the initialization closure into the common path that fetches
+        /// the already initialized value
+        #[cold]
+        fn outlined_call<F, T, E>(f: F) -> Result<T, E>
+        where
+            F: FnOnce() -> Result<T, E>,
+        {
+            f()
+        }
+        let val = outlined_call(f)?;
         // Note that *some* forms of reentrant initialization might lead to
         // UB (see `reentrant_init` test). I believe that just removing this
         // `assert`, while keeping `set/get` would be sound, but it seems