libs-team icon indicating copy to clipboard operation
libs-team copied to clipboard

ACP For `OnceLock::get_unchecked` and `LazyLock::get_unchecked`

Open HomelikeBrick42 opened this issue 1 year ago • 4 comments

Proposal

Problem statement

Currently there is no way to get a reference to the value in a OnceLock or LazyLock without there being an atomic initialization check on every access, even if you know it has already been initialized, which adds slight overhead to every access.

Motivating examples or use cases

This is one use case where I wanted this:

use derive_more::derive::{Debug, Display};
use lasso::{Spur, ThreadedRodeo};
use rustc_hash::FxBuildHasher;
use std::sync::OnceLock;

#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, Hash)]
#[display("{}", self.as_str())]
#[debug("{:?}", self.as_str())]
pub struct InternedStr(Spur);

impl InternedStr {
    pub fn intern(s: &str) -> Self {
        InternedStr(
            INTERNER
                .get_or_init(|| ThreadedRodeo::with_hasher(FxBuildHasher))
                .get_or_intern(s),
        )
    }

    pub fn as_str(self) -> &'static str {
        // SAFETY: if `self` has been constructed, then `INTERNER` has already been initialized by `Self::intern`
        // even though this uses `unwrap_unchecked`, the compiler still needs to atomically load the
        // initialization flag inside of `get` because the atomic ordering could matter, but it does not here
        let interner = unsafe { INTERNER.get().unwrap_unchecked() };

        // SAFETY: if `self` has been constructed, then `self.0` was retrieved from `INTERNER` so it will be resolved
        unsafe { interner.try_resolve(&self.0).unwrap_unchecked() }
    }
}

static INTERNER: OnceLock<ThreadedRodeo<Spur, FxBuildHasher>> = OnceLock::new();

Solution sketch

The solution would be having OnceLock::get_unchecked/LazyLock::get_unchecked so that the atomic initialization flag doesnt have to be read at all.

They would have these signatures:

impl<T, F> LazyLock<T, F> {
    pub unsafe fn get_unchecked(this: &Self) -> &T;
    pub unsafe fn get_unchecked_mut(this: &mut Self) -> &mut T;
}
impl<T> OnceLock<T> {
    pub unsafe fn get_unchecked(&self) -> &T;
    pub unsafe fn get_unchecked_mut(&mut self) -> &mut T;
}

Alternatives

There are no other alternatives.

HomelikeBrick42 avatar Sep 14 '25 05:09 HomelikeBrick42

You should clarify in the ACP about the signature of these functions... Assuming rust-lang/rust#138914 is the current proposal:

impl<T, F> LazyLock<T, F> {
    pub unsafe fn get_unchecked(this: &Self) -> &T;
    pub unsafe fn get_unchecked_mut(this: &mut Self) -> &mut T;
}
impl<T> OnceLock<T> {
    pub unsafe fn get_unchecked(&self) -> &T;
    pub unsafe fn get_unchecked_mut(&mut self) -> &mut T;
}

kennytm avatar Sep 15 '25 03:09 kennytm

updated

HomelikeBrick42 avatar Sep 15 '25 03:09 HomelikeBrick42

also something that could be added, maybe the *Cell versions of the types should have these methods too? though it doesnt add much advantage because the compiler should be able to optimize the branch away when using Option::unwrap_unchecked on their return values, since there are no atomics involved

HomelikeBrick42 avatar Sep 15 '25 03:09 HomelikeBrick42

These seem great!

Please open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. You can close this ACP once the tracking issue has been created.

Amanieu avatar Sep 16 '25 18:09 Amanieu