Skip to content

Commit

Permalink
Remove OnceLock usage from bevy_ecs (#16870)
Browse files Browse the repository at this point in the history
# Objective

- Fixes #16868

## Solution

- Replaced several usages of `OnceLock` within `bevy_ecs` with `const`s

## Testing

- CI
  • Loading branch information
bushrat011899 authored Dec 17, 2024
1 parent 1f2d0e6 commit 1371619
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 49 deletions.
1 change: 0 additions & 1 deletion crates/bevy_ecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ portable-atomic-util = { version = "0.2.4", features = [
spin = { version = "0.9.8", default-features = false, features = [
"spin_mutex",
"rwlock",
"once",
] }
tracing = { version = "0.1", default-features = false, optional = true }
log = { version = "0.4", default-features = false }
Expand Down
24 changes: 9 additions & 15 deletions crates/bevy_ecs/src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use alloc::{borrow::ToOwned, boxed::Box};
use core::{fmt::Debug, hash::Hash, ops::Deref};

#[cfg(feature = "std")]
use std::sync::{OnceLock, PoisonError, RwLock};
use std::sync::{PoisonError, RwLock};

#[cfg(not(feature = "std"))]
use spin::{once::Once as OnceLock, rwlock::RwLock};
use spin::rwlock::RwLock;

use bevy_utils::HashSet;
use bevy_utils::{FixedHasher, HashSet};

/// An interned value. Will stay valid until the end of the program and will not drop.
///
Expand Down Expand Up @@ -126,12 +126,12 @@ impl Internable for str {
/// The implementation ensures that two equal values return two equal [`Interned<T>`] values.
///
/// To use an [`Interner<T>`], `T` must implement [`Internable`].
pub struct Interner<T: ?Sized + 'static>(OnceLock<RwLock<HashSet<&'static T>>>);
pub struct Interner<T: ?Sized + 'static>(RwLock<HashSet<&'static T>>);

impl<T: ?Sized> Interner<T> {
/// Creates a new empty interner
pub const fn new() -> Self {
Self(OnceLock::new())
Self(RwLock::new(HashSet::with_hasher(FixedHasher)))
}
}

Expand All @@ -142,18 +142,12 @@ impl<T: Internable + ?Sized> Interner<T> {
/// [`Interned<T>`] using the obtained static reference. Subsequent calls for the same `value`
/// will return [`Interned<T>`] using the same static reference.
pub fn intern(&self, value: &T) -> Interned<T> {
#[cfg(feature = "std")]
let lock = self.0.get_or_init(Default::default);

#[cfg(not(feature = "std"))]
let lock = self.0.call_once(Default::default);

{
#[cfg(feature = "std")]
let set = lock.read().unwrap_or_else(PoisonError::into_inner);
let set = self.0.read().unwrap_or_else(PoisonError::into_inner);

#[cfg(not(feature = "std"))]
let set = lock.read();
let set = self.0.read();

if let Some(value) = set.get(value) {
return Interned(*value);
Expand All @@ -162,10 +156,10 @@ impl<T: Internable + ?Sized> Interner<T> {

{
#[cfg(feature = "std")]
let mut set = lock.write().unwrap_or_else(PoisonError::into_inner);
let mut set = self.0.write().unwrap_or_else(PoisonError::into_inner);

#[cfg(not(feature = "std"))]
let mut set = lock.write();
let mut set = self.0.write();

if let Some(value) = set.get(value) {
Interned(*value)
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/query/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,13 @@ impl<T: SparseSetIndex> Access<T> {

/// Sets this as having access to all resources (i.e. `&World`).
#[inline]
pub fn read_all_resources(&mut self) {
pub const fn read_all_resources(&mut self) {
self.reads_all_resources = true;
}

/// Sets this as having mutable access to all resources (i.e. `&mut World`).
#[inline]
pub fn write_all_resources(&mut self) {
pub const fn write_all_resources(&mut self) {
self.reads_all_resources = true;
self.writes_all_resources = true;
}
Expand Down
39 changes: 8 additions & 31 deletions crates/bevy_ecs/src/world/filtered_resource.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#[cfg(feature = "std")]
use std::sync::OnceLock;

#[cfg(not(feature = "std"))]
use spin::once::Once as OnceLock;

use crate::{
change_detection::{Mut, MutUntyped, Ref, Ticks, TicksMut},
component::{ComponentId, Tick},
Expand Down Expand Up @@ -220,21 +214,13 @@ impl<'w, 's> From<&'w FilteredResourcesMut<'_, 's>> for FilteredResources<'w, 's

impl<'w> From<&'w World> for FilteredResources<'w, 'static> {
fn from(value: &'w World) -> Self {
static READ_ALL_RESOURCES: OnceLock<Access<ComponentId>> = OnceLock::new();
let access = {
let init = || {
const READ_ALL_RESOURCES: &Access<ComponentId> = {
const ACCESS: Access<ComponentId> = {
let mut access = Access::new();
access.read_all_resources();
access
};

#[cfg(feature = "std")]
let access = READ_ALL_RESOURCES.get_or_init(init);

#[cfg(not(feature = "std"))]
let access = READ_ALL_RESOURCES.call_once(init);

access
&ACCESS
};

let last_run = value.last_change_tick();
Expand All @@ -243,7 +229,7 @@ impl<'w> From<&'w World> for FilteredResources<'w, 'static> {
unsafe {
Self::new(
value.as_unsafe_world_cell_readonly(),
access,
READ_ALL_RESOURCES,
last_run,
this_run,
)
Expand Down Expand Up @@ -507,22 +493,13 @@ impl<'w, 's> FilteredResourcesMut<'w, 's> {

impl<'w> From<&'w mut World> for FilteredResourcesMut<'w, 'static> {
fn from(value: &'w mut World) -> Self {
static WRITE_ALL_RESOURCES: OnceLock<Access<ComponentId>> = OnceLock::new();

let access = {
let init = || {
const WRITE_ALL_RESOURCES: &Access<ComponentId> = {
const ACCESS: Access<ComponentId> = {
let mut access = Access::new();
access.write_all_resources();
access
};

#[cfg(feature = "std")]
let access = WRITE_ALL_RESOURCES.get_or_init(init);

#[cfg(not(feature = "std"))]
let access = WRITE_ALL_RESOURCES.call_once(init);

access
&ACCESS
};

let last_run = value.last_change_tick();
Expand All @@ -531,7 +508,7 @@ impl<'w> From<&'w mut World> for FilteredResourcesMut<'w, 'static> {
unsafe {
Self::new(
value.as_unsafe_world_cell_readonly(),
access,
WRITE_ALL_RESOURCES,
last_run,
this_run,
)
Expand Down

0 comments on commit 1371619

Please sign in to comment.