Skip to content

Commit

Permalink
[MP] Optimize internal SceneReplicationConfig access
Browse files Browse the repository at this point in the history
Use direct pointer addressing to avoid unnecessary refs/unrefs
  • Loading branch information
Faless committed Oct 10, 2023
1 parent 311a272 commit 5df7577
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions modules/multiplayer/multiplayer_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ void MultiplayerDebugger::RPCProfiler::tick(double p_frame_time, double p_proces
MultiplayerDebugger::SyncInfo::SyncInfo(MultiplayerSynchronizer *p_sync) {
ERR_FAIL_NULL(p_sync);
synchronizer = p_sync->get_instance_id();
if (p_sync->get_replication_config().is_valid()) {
config = p_sync->get_replication_config()->get_instance_id();
if (p_sync->get_replication_config_ptr()) {
config = p_sync->get_replication_config_ptr()->get_instance_id();
}
if (p_sync->get_root_node()) {
root_node = p_sync->get_root_node()->get_instance_id();
Expand Down
4 changes: 4 additions & 0 deletions modules/multiplayer/multiplayer_synchronizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ List<NodePath> MultiplayerSynchronizer::get_delta_properties(uint64_t p_indexes)
return out;
}

SceneReplicationConfig *MultiplayerSynchronizer::get_replication_config_ptr() const {
return replication_config.ptr();
}

MultiplayerSynchronizer::MultiplayerSynchronizer() {
// Publicly visible by default.
peer_visibility.insert(0);
Expand Down
1 change: 1 addition & 0 deletions modules/multiplayer/multiplayer_synchronizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class MultiplayerSynchronizer : public Node {

List<Variant> get_delta_state(uint64_t p_cur_usec, uint64_t p_last_usec, uint64_t &r_indexes);
List<NodePath> get_delta_properties(uint64_t p_indexes);
SceneReplicationConfig *get_replication_config_ptr() const;

MultiplayerSynchronizer();
};
Expand Down
16 changes: 8 additions & 8 deletions modules/multiplayer/scene_replication_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ Error SceneReplicationInterface::on_replication_start(Object *p_obj, Variant p_c

// Try to apply spawn state (before ready).
if (pending_buffer_size > 0) {
ERR_FAIL_COND_V(!node || sync->get_replication_config().is_null(), ERR_UNCONFIGURED);
ERR_FAIL_COND_V(!node || !sync->get_replication_config_ptr(), ERR_UNCONFIGURED);
int consumed = 0;
const List<NodePath> props = sync->get_replication_config()->get_spawn_properties();
const List<NodePath> props = sync->get_replication_config_ptr()->get_spawn_properties();
Vector<Variant> vars;
vars.resize(props.size());
Error err = MultiplayerAPI::decode_and_decompress_variants(vars, pending_buffer, pending_buffer_size, consumed);
Expand Down Expand Up @@ -498,8 +498,8 @@ Error SceneReplicationInterface::_make_spawn_packet(Node *p_node, MultiplayerSpa
continue;
}
ERR_CONTINUE(!sync);
ERR_FAIL_COND_V(sync->get_replication_config().is_null(), ERR_BUG);
for (const NodePath &prop : sync->get_replication_config()->get_spawn_properties()) {
ERR_FAIL_NULL_V(sync->get_replication_config_ptr(), ERR_BUG);
for (const NodePath &prop : sync->get_replication_config_ptr()->get_spawn_properties()) {
state_props.push_back(prop);
}
// Ensure the synchronizer has an ID.
Expand Down Expand Up @@ -714,7 +714,7 @@ void SceneReplicationInterface::_send_delta(int p_peer, const HashSet<ObjectID>
int ofs = 1;
for (const ObjectID &oid : p_synchronizers) {
MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(oid);
ERR_CONTINUE(!sync || !sync->get_replication_config().is_valid() || !_has_authority(sync));
ERR_CONTINUE(!sync || !sync->get_replication_config_ptr() || !_has_authority(sync));
uint32_t net_id;
if (!_verify_synchronizer(p_peer, sync, net_id)) {
continue;
Expand Down Expand Up @@ -809,7 +809,7 @@ void SceneReplicationInterface::_send_sync(int p_peer, const HashSet<ObjectID> p
// This is a lazy implementation, we could optimize much more here with by grouping by replication config.
for (const ObjectID &oid : p_synchronizers) {
MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(oid);
ERR_CONTINUE(!sync || !sync->get_replication_config().is_valid() || !_has_authority(sync));
ERR_CONTINUE(!sync || !sync->get_replication_config_ptr() || !_has_authority(sync));
if (!sync->update_outbound_sync_time(p_usec)) {
continue; // nothing to sync.
}
Expand All @@ -824,7 +824,7 @@ void SceneReplicationInterface::_send_sync(int p_peer, const HashSet<ObjectID> p
int size;
Vector<Variant> vars;
Vector<const Variant *> varp;
const List<NodePath> props = sync->get_replication_config()->get_sync_properties();
const List<NodePath> props = sync->get_replication_config_ptr()->get_sync_properties();
Error err = MultiplayerSynchronizer::get_state(props, node, vars, varp);
ERR_CONTINUE_MSG(err != OK, "Unable to retrieve sync state.");
err = MultiplayerAPI::encode_and_compress_variants(varp.ptrw(), varp.size(), nullptr, size);
Expand Down Expand Up @@ -883,7 +883,7 @@ Error SceneReplicationInterface::on_sync_receive(int p_from, const uint8_t *p_bu
ofs += size;
continue;
}
const List<NodePath> props = sync->get_replication_config()->get_sync_properties();
const List<NodePath> props = sync->get_replication_config_ptr()->get_sync_properties();
Vector<Variant> vars;
vars.resize(props.size());
int consumed;
Expand Down

0 comments on commit 5df7577

Please sign in to comment.