Skip to content

Commit

Permalink
[MP] Avoid unnecessary internal ref/unrefs
Browse files Browse the repository at this point in the history
Access the various internal components (cache/replicator) via pointer,
to avoid unnecessary overhead.
  • Loading branch information
Faless committed Oct 10, 2023
1 parent 9ce4239 commit 311a272
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
8 changes: 6 additions & 2 deletions modules/multiplayer/scene_multiplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,12 +680,16 @@ void SceneMultiplayer::_bind_methods() {

SceneMultiplayer::SceneMultiplayer() {
relay_buffer.instantiate();
replicator = Ref<SceneReplicationInterface>(memnew(SceneReplicationInterface(this)));
rpc = Ref<SceneRPCInterface>(memnew(SceneRPCInterface(this)));
cache = Ref<SceneCacheInterface>(memnew(SceneCacheInterface(this)));
replicator = Ref<SceneReplicationInterface>(memnew(SceneReplicationInterface(this, cache.ptr())));
rpc = Ref<SceneRPCInterface>(memnew(SceneRPCInterface(this, cache.ptr(), replicator.ptr())));
set_multiplayer_peer(Ref<OfflineMultiplayerPeer>(memnew(OfflineMultiplayerPeer)));
}

SceneMultiplayer::~SceneMultiplayer() {
clear();
// Ensure unref in reverse order for safety (we shouldn't use those pointers in the deconstructors anyway).
rpc.unref();
replicator.unref();
cache.unref();
}
3 changes: 0 additions & 3 deletions modules/multiplayer/scene_multiplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,6 @@ class SceneMultiplayer : public MultiplayerAPI {
void set_max_delta_packet_size(int p_size);
int get_max_delta_packet_size() const;

Ref<SceneCacheInterface> get_path_cache() { return cache; }
Ref<SceneReplicationInterface> get_replicator() { return replicator; }

SceneMultiplayer();
~SceneMultiplayer();
};
Expand Down
10 changes: 5 additions & 5 deletions modules/multiplayer/scene_replication_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ Error SceneReplicationInterface::_update_spawn_visibility(int p_peer, const Obje
for (int pid : to_spawn) {
ERR_CONTINUE(!peers_info.has(pid));
int path_id;
multiplayer->get_path_cache()->send_object_cache(spawner, pid, path_id);
multiplayer_cache->send_object_cache(spawner, pid, path_id);
_send_raw(packet_cache.ptr(), len, pid, true);
peers_info[pid].spawn_nodes.insert(p_oid);
}
Expand Down Expand Up @@ -519,7 +519,7 @@ Error SceneReplicationInterface::_make_spawn_packet(Node *p_node, MultiplayerSpa
}

// Encode scene ID, path ID, net ID, node name.
int path_id = multiplayer->get_path_cache()->make_object_cache(p_spawner);
int path_id = multiplayer_cache->make_object_cache(p_spawner);
CharString cname = p_node->get_name().operator String().utf8();
int nlen = encode_cstring(cname.get_data(), nullptr);
MAKE_ROOM(1 + 1 + 4 + 4 + 4 + 4 * sync_ids.size() + 4 + nlen + (is_custom ? 4 + spawn_arg_size : 0) + state_size);
Expand Down Expand Up @@ -573,7 +573,7 @@ Error SceneReplicationInterface::on_spawn_receive(int p_from, const uint8_t *p_b
ofs += 1;
uint32_t node_target = decode_uint32(&p_buffer[ofs]);
ofs += 4;
MultiplayerSpawner *spawner = Object::cast_to<MultiplayerSpawner>(multiplayer->get_path_cache()->get_cached_object(p_from, node_target));
MultiplayerSpawner *spawner = Object::cast_to<MultiplayerSpawner>(multiplayer_cache->get_cached_object(p_from, node_target));
ERR_FAIL_NULL_V(spawner, ERR_DOES_NOT_EXIST);
ERR_FAIL_COND_V(p_from != spawner->get_multiplayer_authority(), ERR_UNAUTHORIZED);

Expand Down Expand Up @@ -684,7 +684,7 @@ bool SceneReplicationInterface::_verify_synchronizer(int p_peer, MultiplayerSync
r_net_id = p_sync->get_net_id();
if (r_net_id == 0 || (r_net_id & 0x80000000)) {
int path_id = 0;
bool verified = multiplayer->get_path_cache()->send_object_cache(p_sync, p_peer, path_id);
bool verified = multiplayer_cache->send_object_cache(p_sync, p_peer, path_id);
ERR_FAIL_COND_V_MSG(path_id < 0, false, "This should never happen!");
if (r_net_id == 0) {
// First time path based ID.
Expand All @@ -699,7 +699,7 @@ bool SceneReplicationInterface::_verify_synchronizer(int p_peer, MultiplayerSync
MultiplayerSynchronizer *SceneReplicationInterface::_find_synchronizer(int p_peer, uint32_t p_net_id) {
MultiplayerSynchronizer *sync = nullptr;
if (p_net_id & 0x80000000) {
sync = Object::cast_to<MultiplayerSynchronizer>(multiplayer->get_path_cache()->get_cached_object(p_peer, p_net_id & 0x7FFFFFFF));
sync = Object::cast_to<MultiplayerSynchronizer>(multiplayer_cache->get_cached_object(p_peer, p_net_id & 0x7FFFFFFF));
} else if (peers_info[p_peer].recv_sync_ids.has(p_net_id)) {
const ObjectID &sid = peers_info[p_peer].recv_sync_ids[p_net_id];
sync = get_id_as<MultiplayerSynchronizer>(sid);
Expand Down
5 changes: 4 additions & 1 deletion modules/multiplayer/scene_replication_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "core/object/ref_counted.h"

class SceneMultiplayer;
class SceneCacheInterface;

class SceneReplicationInterface : public RefCounted {
GDCLASS(SceneReplicationInterface, RefCounted);
Expand Down Expand Up @@ -87,6 +88,7 @@ class SceneReplicationInterface : public RefCounted {

// Replicator config.
SceneMultiplayer *multiplayer = nullptr;
SceneCacheInterface *multiplayer_cache = nullptr;
PackedByteArray packet_cache;
int sync_mtu = 1350; // Highly dependent on underlying protocol.
int delta_mtu = 65535;
Expand Down Expand Up @@ -144,8 +146,9 @@ class SceneReplicationInterface : public RefCounted {
void set_max_delta_packet_size(int p_size);
int get_max_delta_packet_size() const;

SceneReplicationInterface(SceneMultiplayer *p_multiplayer) {
SceneReplicationInterface(SceneMultiplayer *p_multiplayer, SceneCacheInterface *p_cache) {
multiplayer = p_multiplayer;
multiplayer_cache = p_cache;
}
};

Expand Down
15 changes: 7 additions & 8 deletions modules/multiplayer/scene_rpc_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Node *SceneRPCInterface::_process_get_node(int p_from, const uint8_t *p_packet,
return node;
} else {
// Use cached path.
return Object::cast_to<Node>(multiplayer->get_path_cache()->get_cached_object(p_from, p_node_target));
return Object::cast_to<Node>(multiplayer_cache->get_cached_object(p_from, p_node_target));
}
}

Expand Down Expand Up @@ -309,25 +309,24 @@ void SceneRPCInterface::_send_rpc(Node *p_node, int p_to, uint16_t p_rpc_id, con

// See if all peers have cached path (if so, call can be fast) while building the RPC target list.
HashSet<int> targets;
Ref<SceneCacheInterface> cache = multiplayer->get_path_cache();
int psc_id = -1;
bool has_all_peers = true;
const ObjectID oid = p_node->get_instance_id();
if (p_to > 0) {
ERR_FAIL_COND_MSG(!multiplayer->get_replicator()->is_rpc_visible(oid, p_to), "Attempt to call an RPC to a peer that cannot see this node. Peer ID: " + itos(p_to));
ERR_FAIL_COND_MSG(!multiplayer_replicator->is_rpc_visible(oid, p_to), "Attempt to call an RPC to a peer that cannot see this node. Peer ID: " + itos(p_to));
targets.insert(p_to);
has_all_peers = cache->send_object_cache(p_node, p_to, psc_id);
has_all_peers = multiplayer_cache->send_object_cache(p_node, p_to, psc_id);
} else {
bool restricted = !multiplayer->get_replicator()->is_rpc_visible(oid, 0);
bool restricted = !multiplayer_replicator->is_rpc_visible(oid, 0);
for (const int &P : multiplayer->get_connected_peers()) {
if (p_to < 0 && P == -p_to) {
continue; // Excluded peer.
}
if (restricted && !multiplayer->get_replicator()->is_rpc_visible(oid, P)) {
if (restricted && !multiplayer_replicator->is_rpc_visible(oid, P)) {
continue; // Not visible to this peer.
}
targets.insert(P);
bool has_peer = cache->send_object_cache(p_node, P, psc_id);
bool has_peer = multiplayer_cache->send_object_cache(p_node, P, psc_id);
has_all_peers = has_all_peers && has_peer;
}
}
Expand Down Expand Up @@ -446,7 +445,7 @@ void SceneRPCInterface::_send_rpc(Node *p_node, int p_to, uint16_t p_rpc_id, con

// Not all verified path, so check which needs the longer packet.
for (const int P : targets) {
bool confirmed = multiplayer->get_path_cache()->is_cache_confirmed(p_node, P);
bool confirmed = multiplayer_cache->is_cache_confirmed(p_node, P);
if (confirmed) {
// This one confirmed path, so use id.
encode_uint32(psc_id, &(packet_cache.write[1]));
Expand Down
11 changes: 10 additions & 1 deletion modules/multiplayer/scene_rpc_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "scene/main/multiplayer_api.h"

class SceneMultiplayer;
class SceneCacheInterface;
class SceneReplicationInterface;
class Node;

class SceneRPCInterface : public RefCounted {
Expand Down Expand Up @@ -77,6 +79,9 @@ class SceneRPCInterface : public RefCounted {
};

SceneMultiplayer *multiplayer = nullptr;
SceneCacheInterface *multiplayer_cache = nullptr;
SceneReplicationInterface *multiplayer_replicator = nullptr;

Vector<uint8_t> packet_cache;

HashMap<ObjectID, RPCConfigCache> rpc_cache;
Expand All @@ -99,7 +104,11 @@ class SceneRPCInterface : public RefCounted {
void process_rpc(int p_from, const uint8_t *p_packet, int p_packet_len);
String get_rpc_md5(const Object *p_obj);

SceneRPCInterface(SceneMultiplayer *p_multiplayer) { multiplayer = p_multiplayer; }
SceneRPCInterface(SceneMultiplayer *p_multiplayer, SceneCacheInterface *p_cache, SceneReplicationInterface *p_replicator) {
multiplayer = p_multiplayer;
multiplayer_cache = p_cache;
multiplayer_replicator = p_replicator;
}
};

#endif // SCENE_RPC_INTERFACE_H

0 comments on commit 311a272

Please sign in to comment.