From c9785486a19ad8339506024db9321a433ee4f902 Mon Sep 17 00:00:00 2001 From: sm <80ROkWOC4j@gmail.com> Date: Wed, 22 Jan 2025 23:50:50 +0900 Subject: [PATCH] Change tracking value type,from u32 to u64. (#214) --- src/all_storages/mod.rs | 8 ++++---- src/scheduler/into_workload_run_if.rs | 6 +++--- src/scheduler/into_workload_system.rs | 4 ++-- src/scheduler/into_workload_try_system.rs | 6 +++--- src/tracking.rs | 22 +++++++++++----------- src/world.rs | 6 +++--- src/world/builder.rs | 4 ++-- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/all_storages/mod.rs b/src/all_storages/mod.rs index 1c752658..c8879cee 100644 --- a/src/all_storages/mod.rs +++ b/src/all_storages/mod.rs @@ -33,7 +33,7 @@ use alloc::sync::Arc; use core::any::type_name; use core::hash::BuildHasherDefault; use core::marker::PhantomData; -use core::sync::atomic::AtomicU32; +use core::sync::atomic::AtomicU64; use hashbrown::hash_map::Entry; #[allow(missing_docs)] @@ -103,7 +103,7 @@ impl AllStoragesBuilder { } impl AllStoragesBuilder { - pub(crate) fn build(self, counter: Arc) -> AtomicRefCell { + pub(crate) fn build(self, counter: Arc) -> AtomicRefCell { let mut storages = ShipHashMap::with_hasher(BuildHasherDefault::default()); storages.insert(StorageId::of::(), SBox::new(Entities::new())); @@ -159,7 +159,7 @@ pub struct AllStorages { main_thread_id: u64, #[cfg(feature = "thread_local")] thread_id_generator: Arc u64 + Send + Sync>, - counter: Arc, + counter: Arc, } #[cfg(not(feature = "thread_local"))] @@ -169,7 +169,7 @@ unsafe impl Sync for AllStorages {} impl AllStorages { #[cfg(feature = "std")] - pub(crate) fn new(counter: Arc) -> Self { + pub(crate) fn new(counter: Arc) -> Self { let mut storages = ShipHashMap::with_hasher(BuildHasherDefault::default()); storages.insert(StorageId::of::(), SBox::new(Entities::new())); diff --git a/src/scheduler/into_workload_run_if.rs b/src/scheduler/into_workload_run_if.rs index 22c6039f..03c5061a 100644 --- a/src/scheduler/into_workload_run_if.rs +++ b/src/scheduler/into_workload_run_if.rs @@ -9,7 +9,7 @@ use crate::World; use alloc::boxed::Box; use alloc::sync::Arc; use alloc::vec::Vec; -use core::sync::atomic::{AtomicU32, Ordering}; +use core::sync::atomic::{AtomicU64, Ordering}; pub trait IntoRunIf { fn into_workload_run_if(self) -> Result; @@ -80,7 +80,7 @@ macro_rules! impl_into_workload_run_if { } } - let last_run = AtomicU32::new(0); + let last_run = AtomicU64::new(0); Ok(RunIf { system_fn: Box::new(move |world: &World| { let current = world.get_current(); @@ -165,7 +165,7 @@ macro_rules! impl_into_workload_run_if { } } - let last_run = Arc::new(AtomicU32::new(0)); + let last_run = Arc::new(AtomicU64::new(0)); Ok(Box::new(move |world: &World| { let current = world.get_current(); let last_run = TrackingTimestamp::new(last_run.swap(current.get(), Ordering::Acquire)); diff --git a/src/scheduler/into_workload_system.rs b/src/scheduler/into_workload_system.rs index cc8370ba..8fb0d735 100644 --- a/src/scheduler/into_workload_system.rs +++ b/src/scheduler/into_workload_system.rs @@ -12,7 +12,7 @@ use alloc::boxed::Box; use alloc::vec; use alloc::vec::Vec; use core::any::type_name; -use core::sync::atomic::{AtomicU32, Ordering}; +use core::sync::atomic::{AtomicU64, Ordering}; /// Trait used to add systems to a workload. /// @@ -145,7 +145,7 @@ macro_rules! impl_into_workload_system { $type::enable_tracking(&mut tracking_to_enable); )+ - let last_run = AtomicU32::new(0); + let last_run = AtomicU64::new(0); Ok(WorkloadSystem { borrow_constraints: borrows, tracking_to_enable, diff --git a/src/scheduler/into_workload_try_system.rs b/src/scheduler/into_workload_try_system.rs index f0bb8699..0380a91e 100644 --- a/src/scheduler/into_workload_try_system.rs +++ b/src/scheduler/into_workload_try_system.rs @@ -15,7 +15,7 @@ use alloc::vec::Vec; use core::any::type_name; #[cfg(not(feature = "std"))] use core::any::Any; -use core::sync::atomic::{AtomicU32, Ordering}; +use core::sync::atomic::{AtomicU64, Ordering}; #[cfg(feature = "std")] use std::error::Error; @@ -179,7 +179,7 @@ macro_rules! impl_into_workload_try_system { $type::enable_tracking(&mut tracking_to_enable); )+ - let last_run = AtomicU32::new(0); + let last_run = AtomicU64::new(0); Ok(WorkloadSystem { borrow_constraints: borrows, tracking_to_enable, @@ -250,7 +250,7 @@ macro_rules! impl_into_workload_try_system { $type::enable_tracking(&mut tracking_to_enable); )+ - let last_run = AtomicU32::new(0); + let last_run = AtomicU64::new(0); Ok(WorkloadSystem { borrow_constraints: borrows, tracking_to_enable, diff --git a/src/tracking.rs b/src/tracking.rs index d80d13d1..1acc0aff 100644 --- a/src/tracking.rs +++ b/src/tracking.rs @@ -182,17 +182,17 @@ pub(crate) fn map_deletion_data( /// Timestamp used to clear tracking information. #[derive(Clone, Copy, Debug)] -pub struct TrackingTimestamp(u32); +pub struct TrackingTimestamp(u64); impl TrackingTimestamp { /// Returns a new [`TrackingTimestamp`] at the given tracking cycle. #[inline] - pub fn new(now: u32) -> TrackingTimestamp { + pub fn new(now: u64) -> TrackingTimestamp { TrackingTimestamp(now) } #[inline] - pub(crate) fn get(self) -> u32 { + pub(crate) fn get(self) -> u64 { self.0 } @@ -212,13 +212,13 @@ impl TrackingTimestamp { /// This method should only be necessary for custom storages that want to implement tracking. #[inline] pub fn is_older_than(self, other: TrackingTimestamp) -> bool { - other.0.wrapping_sub(1).wrapping_sub(self.0) < u32::MAX / 2 + other.0.wrapping_sub(1).wrapping_sub(self.0) < u64::MAX / 2 } /// Returns the timesptamp the furthest from the given one. #[inline] pub fn furthest_from(self) -> TrackingTimestamp { - TrackingTimestamp(self.0.wrapping_add(u32::MAX / 2)) + TrackingTimestamp(self.0.wrapping_add(u64::MAX / 2)) } } @@ -233,9 +233,9 @@ mod tests { (5, 0, 10, true), (11, 0, 10, false), // check wrapping true - (u32::MAX, u32::MAX - 1, 0, true), + (u64::MAX, u64::MAX - 1, 0, true), // check wrapping false - (u32::MAX - 1, u32::MAX, 0, false), + (u64::MAX - 1, u64::MAX, 0, false), (1, 2, 0, false), // timestamp is equal to last (1, 1, 0, false), @@ -262,13 +262,13 @@ mod tests { (5, 10, true), (11, 10, false), // check wrapping true - (u32::MAX, 0, true), + (u64::MAX, 0, true), // check wrapping false - (0, u32::MAX, false), + (0, u64::MAX, false), // barely within limit - (0, u32::MAX / 2, true), + (0, u64::MAX / 2, true), // barely outside limit - (0, u32::MAX / 2 + 1, false), + (0, u64::MAX / 2 + 1, false), // timestamp is equal to other (1, 1, false), ]; diff --git a/src/world.rs b/src/world.rs index 0e271ec5..fe0a66a2 100644 --- a/src/world.rs +++ b/src/world.rs @@ -27,13 +27,13 @@ use crate::views::EntitiesViewMut; use alloc::boxed::Box; use alloc::format; use alloc::sync::Arc; -use core::sync::atomic::AtomicU32; +use core::sync::atomic::AtomicU64; /// `World` contains all data this library will manipulate. pub struct World { pub(crate) all_storages: AtomicRefCell, pub(crate) scheduler: AtomicRefCell, - counter: Arc, + counter: Arc, #[cfg(feature = "parallel")] thread_pool: Option, } @@ -42,7 +42,7 @@ pub struct World { impl Default for World { /// Creates an empty `World`. fn default() -> Self { - let counter = Arc::new(AtomicU32::new(1)); + let counter = Arc::new(AtomicU64::new(1)); World { #[cfg(not(feature = "thread_local"))] all_storages: AtomicRefCell::new(AllStorages::new(counter.clone())), diff --git a/src/world/builder.rs b/src/world/builder.rs index cbdb44b6..b4edb781 100644 --- a/src/world/builder.rs +++ b/src/world/builder.rs @@ -3,7 +3,7 @@ use crate::atomic_refcell::AtomicRefCell; use crate::public_transport::ShipyardRwLock; use crate::world::World; use alloc::sync::Arc; -use core::sync::atomic::AtomicU32; +use core::sync::atomic::AtomicU64; /// Builder for [`World`] when one wants custom lock, custom thread pool /// or custom thread id provider function. @@ -105,7 +105,7 @@ impl WorldBuilder { impl WorldBuilder { /// Creates a new [`World`] based on the [`WorldBuilder`] config. pub fn build(self) -> World { - let counter = Arc::new(AtomicU32::new(1)); + let counter = Arc::new(AtomicU64::new(1)); let all_storages = self.all_storages_builder.build(counter.clone());