refactor: s/once_cell::Lazy/std::sync::LazyLock

Weaken our dependence on the `once_cell` crate by using functionality
from `std` instead that was upstreamed from `once_cell`, this time with
what's available in Rust 1.80+.

It's not yet possible to eliminate this dependency entirely, but do what
we can for now.
This commit is contained in:
Erich Gubler 2024-10-10 14:28:42 -04:00
parent 8c9dd13097
commit af323622db
8 changed files with 22 additions and 26 deletions

2
Cargo.lock generated
View File

@ -3605,7 +3605,6 @@ dependencies = [
"criterion", "criterion",
"naga", "naga",
"nanorand", "nanorand",
"once_cell",
"pollster", "pollster",
"profiling", "profiling",
"rayon", "rayon",
@ -3702,7 +3701,6 @@ dependencies = [
"naga", "naga",
"ndk-sys", "ndk-sys",
"objc", "objc",
"once_cell",
"parking_lot", "parking_lot",
"profiling", "profiling",
"range-alloc", "range-alloc",

View File

@ -43,7 +43,6 @@ naga = { workspace = true, features = [
"wgsl-out", "wgsl-out",
] } ] }
nanorand.workspace = true nanorand.workspace = true
once_cell.workspace = true
pollster.workspace = true pollster.workspace = true
profiling.workspace = true profiling.workspace = true
rayon.workspace = true rayon.workspace = true

View File

@ -5,8 +5,8 @@ use std::{
use criterion::{criterion_group, Criterion, Throughput}; use criterion::{criterion_group, Criterion, Throughput};
use nanorand::{Rng, WyRand}; use nanorand::{Rng, WyRand};
use once_cell::sync::Lazy;
use rayon::iter::{IntoParallelIterator, ParallelIterator}; use rayon::iter::{IntoParallelIterator, ParallelIterator};
use std::sync::LazyLock;
use crate::DeviceState; use crate::DeviceState;
@ -424,7 +424,7 @@ impl ComputepassState {
} }
fn run_bench(ctx: &mut Criterion) { fn run_bench(ctx: &mut Criterion) {
let state = Lazy::new(ComputepassState::new); let state = LazyLock::new(ComputepassState::new);
let dispatch_count = dispatch_count(); let dispatch_count = dispatch_count();
let dispatch_count_bindless = dispatch_count_bindless(); let dispatch_count_bindless = dispatch_count_bindless();
@ -449,7 +449,7 @@ fn run_bench(ctx: &mut Criterion) {
group.bench_function( group.bench_function(
format!("{cpasses} computepasses x {dispatch_per_pass} dispatches ({label})"), format!("{cpasses} computepasses x {dispatch_per_pass} dispatches ({label})"),
|b| { |b| {
Lazy::force(&state); LazyLock::force(&state);
b.iter_custom(|iters| { b.iter_custom(|iters| {
profiling::scope!("benchmark invocation"); profiling::scope!("benchmark invocation");
@ -498,7 +498,7 @@ fn run_bench(ctx: &mut Criterion) {
group.bench_function( group.bench_function(
format!("{threads} threads x {dispatch_per_pass} dispatch"), format!("{threads} threads x {dispatch_per_pass} dispatch"),
|b| { |b| {
Lazy::force(&state); LazyLock::force(&state);
b.iter_custom(|iters| { b.iter_custom(|iters| {
profiling::scope!("benchmark invocation"); profiling::scope!("benchmark invocation");
@ -538,7 +538,7 @@ fn run_bench(ctx: &mut Criterion) {
group.throughput(Throughput::Elements(dispatch_count_bindless as _)); group.throughput(Throughput::Elements(dispatch_count_bindless as _));
group.bench_function(format!("{dispatch_count_bindless} dispatch"), |b| { group.bench_function(format!("{dispatch_count_bindless} dispatch"), |b| {
Lazy::force(&state); LazyLock::force(&state);
b.iter_custom(|iters| { b.iter_custom(|iters| {
profiling::scope!("benchmark invocation"); profiling::scope!("benchmark invocation");
@ -579,7 +579,7 @@ fn run_bench(ctx: &mut Criterion) {
texture_count + storage_texture_count + storage_buffer_count texture_count + storage_texture_count + storage_buffer_count
), ),
|b| { |b| {
Lazy::force(&state); LazyLock::force(&state);
b.iter(|| state.device_state.queue.submit([])); b.iter(|| state.device_state.queue.submit([]));
}, },

View File

@ -5,8 +5,8 @@ use std::{
use criterion::{criterion_group, Criterion, Throughput}; use criterion::{criterion_group, Criterion, Throughput};
use nanorand::{Rng, WyRand}; use nanorand::{Rng, WyRand};
use once_cell::sync::Lazy;
use rayon::iter::{IntoParallelIterator, ParallelIterator}; use rayon::iter::{IntoParallelIterator, ParallelIterator};
use std::sync::LazyLock;
use crate::DeviceState; use crate::DeviceState;
@ -427,7 +427,7 @@ impl RenderpassState {
} }
fn run_bench(ctx: &mut Criterion) { fn run_bench(ctx: &mut Criterion) {
let state = Lazy::new(RenderpassState::new); let state = LazyLock::new(RenderpassState::new);
let draw_count = draw_count(); let draw_count = draw_count();
let vertex_buffer_count = draw_count * VERTEX_BUFFERS_PER_DRAW; let vertex_buffer_count = draw_count * VERTEX_BUFFERS_PER_DRAW;
@ -450,7 +450,7 @@ fn run_bench(ctx: &mut Criterion) {
group.bench_function( group.bench_function(
format!("{rpasses} renderpasses x {draws_per_pass} draws ({label})"), format!("{rpasses} renderpasses x {draws_per_pass} draws ({label})"),
|b| { |b| {
Lazy::force(&state); LazyLock::force(&state);
b.iter_custom(|iters| { b.iter_custom(|iters| {
profiling::scope!("benchmark invocation"); profiling::scope!("benchmark invocation");
@ -502,7 +502,7 @@ fn run_bench(ctx: &mut Criterion) {
for threads in [2, 4, 8] { for threads in [2, 4, 8] {
let draws_per_pass = draw_count / threads; let draws_per_pass = draw_count / threads;
group.bench_function(format!("{threads} threads x {draws_per_pass} draws"), |b| { group.bench_function(format!("{threads} threads x {draws_per_pass} draws"), |b| {
Lazy::force(&state); LazyLock::force(&state);
b.iter_custom(|iters| { b.iter_custom(|iters| {
profiling::scope!("benchmark invocation"); profiling::scope!("benchmark invocation");
@ -541,7 +541,7 @@ fn run_bench(ctx: &mut Criterion) {
group.throughput(Throughput::Elements(draw_count as _)); group.throughput(Throughput::Elements(draw_count as _));
group.bench_function(format!("{draw_count} draws"), |b| { group.bench_function(format!("{draw_count} draws"), |b| {
Lazy::force(&state); LazyLock::force(&state);
b.iter_custom(|iters| { b.iter_custom(|iters| {
profiling::scope!("benchmark invocation"); profiling::scope!("benchmark invocation");
@ -577,7 +577,7 @@ fn run_bench(ctx: &mut Criterion) {
texture_count + vertex_buffer_count texture_count + vertex_buffer_count
), ),
|b| { |b| {
Lazy::force(&state); LazyLock::force(&state);
b.iter(|| state.device_state.queue.submit([])); b.iter(|| state.device_state.queue.submit([]));
}, },

View File

@ -1,13 +1,13 @@
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use criterion::{criterion_group, Criterion, Throughput}; use criterion::{criterion_group, Criterion, Throughput};
use once_cell::sync::Lazy;
use rayon::iter::{IntoParallelIterator, ParallelIterator}; use rayon::iter::{IntoParallelIterator, ParallelIterator};
use std::sync::LazyLock;
use crate::DeviceState; use crate::DeviceState;
fn run_bench(ctx: &mut Criterion) { fn run_bench(ctx: &mut Criterion) {
let state = Lazy::new(DeviceState::new); let state = LazyLock::new(DeviceState::new);
const RESOURCES_TO_CREATE: usize = 8; const RESOURCES_TO_CREATE: usize = 8;
@ -19,7 +19,7 @@ fn run_bench(ctx: &mut Criterion) {
group.bench_function( group.bench_function(
format!("{threads} threads x {resources_per_thread} resource"), format!("{threads} threads x {resources_per_thread} resource"),
|b| { |b| {
Lazy::force(&state); LazyLock::force(&state);
b.iter_custom(|iters| { b.iter_custom(|iters| {
profiling::scope!("benchmark invocation"); profiling::scope!("benchmark invocation");

View File

@ -122,7 +122,6 @@ parking_lot.workspace = true
profiling = { workspace = true, default-features = false } profiling = { workspace = true, default-features = false }
raw-window-handle.workspace = true raw-window-handle.workspace = true
thiserror.workspace = true thiserror.workspace = true
once_cell.workspace = true
# backends common # backends common
arrayvec.workspace = true arrayvec.workspace = true

View File

@ -1,9 +1,9 @@
use glow::HasContext; use glow::HasContext;
use once_cell::sync::Lazy;
use parking_lot::{MappedMutexGuard, Mutex, MutexGuard, RwLock}; use parking_lot::{MappedMutexGuard, Mutex, MutexGuard, RwLock};
use std::{ use std::{
collections::HashMap, ffi, mem::ManuallyDrop, os::raw, ptr, rc::Rc, sync::Arc, time::Duration, collections::HashMap, ffi, mem::ManuallyDrop, os::raw, ptr, rc::Rc, sync::Arc, sync::LazyLock,
time::Duration,
}; };
/// The amount of time to wait while trying to obtain a lock to the adapter context /// The amount of time to wait while trying to obtain a lock to the adapter context
@ -466,7 +466,8 @@ struct Inner {
// Different calls to `eglGetPlatformDisplay` may return the same `Display`, making it a global // Different calls to `eglGetPlatformDisplay` may return the same `Display`, making it a global
// state of all our `EglContext`s. This forces us to track the number of such context to prevent // state of all our `EglContext`s. This forces us to track the number of such context to prevent
// terminating the display if it's currently used by another `EglContext`. // terminating the display if it's currently used by another `EglContext`.
static DISPLAYS_REFERENCE_COUNT: Lazy<Mutex<HashMap<usize, usize>>> = Lazy::new(Default::default); static DISPLAYS_REFERENCE_COUNT: LazyLock<Mutex<HashMap<usize, usize>>> =
LazyLock::new(Default::default);
fn initialize_display( fn initialize_display(
egl: &EglInstance, egl: &EglInstance,

View File

@ -6,7 +6,7 @@ use std::{
ptr, ptr,
sync::{ sync::{
mpsc::{sync_channel, SyncSender}, mpsc::{sync_channel, SyncSender},
Arc, Arc, LazyLock,
}, },
thread, thread,
time::Duration, time::Duration,
@ -17,7 +17,6 @@ use glutin_wgl_sys::wgl_extra::{
Wgl, CONTEXT_CORE_PROFILE_BIT_ARB, CONTEXT_DEBUG_BIT_ARB, CONTEXT_FLAGS_ARB, Wgl, CONTEXT_CORE_PROFILE_BIT_ARB, CONTEXT_DEBUG_BIT_ARB, CONTEXT_FLAGS_ARB,
CONTEXT_PROFILE_MASK_ARB, CONTEXT_PROFILE_MASK_ARB,
}; };
use once_cell::sync::Lazy;
use parking_lot::{Mutex, MutexGuard, RwLock}; use parking_lot::{Mutex, MutexGuard, RwLock};
use raw_window_handle::{RawDisplayHandle, RawWindowHandle}; use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use wgt::InstanceFlags; use wgt::InstanceFlags;
@ -319,8 +318,8 @@ fn create_global_window_class() -> Result<CString, crate::InstanceError> {
} }
fn get_global_window_class() -> Result<CString, crate::InstanceError> { fn get_global_window_class() -> Result<CString, crate::InstanceError> {
static GLOBAL: Lazy<Result<CString, crate::InstanceError>> = static GLOBAL: LazyLock<Result<CString, crate::InstanceError>> =
Lazy::new(create_global_window_class); LazyLock::new(create_global_window_class);
GLOBAL.clone() GLOBAL.clone()
} }