Skip to content

Commit

Permalink
Simplify tracking logic
Browse files Browse the repository at this point in the history
With TrackingTimestamp now 64 bits, there is no need to wrap anymore
  • Loading branch information
leudz committed Jan 22, 2025
1 parent c978548 commit 375a8a8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 70 deletions.
47 changes: 13 additions & 34 deletions src/borrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,9 @@ where

sparse_set.check_tracking::<Track>()?;

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,
)))
}
}

Expand All @@ -240,16 +233,9 @@ where

sparse_set.check_tracking::<Track>()?;

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,
)))
}
}

Expand All @@ -274,16 +260,9 @@ where

sparse_set.check_tracking::<Track>()?;

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,
)))
}
}

Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/sparse_set/bulk_add_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<T: Send + Sync + Component> 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;
Expand Down
2 changes: 1 addition & 1 deletion src/sparse_set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<T: Component> SparseSet<T> {
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);
Expand Down
40 changes: 10 additions & 30 deletions src/tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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),
Expand All @@ -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),
];
Expand Down
6 changes: 3 additions & 3 deletions src/unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ impl<T: Unique> UniqueStorage<T> {
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(),
}
}
}
2 changes: 1 addition & 1 deletion src/views/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 375a8a8

Please sign in to comment.