Skip to content

Commit

Permalink
Rework messages serialization (#352)
Browse files Browse the repository at this point in the history
Co-authored-by: UkoeHB <[email protected]>
  • Loading branch information
Shatur and UkoeHB authored Nov 26, 2024
1 parent df44894 commit 0c7f059
Show file tree
Hide file tree
Showing 32 changed files with 1,716 additions and 1,327 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Various optimizations for replication messages to use fewer bytes.
- Accept `Vec<u8>` instead of `Cursor<Vec<u8>>` for serialization.
- `ConnectedClients` now store `ConnectedClient` instead of `ClientId` with more information about the client.
- All `TestFnsEntityExt` now accept `FnsId`.
- Move replication-related modules from `core` module under `core::replication`.
- Move `Replicated` to the `replication` module.
- Split the `ctx` module and move event-related contexts under `core::events_registry::ctx` and replication-related contexts under `core::replication_registry::ctx`.
- Rename `ServerPlugin::change_timeout` into `ServerPlugin::mutations_timeout`.
- Rename `ServerInitTick` into `ServerChangeTick`.
- Rename `ReplicatedClient::init_tick` into `ReplicatedClient::change_tick`.
- Rename `ReplicatedClient::get_change_tick` into `ReplicatedClient::mutation_tick`.
- Rename `ReplicationChannel::Init` into `ReplicationChannel::Changes`.
- Rename `ReplicationChannel::Update` into `ReplicationChannel::Mutations`.

### Removed

Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ bincode = "1.3"
serde = "1.0"
integer-encoding = "4.0"
ordered-multimap = "0.7"
bitflags = "2.6"

[dev-dependencies]
bevy = { version = "0.14", default-features = false, features = [
Expand Down Expand Up @@ -66,7 +67,7 @@ name = "replication"
harness = false

[[test]]
name = "changes"
name = "mutations"
required-features = ["client", "server"]

[[test]]
Expand Down
77 changes: 40 additions & 37 deletions benches/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn replication<C: Component + Default + Serialize + DeserializeOwned + Clone>(c:
name = &name[MODULE_PREFIX_LEN..];

for clients in [1, 20] {
c.bench_function(&format!("{name}, init send, {clients} client(s)"), |b| {
c.bench_function(&format!("{name}, changes send, {clients} client(s)"), |b| {
b.iter_custom(|iter| {
let mut elapsed = Duration::ZERO;
for _ in 0..iter {
Expand Down Expand Up @@ -82,53 +82,56 @@ fn replication<C: Component + Default + Serialize + DeserializeOwned + Clone>(c:
})
});

c.bench_function(&format!("{name}, update send, {clients} client(s)"), |b| {
b.iter_custom(|iter| {
let mut server_app = create_app::<C>();
let mut client_apps = Vec::new();
for _ in 0..clients {
client_apps.push(create_app::<C>());
}

for client_app in &mut client_apps {
server_app.connect_client(client_app);
}

server_app
.world_mut()
.spawn_batch(vec![(Replicated, C::default()); ENTITIES as usize]);
let mut query = server_app.world_mut().query::<&mut C>();

server_app.update();
for client_app in &mut client_apps {
server_app.exchange_with_client(client_app);
client_app.update();
assert_eq!(client_app.world().entities().len(), ENTITIES);
}
c.bench_function(
&format!("{name}, mutations send, {clients} client(s)"),
|b| {
b.iter_custom(|iter| {
let mut server_app = create_app::<C>();
let mut client_apps = Vec::new();
for _ in 0..clients {
client_apps.push(create_app::<C>());
}

let mut elapsed = Duration::ZERO;
for _ in 0..iter {
for mut component in query.iter_mut(server_app.world_mut()) {
component.set_changed();
for client_app in &mut client_apps {
server_app.connect_client(client_app);
}

let instant = Instant::now();
server_app.update();
elapsed += instant.elapsed();
server_app
.world_mut()
.spawn_batch(vec![(Replicated, C::default()); ENTITIES as usize]);
let mut query = server_app.world_mut().query::<&mut C>();

server_app.update();
for client_app in &mut client_apps {
server_app.exchange_with_client(client_app);
client_app.update();
assert_eq!(client_app.world().entities().len(), ENTITIES);
}
}

elapsed
})
});
let mut elapsed = Duration::ZERO;
for _ in 0..iter {
for mut component in query.iter_mut(server_app.world_mut()) {
component.set_changed();
}

let instant = Instant::now();
server_app.update();
elapsed += instant.elapsed();

for client_app in &mut client_apps {
server_app.exchange_with_client(client_app);
client_app.update();
assert_eq!(client_app.world().entities().len(), ENTITIES);
}
}

elapsed
})
},
);
}

c.bench_function(&format!("{name}, init receive"), |b| {
c.bench_function(&format!("{name}, changes receive"), |b| {
b.iter_custom(|iter| {
let mut elapsed = Duration::ZERO;
for _ in 0..iter {
Expand All @@ -154,7 +157,7 @@ fn replication<C: Component + Default + Serialize + DeserializeOwned + Clone>(c:
})
});

c.bench_function(&format!("{name}, update receive"), |b| {
c.bench_function(&format!("{name}, mutations receive"), |b| {
b.iter_custom(|iter| {
let mut server_app = create_app::<C>();
let mut client_app = create_app::<C>();
Expand Down
Loading

0 comments on commit 0c7f059

Please sign in to comment.