From 2d2855cd53a6818e258e8ddebc38049fd87726ee Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Thu, 26 Aug 2021 13:35:33 +0100 Subject: [PATCH] Data structure optimizations as per review. --- core/io/multiplayer_replicator.cpp | 8 ++++++-- core/io/multiplayer_replicator.h | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/core/io/multiplayer_replicator.cpp b/core/io/multiplayer_replicator.cpp index 87c3cc4abcf..1642aab1365 100644 --- a/core/io/multiplayer_replicator.cpp +++ b/core/io/multiplayer_replicator.cpp @@ -341,8 +341,10 @@ void MultiplayerReplicator::process_sync(int p_from, const uint8_t *p_packet, in if (cfg.on_sync_receive.is_valid()) { Array objs; if (tracked_objects.has(id)) { + objs.resize(tracked_objects[id].size()); + int idx = 0; for (const ObjectID &obj_id : tracked_objects[id]) { - objs.push_back(ObjectDB::get_instance(obj_id)); + objs[idx++] = ObjectDB::get_instance(obj_id); } } PackedByteArray pba; @@ -717,8 +719,10 @@ Error MultiplayerReplicator::sync_all(const ResourceUID::ID &p_scene_id, int p_p if (cfg.on_sync_send.is_valid()) { Array objs; if (tracked_objects.has(p_scene_id)) { + objs.resize(tracked_objects[p_scene_id].size()); + int idx = 0; for (const ObjectID &obj_id : tracked_objects[p_scene_id]) { - objs.push_back(ObjectDB::get_instance(obj_id)); + objs[idx++] = ObjectDB::get_instance(obj_id); } } Variant args[3] = { p_scene_id, objs, p_peer }; diff --git a/core/io/multiplayer_replicator.h b/core/io/multiplayer_replicator.h index 6d2618d3dfb..2630ad7a8a3 100644 --- a/core/io/multiplayer_replicator.h +++ b/core/io/multiplayer_replicator.h @@ -32,6 +32,8 @@ #define MULTIPLAYER_REPLICATOR_H #include "core/io/multiplayer_api.h" + +#include "core/templates/hash_map.h" #include "core/variant/typed_array.h" class MultiplayerReplicator : public Object { @@ -70,7 +72,7 @@ private: Vector packet_cache; Map replications; Map replicated_nodes; - Map> tracked_objects; + HashMap> tracked_objects; // Encoding Error _get_state(const List &p_properties, const Object *p_obj, List &r_variant);