Don't inline OnceCell initialization closures

This commit is contained in:
The8472 2021-09-17 00:24:36 +02:00
parent 237bb5e008
commit ca2d2fa283

View File

@ -214,7 +214,16 @@ impl<T> OnceCell<T> {
if let Some(val) = self.get() { if let Some(val) = self.get() {
return Ok(val); 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 // Note that *some* forms of reentrant initialization might lead to
// UB (see `reentrant_init` test). I believe that just removing this // UB (see `reentrant_init` test). I believe that just removing this
// `assert`, while keeping `set/get` would be sound, but it seems // `assert`, while keeping `set/get` would be sound, but it seems