-
Notifications
You must be signed in to change notification settings - Fork 32
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
client function in ReplicatedClients is O(n) #326
Comments
Hi! I used a But I also see your problem. I need to think about it. Maybe you could iterate over clients first and cache the necessary data on your side? |
bevy_replicon_attributes might be a good solution to your use-case. You'd probably want to do perf tests comparing it with a raw approach. And also perf test both with few and many clients. How to implement:
Since each player and entity will change sectors at most a few times per second, you avoid the worst-case of updating visibility of everything every tick. You also decouple entities from clients, allowing more complex visibility calculations for players (e.g. what if a big rock occludes entities?). |
What about players moving frequently between sector borders. In my indented approach I would make entities inside a 3x3 range visible and entities outside a 5x5 range invisible. This is possible using your approach when the player keeps ownership over all sectors in a 3x3 range (keep in mind that all these 9 sectors need to lookout for a 3x3 range). Since I consider this a hotpath it would be good to have more control over the implementation.
I think O(1) access is very useful since getting entities by id is also used inside the replicon. I would suggest one of the following approaches.
I think the least intrusive and easiest to implement is probably 4. We keep the data structure in tact and just change only the methods add, remove & get. What do you think? |
It's hard to know unless you test it. The various options are easy to implement, I recommend testing to find what works best. You should also test with a fork of |
I would go with this one. It's as simple as 4, but more performant for lookups and doesn't require a separate crate. |
@phisn could you submit a PR? |
Will implement it in the next month. |
Is there any reason why a vector instead of a hash map is used here?
bevy_replicon/src/core/replicated_clients.rs
Lines 21 to 25 in c740527
I have the following use-case: Entities & Players have a transform and move around. When entities get near players they should become visible. Given I have a lot of entities moving around, it seems more realistic that the entity notifies the player if it gets near enough. Therefore I would like to lookup ClientVisibility efficiently.
The required function is
change_visibility(entity, client_id, state);
. We can implement it the following:replicated_clients.client(client_id).visibility_mut().set_visibility(entity, state)
.replicated_client.visibility_mut().set_visibility(entity, state)
when iterating over all clients usingreplicated_clients.iter_mut()
. Since the number of roaming entities is large it is not visible to do this without caching proximity changes during iterating the entities. We could use a HashMap<ClientId, Vec<(Entity, bool)>> which seems unnecessary difficult considering the underlying structure of ReplicatedClients.I suggest making
Clients
aHashMap
to an enable a broader range of use-cases. I am open do fix this myself if there is no real reason against this suggested approach.The text was updated successfully, but these errors were encountered: