Skip to content

[naga]: Switch off of LazyLock to once_cell::racy::OnceBox #7587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions naga/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ fn main() {
msl_out: { any(feature = "msl-out", all(target_vendor = "apple", feature = "msl-out-if-target-apple")) },
spv_out: { feature = "spv-out" },
wgsl_out: { feature = "wgsl-out" },
std: { any(test, spv_out, feature = "spv-in", feature = "wgsl-in", feature = "stderr") },
no_std: { not(std) },
}
}
4 changes: 2 additions & 2 deletions naga/src/back/glsl/keywords.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::LazyLock;
use crate::racy_lock::RacyLock;

use hashbrown::HashSet;

Expand Down Expand Up @@ -499,7 +499,7 @@ pub const RESERVED_KEYWORDS: &[&str] = &[
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
///
/// See <https://github.com/gfx-rs/wgpu/pull/7338> for benchmarks.
pub static RESERVED_KEYWORD_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
pub static RESERVED_KEYWORD_SET: RacyLock<HashSet<&'static str>> = RacyLock::new(|| {
let mut set = HashSet::default();
set.reserve(RESERVED_KEYWORDS.len());
for &word in RESERVED_KEYWORDS {
Expand Down
4 changes: 2 additions & 2 deletions naga/src/back/hlsl/keywords.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::LazyLock;
use crate::racy_lock::RacyLock;

use hashbrown::HashSet;

Expand Down Expand Up @@ -924,7 +924,7 @@ pub const TYPES: &[&str] = &{
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
///
/// See <https://github.com/gfx-rs/wgpu/pull/7338> for benchmarks.
pub static RESERVED_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
pub static RESERVED_SET: RacyLock<HashSet<&'static str>> = RacyLock::new(|| {
let mut set = HashSet::default();
set.reserve(RESERVED.len() + TYPES.len());
for &word in RESERVED {
Expand Down
4 changes: 2 additions & 2 deletions naga/src/back/msl/keywords.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::LazyLock;
use crate::racy_lock::RacyLock;

use hashbrown::HashSet;

Expand Down Expand Up @@ -360,7 +360,7 @@ pub const RESERVED: &[&str] = &[
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
///
/// See <https://github.com/gfx-rs/wgpu/pull/7338> for benchmarks.
pub static RESERVED_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
pub static RESERVED_SET: RacyLock<HashSet<&'static str>> = RacyLock::new(|| {
let mut set = HashSet::default();
set.reserve(RESERVED.len());
for &word in RESERVED {
Expand Down
3 changes: 3 additions & 0 deletions naga/src/back/pipeline_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use crate::{
Span, Statement, TypeInner, WithSpan,
};

#[cfg(no_std)]
use num_traits::float::FloatCore as _;

#[derive(Error, Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub enum PipelineConstantError {
Expand Down
4 changes: 2 additions & 2 deletions naga/src/common/wgsl/to_wgsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use alloc::string::{String, ToString};
///
/// - If a type's WGSL form requires dynamic formatting, so that
/// returning a `&'static str` isn't feasible, consider implementing
/// [`std::fmt::Display`] on some wrapper type instead.
/// [`core::fmt::Display`] on some wrapper type instead.
pub trait ToWgsl: Sized {
/// Return WGSL source code representation of `self`.
fn to_wgsl(self) -> &'static str;
Expand All @@ -32,7 +32,7 @@ pub trait ToWgsl: Sized {
///
/// - If a type's WGSL form requires dynamic formatting, so that
/// returning a `&'static str` isn't feasible, consider implementing
/// [`std::fmt::Display`] on some wrapper type instead.
/// [`core::fmt::Display`] on some wrapper type instead.
pub trait TryToWgsl: Sized {
/// Return the WGSL form of `self` as a `'static` string.
///
Expand Down
4 changes: 2 additions & 2 deletions naga/src/keywords/wgsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Keywords for [WGSL][wgsl] (WebGPU Shading Language).
[wgsl]: https://gpuweb.github.io/gpuweb/wgsl.html
*/

use std::sync::LazyLock;
use crate::racy_lock::RacyLock;

use hashbrown::HashSet;

Expand Down Expand Up @@ -238,7 +238,7 @@ pub const RESERVED: &[&str] = &[
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
///
/// See <https://github.com/gfx-rs/wgpu/pull/7338> for benchmarks.
pub static RESERVED_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
pub static RESERVED_SET: RacyLock<HashSet<&'static str>> = RacyLock::new(|| {
let mut set = HashSet::default();
set.reserve(RESERVED.len());
for &word in RESERVED {
Expand Down
16 changes: 2 additions & 14 deletions naga/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,7 @@ void main() {
)]
#![no_std]

#[cfg(any(
test,
spv_out,

// Need OnceLock
hlsl_out,
msl_out,
wgsl_out,

feature = "spv-in",
feature = "wgsl-in",

feature = "stderr",
))]
#[cfg(std)]
extern crate std;

extern crate alloc;
Expand All @@ -130,6 +117,7 @@ pub mod ir;
pub mod keywords;
mod non_max_u32;
pub mod proc;
mod racy_lock;
mod span;
pub mod valid;

Expand Down
41 changes: 41 additions & 0 deletions naga/src/racy_lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#![cfg_attr(
not(any(glsl_out, hlsl_out, msl_out, feature = "wgsl-in", wgsl_out)),
expect(
dead_code,
reason = "RacyLock is only required for the above configurations"
)
)]

use alloc::boxed::Box;
use once_cell::race::OnceBox;

/// An alternative to [`LazyLock`] based on [`OnceBox`].
///
/// [`LazyLock`]: https://doc.rust-lang.org/stable/std/sync/struct.LazyLock.html
pub struct RacyLock<T: 'static> {
inner: OnceBox<T>,
init: fn() -> T,
}

impl<T: 'static> RacyLock<T> {
/// Creates a new [`RacyLock`], which will initialize using the provided `init` function.
pub const fn new(init: fn() -> T) -> Self {
Self {
inner: OnceBox::new(),
init,
}
}

/// Loads the internal value, initializing it if required.
pub fn get(&self) -> &T {
self.inner.get_or_init(|| Box::new((self.init)()))
}
}

impl<T: 'static> core::ops::Deref for RacyLock<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
self.get()
}
}