Skip to content

Commit

Permalink
Auto merge of #89031 - the8472:outline-once-cell-init-closure, r=Mark…
Browse files Browse the repository at this point in the history
…-Simulacrum

Don't inline OnceCell initialization closures

The more general variant of #89026, originally suggested in #86898 (comment)
  • Loading branch information
bors committed Sep 19, 2021
2 parents 3bca723 + ca2d2fa commit 7a3d1a5
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion library/core/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7a3d1a5

Please sign in to comment.