Skip to content

Commit 285fa48

Browse files
[naga]: Switch off of LazyLock to once_cell::racy::OnceBox (#7587)
* Switch off of `LazyLock` to a custom `RacyLock` * Formatting * Switch to `OnceBox` internally * Clippy * Simplify `trunc` usage Co-Authored-By: Connor Fitzgerald <[email protected]> * Switch to `expect` Co-Authored-By: Connor Fitzgerald <[email protected]> * Fix expectation conditions Co-Authored-By: Connor Fitzgerald <[email protected]> * Update documentation --------- Co-authored-by: Connor Fitzgerald <[email protected]>
1 parent 0d06284 commit 285fa48

File tree

9 files changed

+58
-24
lines changed

9 files changed

+58
-24
lines changed

naga/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ fn main() {
66
msl_out: { any(feature = "msl-out", all(target_vendor = "apple", feature = "msl-out-if-target-apple")) },
77
spv_out: { feature = "spv-out" },
88
wgsl_out: { feature = "wgsl-out" },
9+
std: { any(test, spv_out, feature = "spv-in", feature = "wgsl-in", feature = "stderr") },
10+
no_std: { not(std) },
911
}
1012
}

naga/src/back/glsl/keywords.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::LazyLock;
1+
use crate::racy_lock::RacyLock;
22

33
use hashbrown::HashSet;
44

@@ -499,7 +499,7 @@ pub const RESERVED_KEYWORDS: &[&str] = &[
499499
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
500500
///
501501
/// See <https://github.com/gfx-rs/wgpu/pull/7338> for benchmarks.
502-
pub static RESERVED_KEYWORD_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
502+
pub static RESERVED_KEYWORD_SET: RacyLock<HashSet<&'static str>> = RacyLock::new(|| {
503503
let mut set = HashSet::default();
504504
set.reserve(RESERVED_KEYWORDS.len());
505505
for &word in RESERVED_KEYWORDS {

naga/src/back/hlsl/keywords.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::LazyLock;
1+
use crate::racy_lock::RacyLock;
22

33
use hashbrown::HashSet;
44

@@ -924,7 +924,7 @@ pub const TYPES: &[&str] = &{
924924
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
925925
///
926926
/// See <https://github.com/gfx-rs/wgpu/pull/7338> for benchmarks.
927-
pub static RESERVED_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
927+
pub static RESERVED_SET: RacyLock<HashSet<&'static str>> = RacyLock::new(|| {
928928
let mut set = HashSet::default();
929929
set.reserve(RESERVED.len() + TYPES.len());
930930
for &word in RESERVED {

naga/src/back/msl/keywords.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::LazyLock;
1+
use crate::racy_lock::RacyLock;
22

33
use hashbrown::HashSet;
44

@@ -360,7 +360,7 @@ pub const RESERVED: &[&str] = &[
360360
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
361361
///
362362
/// See <https://github.com/gfx-rs/wgpu/pull/7338> for benchmarks.
363-
pub static RESERVED_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
363+
pub static RESERVED_SET: RacyLock<HashSet<&'static str>> = RacyLock::new(|| {
364364
let mut set = HashSet::default();
365365
set.reserve(RESERVED.len());
366366
for &word in RESERVED {

naga/src/back/pipeline_constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ use crate::{
1616
Span, Statement, TypeInner, WithSpan,
1717
};
1818

19+
#[cfg(no_std)]
20+
use num_traits::float::FloatCore as _;
21+
1922
#[derive(Error, Debug, Clone)]
2023
#[cfg_attr(test, derive(PartialEq))]
2124
pub enum PipelineConstantError {

naga/src/common/wgsl/to_wgsl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use alloc::string::{String, ToString};
1414
///
1515
/// - If a type's WGSL form requires dynamic formatting, so that
1616
/// returning a `&'static str` isn't feasible, consider implementing
17-
/// [`std::fmt::Display`] on some wrapper type instead.
17+
/// [`core::fmt::Display`] on some wrapper type instead.
1818
pub trait ToWgsl: Sized {
1919
/// Return WGSL source code representation of `self`.
2020
fn to_wgsl(self) -> &'static str;
@@ -32,7 +32,7 @@ pub trait ToWgsl: Sized {
3232
///
3333
/// - If a type's WGSL form requires dynamic formatting, so that
3434
/// returning a `&'static str` isn't feasible, consider implementing
35-
/// [`std::fmt::Display`] on some wrapper type instead.
35+
/// [`core::fmt::Display`] on some wrapper type instead.
3636
pub trait TryToWgsl: Sized {
3737
/// Return the WGSL form of `self` as a `'static` string.
3838
///

naga/src/keywords/wgsl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Keywords for [WGSL][wgsl] (WebGPU Shading Language).
44
[wgsl]: https://gpuweb.github.io/gpuweb/wgsl.html
55
*/
66

7-
use std::sync::LazyLock;
7+
use crate::racy_lock::RacyLock;
88

99
use hashbrown::HashSet;
1010

@@ -238,7 +238,7 @@ pub const RESERVED: &[&str] = &[
238238
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
239239
///
240240
/// See <https://github.com/gfx-rs/wgpu/pull/7338> for benchmarks.
241-
pub static RESERVED_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
241+
pub static RESERVED_SET: RacyLock<HashSet<&'static str>> = RacyLock::new(|| {
242242
let mut set = HashSet::default();
243243
set.reserve(RESERVED.len());
244244
for &word in RESERVED {

naga/src/lib.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,7 @@ void main() {
100100
)]
101101
#![no_std]
102102

103-
#[cfg(any(
104-
test,
105-
spv_out,
106-
107-
// Need OnceLock
108-
hlsl_out,
109-
msl_out,
110-
wgsl_out,
111-
112-
feature = "spv-in",
113-
feature = "wgsl-in",
114-
115-
feature = "stderr",
116-
))]
103+
#[cfg(std)]
117104
extern crate std;
118105

119106
extern crate alloc;
@@ -130,6 +117,7 @@ pub mod ir;
130117
pub mod keywords;
131118
mod non_max_u32;
132119
pub mod proc;
120+
mod racy_lock;
133121
mod span;
134122
pub mod valid;
135123

naga/src/racy_lock.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#![cfg_attr(
2+
not(any(glsl_out, hlsl_out, msl_out, feature = "wgsl-in", wgsl_out)),
3+
expect(
4+
dead_code,
5+
reason = "RacyLock is only required for the above configurations"
6+
)
7+
)]
8+
9+
use alloc::boxed::Box;
10+
use once_cell::race::OnceBox;
11+
12+
/// An alternative to [`LazyLock`] based on [`OnceBox`].
13+
///
14+
/// [`LazyLock`]: https://doc.rust-lang.org/stable/std/sync/struct.LazyLock.html
15+
pub struct RacyLock<T: 'static> {
16+
inner: OnceBox<T>,
17+
init: fn() -> T,
18+
}
19+
20+
impl<T: 'static> RacyLock<T> {
21+
/// Creates a new [`RacyLock`], which will initialize using the provided `init` function.
22+
pub const fn new(init: fn() -> T) -> Self {
23+
Self {
24+
inner: OnceBox::new(),
25+
init,
26+
}
27+
}
28+
29+
/// Loads the internal value, initializing it if required.
30+
pub fn get(&self) -> &T {
31+
self.inner.get_or_init(|| Box::new((self.init)()))
32+
}
33+
}
34+
35+
impl<T: 'static> core::ops::Deref for RacyLock<T> {
36+
type Target = T;
37+
38+
fn deref(&self) -> &Self::Target {
39+
self.get()
40+
}
41+
}

0 commit comments

Comments
 (0)