about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-09-19 08:05:45 +0000
committerbors <bors@rust-lang.org>2021-09-19 08:05:45 +0000
commit7a3d1a5f3dfeaf5177885fedd7db8ecc70670dc1 (patch)
tree1aa67a879222565c488b3043dd72a0f79c7cdbea
parent3bca7230ff1ec35db25f2547cf2e83a6f450e923 (diff)
parentca2d2fa28391fec6e022cede26b400c85afb8c0c (diff)
downloadrust-7a3d1a5f3dfeaf5177885fedd7db8ecc70670dc1.tar.gz
rust-7a3d1a5f3dfeaf5177885fedd7db8ecc70670dc1.zip
Auto merge of #89031 - the8472:outline-once-cell-init-closure, r=Mark-Simulacrum
Don't inline OnceCell initialization closures

The more general variant of #89026, originally suggested in https://github.com/rust-lang/rust/pull/86898#issuecomment-920138051
-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