lucaspoffo / renet

Server/Client network library for multiplayer games with authentication and connection management made with Rust
Apache License 2.0
685 stars 68 forks source link

Refactor RenetClient so channels are accessed more efficiently #154

Open UkoeHB opened 8 months ago

UkoeHB commented 8 months ago

Problem

Currently you need a HashMap lookup to access channels in clients. On the server these hashmaps are accessed for every packet received.

Solution

Use a Vec to store the channels so lookups are fast. Channels should be generated in monotonically increasing order, so in general there won't be any empty entries. Even if there are empty entries, at most 256 channels can be created, so the memory cost of bad channel assignment is not significant.

Follow-up

UkoeHB commented 8 months ago

But I would probably remove channel_id from ChannelConfig and automatically assign it based on the index. This way we ensure that channels have sequenced IDs.

That would be a breaking change, this is just a refactor PR.