Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rolling back resources #622

Merged
merged 3 commits into from
Sep 4, 2024

Conversation

nick-e
Copy link
Contributor

@nick-e nick-e commented Sep 4, 2024

Support rollback for resources. Make the ReadyBuffer not require its item to implement PartialEq. ReadyBuffer should only have one item per tick and so only the tick needs to be compared and not the item. Call App::add_resource_rollback<R>() to add rollback support for a resource.

Fixes #585

@nick-e nick-e changed the title Resource rollback Support rolling back resources Sep 4, 2024
/// Adds an item to the heap marked by time
pub fn push(&mut self, key: K, item: T) {
self.heap.push(ItemWithReadyKey { key, item });
}

/// Returns a reference to the item with the highest `K` value or None if the queue is empty.
/// Does not remove the item from the queue.
pub fn peek_latest_item(&self) -> Option<(K, &T)> {
Copy link
Owner

@cBournhonesque cBournhonesque Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this return the item with the lowest K value?

Can we add a unit test for it?

Copy link
Contributor Author

@nick-e nick-e Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the docs, std::collections::BinaryHeap is a max-heap: https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html#method.peek

I've added a unit test for the new function.

Copy link
Owner

@cBournhonesque cBournhonesque Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but I inverted the Ord order of ItemWithReadyKey to make it a min-heap, or I thought..
I'll have to take a closer look after merging

fn eq(&self, other: &Self) -> bool {
self.item == other.item && self.key == other.key
self.key == other.key
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what if we compare we have two component histories with values at the same ticks but the values are different?
Then we would get ready_buffer_1 == ready_buffer_2?

Copy link
Contributor Author

@nick-e nick-e Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ItemWithReadyKey implements PartialEq so that it can implement Ord so that it can be used in a BinaryHeap within ReadyBuffer. ReadyBuffer's comments describe its functions as dependent on the order of the key (e.g. a tick or instant) and does not mention being dependent on the order of the item's value.

ReadyBuffer doesn't implement PartialEq so ready_buffer_1 == ready_buffer_2 doesn't compile but if it did then the equality would describe whether or not the two buffers have the same keys.

I don't think it would make sense for a ReadyBuffer to have multiple items at the same key. That could result in a scenario where a component has two values at a given tick.

This also allows rollback for resources that don't implement PartialEq.

@cBournhonesque
Copy link
Owner

This looks really good overall! I just left a couple comments

@cBournhonesque
Copy link
Owner

Thanks! the changes look great

@cBournhonesque cBournhonesque merged commit 2cd8cf8 into cBournhonesque:main Sep 4, 2024
4 of 5 checks passed
@nick-e nick-e deleted the resource-rollback branch September 17, 2024 20:45
@cBournhonesque
Copy link
Owner

cBournhonesque commented Jan 10, 2025

Coming back to this, a check_rollback<R> function is probably needed as well?

EDIT: actually that's not relevant because this is PR is for non-networked resources, so we are not receiving server updates for them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle non-networked rollback for resources
2 participants