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

Rework messages serialization #352

Merged
merged 35 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b317a6d
Optimize replication message packing
Shatur Nov 13, 2024
396d91b
Fix doc tests
Shatur Nov 13, 2024
c1bb570
Add a test for many entities
Shatur Nov 15, 2024
2f727b7
Replace many entities test with many components
Shatur Nov 15, 2024
098e18d
Merge remote-tracking branch 'origin/master' into ser-de-rework
Shatur Nov 15, 2024
4fe95b3
Rename `InitMessageHeader` into `InitMessageArrays`
Shatur Nov 16, 2024
972ab21
Swap order of `InitMessage::is_empty` checks
Shatur Nov 16, 2024
44270cc
Use "mutation" instead of "change" for update message for clarity
Shatur Nov 16, 2024
b40cab4
Don't serialize size for last array inside init message
Shatur Nov 16, 2024
a148ab1
Rename `UpdateMessage` into `MutateMessage`
Shatur Nov 16, 2024
e443c8b
Rename `InitMessage` into `ChangeMessage`
Shatur Nov 16, 2024
05336f8
Rename mutate_timeout into mutations_timeout
Shatur Nov 16, 2024
1fcee8b
Improve trace message
Shatur Nov 16, 2024
751e25a
Update docs
Shatur Nov 16, 2024
4fb17d2
Serialize array len for nested arrays of change message
Shatur Nov 17, 2024
6751dc2
Split `apply_array` to avoid having confusing bool arg
Shatur Nov 17, 2024
713f645
Add a test for removal with despawn
Shatur Nov 17, 2024
6558824
Assert if the message is empty
Shatur Nov 17, 2024
a37d3ca
Always include size for mappings
Shatur Nov 17, 2024
9f204f8
Rename `InitMessageArrays` into `InitMessageFlags`
Shatur Nov 18, 2024
397a18c
Print the number of mutate messages sent
Shatur Nov 18, 2024
b866310
Merge remote-tracking branch 'origin/master' into ser-de-rework
Shatur Nov 20, 2024
9eb97cb
Merge branch 'master' into ser-de-rework
Shatur Nov 20, 2024
6c1c60f
Merge branch 'master' into ser-de-rework
Shatur Nov 20, 2024
024829f
Merge branch 'master' into ser-de-rework
Shatur Nov 20, 2024
07be709
Apply doc suggestions from code review [skip ci]
Shatur Nov 25, 2024
50df358
Do not use varints for acks
Shatur Nov 25, 2024
09a6340
Don't use varint encoding for ticks
Shatur Nov 25, 2024
75683ed
Use guarded return
Shatur Nov 25, 2024
ccb1848
Refactor `take_mutations`
Shatur Nov 25, 2024
949a060
Add more assertions
Shatur Nov 25, 2024
42067db
Use a single function to apply array
Shatur Nov 26, 2024
2ad7a4a
Bring back `can_pack`
Shatur Nov 26, 2024
98c7404
Refactor the packing logic
Shatur Nov 26, 2024
d317805
Merge remote-tracking branch 'origin/master' into ser-de-rework
Shatur Nov 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ 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.
- 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`.
Comment on lines +27 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- Rename `ReplicationChannel::Init` into `ReplicationChannel::Changes`.
- Rename `ReplicationChannel::Update` into `ReplicationChannel::Mutations`.
- Rename `ReplicationChannel::Init` into `ReplicationChannel::Update`.
- Rename `ReplicationChannel::Update` into `ReplicationChannel::Mutations`.

What do you think about this instead? It would disambiguate the message type from the internal subsection called 'changes'. Changes also seems slightly off to me for the message name.

If you agree, let's change it in a follow-up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree!


### 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