Skip to content

Commit 7a3d1a5

Browse files
committed
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 rust-lang/rust#86898 (comment)
2 parents 3bca723 + ca2d2fa commit 7a3d1a5

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Diff for: library/core/src/lazy.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,16 @@ impl<T> OnceCell<T> {
214214
if let Some(val) = self.get() {
215215
return Ok(val);
216216
}
217-
let val = f()?;
217+
/// 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)?;
218227
// Note that *some* forms of reentrant initialization might lead to
219228
// UB (see `reentrant_init` test). I believe that just removing this
220229
// `assert`, while keeping `set/get` would be sound, but it seems

0 commit comments

Comments
 (0)