Skip to content

Commit

Permalink
refactor: s/once_cell::Lazy/std::sync::LazyLock
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ErichDonGubler committed Oct 11, 2024
2 parents f9c6bea + 5fd6747 commit f9bb783
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 33 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ naga = { workspace = true, features = [
"wgsl-out",
] }
nanorand.workspace = true
once_cell.workspace = true
pollster.workspace = true
profiling.workspace = true
rayon.workspace = true
Expand Down
12 changes: 6 additions & 6 deletions benches/benches/computepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::{

use criterion::{criterion_group, Criterion, Throughput};
use nanorand::{Rng, WyRand};
use once_cell::sync::Lazy;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use std::sync::LazyLock;

use crate::DeviceState;

Expand Down Expand Up @@ -424,7 +424,7 @@ impl ComputepassState {
}

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_bindless = dispatch_count_bindless();
Expand All @@ -449,7 +449,7 @@ fn run_bench(ctx: &mut Criterion) {
group.bench_function(
format!("{cpasses} computepasses x {dispatch_per_pass} dispatches ({label})"),
|b| {
Lazy::force(&state);
LazyLock::force(&state);

b.iter_custom(|iters| {
profiling::scope!("benchmark invocation");
Expand Down Expand Up @@ -498,7 +498,7 @@ fn run_bench(ctx: &mut Criterion) {
group.bench_function(
format!("{threads} threads x {dispatch_per_pass} dispatch"),
|b| {
Lazy::force(&state);
LazyLock::force(&state);

b.iter_custom(|iters| {
profiling::scope!("benchmark invocation");
Expand Down Expand Up @@ -538,7 +538,7 @@ fn run_bench(ctx: &mut Criterion) {
group.throughput(Throughput::Elements(dispatch_count_bindless as _));

group.bench_function(format!("{dispatch_count_bindless} dispatch"), |b| {
Lazy::force(&state);
LazyLock::force(&state);

b.iter_custom(|iters| {
profiling::scope!("benchmark invocation");
Expand Down Expand Up @@ -579,7 +579,7 @@ fn run_bench(ctx: &mut Criterion) {
texture_count + storage_texture_count + storage_buffer_count
),
|b| {
Lazy::force(&state);
LazyLock::force(&state);

b.iter(|| state.device_state.queue.submit([]));
},
Expand Down
12 changes: 6 additions & 6 deletions benches/benches/renderpass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::{

use criterion::{criterion_group, Criterion, Throughput};
use nanorand::{Rng, WyRand};
use once_cell::sync::Lazy;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use std::sync::LazyLock;

use crate::DeviceState;

Expand Down Expand Up @@ -427,7 +427,7 @@ impl RenderpassState {
}

fn run_bench(ctx: &mut Criterion) {
let state = Lazy::new(RenderpassState::new);
let state = LazyLock::new(RenderpassState::new);

let draw_count = draw_count();
let vertex_buffer_count = draw_count * VERTEX_BUFFERS_PER_DRAW;
Expand All @@ -450,7 +450,7 @@ fn run_bench(ctx: &mut Criterion) {
group.bench_function(
format!("{rpasses} renderpasses x {draws_per_pass} draws ({label})"),
|b| {
Lazy::force(&state);
LazyLock::force(&state);

b.iter_custom(|iters| {
profiling::scope!("benchmark invocation");
Expand Down Expand Up @@ -502,7 +502,7 @@ fn run_bench(ctx: &mut Criterion) {
for threads in [2, 4, 8] {
let draws_per_pass = draw_count / threads;
group.bench_function(format!("{threads} threads x {draws_per_pass} draws"), |b| {
Lazy::force(&state);
LazyLock::force(&state);

b.iter_custom(|iters| {
profiling::scope!("benchmark invocation");
Expand Down Expand Up @@ -541,7 +541,7 @@ fn run_bench(ctx: &mut Criterion) {
group.throughput(Throughput::Elements(draw_count as _));

group.bench_function(format!("{draw_count} draws"), |b| {
Lazy::force(&state);
LazyLock::force(&state);

b.iter_custom(|iters| {
profiling::scope!("benchmark invocation");
Expand Down Expand Up @@ -577,7 +577,7 @@ fn run_bench(ctx: &mut Criterion) {
texture_count + vertex_buffer_count
),
|b| {
Lazy::force(&state);
LazyLock::force(&state);

b.iter(|| state.device_state.queue.submit([]));
},
Expand Down
6 changes: 3 additions & 3 deletions benches/benches/resource_creation.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::time::{Duration, Instant};

use criterion::{criterion_group, Criterion, Throughput};
use once_cell::sync::Lazy;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use std::sync::LazyLock;

use crate::DeviceState;

fn run_bench(ctx: &mut Criterion) {
let state = Lazy::new(DeviceState::new);
let state = LazyLock::new(DeviceState::new);

const RESOURCES_TO_CREATE: usize = 8;

Expand All @@ -19,7 +19,7 @@ fn run_bench(ctx: &mut Criterion) {
group.bench_function(
format!("{threads} threads x {resources_per_thread} resource"),
|b| {
Lazy::force(&state);
LazyLock::force(&state);

b.iter_custom(|iters| {
profiling::scope!("benchmark invocation");
Expand Down
5 changes: 2 additions & 3 deletions wgpu-core/src/binding_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::{

use arrayvec::ArrayVec;

use once_cell::sync::OnceCell;
#[cfg(feature = "serde")]
use serde::Deserialize;
#[cfg(feature = "serde")]
Expand All @@ -27,7 +26,7 @@ use std::{
borrow::Cow,
mem::ManuallyDrop,
ops::Range,
sync::{Arc, Weak},
sync::{Arc, OnceLock, Weak},
};

use thiserror::Error;
Expand Down Expand Up @@ -498,7 +497,7 @@ pub struct BindGroupLayout {
/// We cannot unconditionally remove from the pool, as BGLs that don't come from the pool
/// (derived BGLs) must not be removed.
pub(crate) origin: bgl::Origin,
pub(crate) exclusive_pipeline: OnceCell<ExclusivePipeline>,
pub(crate) exclusive_pipeline: OnceLock<ExclusivePipeline>,
#[allow(unused)]
pub(crate) binding_count_validator: BindingTypeMaxCountValidator,
/// The `label` from the descriptor used to create the resource.
Expand Down
14 changes: 6 additions & 8 deletions wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ use crate::{
};

use arrayvec::ArrayVec;
use once_cell::sync::OnceCell;

use smallvec::SmallVec;
use wgt::{
math::align_to, DeviceLostReason, TextureFormat, TextureSampleType, TextureViewDimension,
Expand All @@ -48,7 +46,7 @@ use std::{
num::NonZeroU32,
sync::{
atomic::{AtomicBool, AtomicU64, Ordering},
Arc, Weak,
Arc, OnceLock, Weak,
},
};

Expand Down Expand Up @@ -80,8 +78,8 @@ use super::{
pub struct Device {
raw: ManuallyDrop<Box<dyn hal::DynDevice>>,
pub(crate) adapter: Arc<Adapter>,
pub(crate) queue: OnceCell<Weak<Queue>>,
queue_to_drop: OnceCell<Box<dyn hal::DynQueue>>,
pub(crate) queue: OnceLock<Weak<Queue>>,
queue_to_drop: OnceLock<Box<dyn hal::DynQueue>>,
pub(crate) zero_buffer: ManuallyDrop<Box<dyn hal::DynBuffer>>,
/// The `label` from the descriptor used to create the resource.
label: String,
Expand Down Expand Up @@ -266,8 +264,8 @@ impl Device {
Ok(Self {
raw: ManuallyDrop::new(raw_device),
adapter: adapter.clone(),
queue: OnceCell::new(),
queue_to_drop: OnceCell::new(),
queue: OnceLock::new(),
queue_to_drop: OnceLock::new(),
zero_buffer: ManuallyDrop::new(zero_buffer),
label: desc.label.to_string(),
command_allocator,
Expand Down Expand Up @@ -1865,7 +1863,7 @@ impl Device {
device: self.clone(),
entries: entry_map,
origin,
exclusive_pipeline: OnceCell::new(),
exclusive_pipeline: OnceLock::new(),
binding_count_validator: count_validator,
label: label.to_string(),
};
Expand Down
1 change: 0 additions & 1 deletion wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ parking_lot.workspace = true
profiling = { workspace = true, default-features = false }
raw-window-handle.workspace = true
thiserror.workspace = true
once_cell.workspace = true

# backends common
arrayvec.workspace = true
Expand Down
7 changes: 4 additions & 3 deletions wgpu-hal/src/gles/egl.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use glow::HasContext;
use once_cell::sync::Lazy;
use parking_lot::{MappedMutexGuard, Mutex, MutexGuard, RwLock};

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
Expand Down Expand Up @@ -466,7 +466,8 @@ struct Inner {
// 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
// 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(
egl: &EglInstance,
Expand Down

0 comments on commit f9bb783

Please sign in to comment.