From 375a8a83a47ed48cc61fae3cee79ea12334cca04 Mon Sep 17 00:00:00 2001 From: leudz Date: Wed, 22 Jan 2025 16:45:43 +0100 Subject: [PATCH] Simplify tracking logic With TrackingTimestamp now 64 bits, there is no need to wrap anymore --- src/borrow/mod.rs | 47 +++++++++---------------------- src/sparse_set/bulk_add_entity.rs | 2 +- src/sparse_set/mod.rs | 2 +- src/tracking.rs | 40 +++++++------------------- src/unique.rs | 6 ++-- src/views/view.rs | 2 +- 6 files changed, 29 insertions(+), 70 deletions(-) diff --git a/src/borrow/mod.rs b/src/borrow/mod.rs index 5928525a..8e2b80fb 100644 --- a/src/borrow/mod.rs +++ b/src/borrow/mod.rs @@ -207,16 +207,9 @@ where sparse_set.check_tracking::()?; - Ok(NonSend(View { - last_insertion: last_run.unwrap_or(sparse_set.last_insert), - last_modification: last_run.unwrap_or(sparse_set.last_modified), - last_removal_or_deletion: last_run.unwrap_or(current), - current, - sparse_set, - borrow, - all_borrow, - phantom: PhantomData, - })) + Ok(NonSend(View::new( + sparse_set, borrow, all_borrow, last_run, current, + ))) } } @@ -240,16 +233,9 @@ where sparse_set.check_tracking::()?; - Ok(NonSync(View { - last_insertion: last_run.unwrap_or(sparse_set.last_insert), - last_modification: last_run.unwrap_or(sparse_set.last_modified), - last_removal_or_deletion: last_run.unwrap_or(current), - current, - sparse_set, - borrow, - all_borrow, - phantom: PhantomData, - })) + Ok(NonSync(View::new( + sparse_set, borrow, all_borrow, last_run, current, + ))) } } @@ -274,16 +260,9 @@ where sparse_set.check_tracking::()?; - Ok(NonSendSync(View { - last_insertion: last_run.unwrap_or(sparse_set.last_insert), - last_modification: last_run.unwrap_or(sparse_set.last_modified), - last_removal_or_deletion: last_run.unwrap_or(current), - current, - sparse_set, - borrow, - all_borrow, - phantom: PhantomData, - })) + Ok(NonSendSync(View::new( + sparse_set, borrow, all_borrow, last_run, current, + ))) } } @@ -309,7 +288,7 @@ where Ok(ViewMut { last_insertion: last_run.unwrap_or(sparse_set.last_insert), last_modification: last_run.unwrap_or(sparse_set.last_modified), - last_removal_or_deletion: last_run.unwrap_or(current), + last_removal_or_deletion: last_run.unwrap_or(TrackingTimestamp::origin()), current, sparse_set, borrow, @@ -343,7 +322,7 @@ where Ok(NonSend(ViewMut { last_insertion: last_run.unwrap_or(sparse_set.last_insert), last_modification: last_run.unwrap_or(sparse_set.last_modified), - last_removal_or_deletion: last_run.unwrap_or(current), + last_removal_or_deletion: last_run.unwrap_or(TrackingTimestamp::origin()), current, sparse_set, borrow: borrow, @@ -377,7 +356,7 @@ where Ok(NonSync(ViewMut { last_insertion: last_run.unwrap_or(sparse_set.last_insert), last_modification: last_run.unwrap_or(sparse_set.last_modified), - last_removal_or_deletion: last_run.unwrap_or(current), + last_removal_or_deletion: last_run.unwrap_or(TrackingTimestamp::origin()), current, sparse_set, borrow: borrow, @@ -411,7 +390,7 @@ where Ok(NonSendSync(ViewMut { last_insertion: last_run.unwrap_or(sparse_set.last_insert), last_modification: last_run.unwrap_or(sparse_set.last_modified), - last_removal_or_deletion: last_run.unwrap_or(current), + last_removal_or_deletion: last_run.unwrap_or(TrackingTimestamp::origin()), current, sparse_set, borrow: borrow, diff --git a/src/sparse_set/bulk_add_entity.rs b/src/sparse_set/bulk_add_entity.rs index 25510fb2..5aebf2c1 100644 --- a/src/sparse_set/bulk_add_entity.rs +++ b/src/sparse_set/bulk_add_entity.rs @@ -91,7 +91,7 @@ impl BulkInsert for T { if sparse_set.is_tracking_modification() { sparse_set .modification_data - .extend(new_entities.iter().map(|_| current.furthest_from())); + .extend(new_entities.iter().map(|_| TrackingTimestamp::origin())); } let SparseSet { sparse, dense, .. } = &mut *sparse_set; diff --git a/src/sparse_set/mod.rs b/src/sparse_set/mod.rs index bfac59dc..5dd1fe02 100644 --- a/src/sparse_set/mod.rs +++ b/src/sparse_set/mod.rs @@ -249,7 +249,7 @@ impl SparseSet { self.insertion_data.push(current); } if self.is_tracking_modification { - self.modification_data.push(current.furthest_from()); + self.modification_data.push(TrackingTimestamp::origin()); } self.dense.push(entity); diff --git a/src/tracking.rs b/src/tracking.rs index 1acc0aff..d7b7de00 100644 --- a/src/tracking.rs +++ b/src/tracking.rs @@ -191,34 +191,27 @@ impl TrackingTimestamp { TrackingTimestamp(now) } + /// Returns a new [`TrackingTimestamp`] that is before all other timestamps. + #[inline] + pub fn origin() -> TrackingTimestamp { + TrackingTimestamp::new(0) + } + #[inline] pub(crate) fn get(self) -> u64 { self.0 } - /// Returns `true` when the track timestamp is after the last time the system ran and before the current execution. - /// - /// This method should only be necessary for custom storages that want to implement tracking. + /// Returns `true` when `self` is within the (last, current] range. #[inline] pub fn is_within(self, last: TrackingTimestamp, current: TrackingTimestamp) -> bool { - let bounds = current.0.wrapping_sub(last.0.wrapping_add(1)); - let track = current.0.wrapping_sub(self.0); - - track <= bounds + last.0 < self.0 && self.0 <= current.0 } - /// Returns `true` when the track timestamp is within `u32::MAX / 2` cycles of `other`. - /// - /// This method should only be necessary for custom storages that want to implement tracking. + /// Returns `true` when `self` is older than `other`. #[inline] pub fn is_older_than(self, other: TrackingTimestamp) -> bool { - 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(u64::MAX / 2)) + self.0 < other.0 } } @@ -229,13 +222,8 @@ mod tests { #[test] fn is_within_bounds() { let tests = [ - (0, 0, 0, true), (5, 0, 10, true), (11, 0, 10, false), - // check wrapping true - (u64::MAX, u64::MAX - 1, 0, true), - // check wrapping false - (u64::MAX - 1, u64::MAX, 0, false), (1, 2, 0, false), // timestamp is equal to last (1, 1, 0, false), @@ -261,14 +249,6 @@ mod tests { (0, 0, false), (5, 10, true), (11, 10, false), - // check wrapping true - (u64::MAX, 0, true), - // check wrapping false - (0, u64::MAX, false), - // barely within limit - (0, u64::MAX / 2, true), - // barely outside limit - (0, u64::MAX / 2 + 1, false), // timestamp is equal to other (1, 1, false), ]; diff --git a/src/unique.rs b/src/unique.rs index ec85d21b..fb6d9647 100644 --- a/src/unique.rs +++ b/src/unique.rs @@ -33,9 +33,9 @@ impl UniqueStorage { UniqueStorage { value, insert: current, - modification: current.furthest_from(), - last_insert: current.furthest_from(), - last_modification: current.furthest_from(), + modification: TrackingTimestamp::origin(), + last_insert: TrackingTimestamp::origin(), + last_modification: TrackingTimestamp::origin(), } } } diff --git a/src/views/view.rs b/src/views/view.rs index 9caf1112..3f34c633 100644 --- a/src/views/view.rs +++ b/src/views/view.rs @@ -64,7 +64,7 @@ impl<'a, T: Component, Track: Tracking> View<'a, T, Track> { Self { last_insertion: last_run.unwrap_or(sparse_set.last_insert), last_modification: last_run.unwrap_or(sparse_set.last_modified), - last_removal_or_deletion: last_run.unwrap_or(current), + last_removal_or_deletion: last_run.unwrap_or(TrackingTimestamp::origin()), current, sparse_set, borrow,