Data structure optimizations as per review.

This commit is contained in:
Fabio Alessandrelli 2021-08-26 13:35:33 +01:00
parent b05cb0fd7d
commit 2d2855cd53
2 changed files with 9 additions and 3 deletions

View File

@ -341,8 +341,10 @@ void MultiplayerReplicator::process_sync(int p_from, const uint8_t *p_packet, in
if (cfg.on_sync_receive.is_valid()) { if (cfg.on_sync_receive.is_valid()) {
Array objs; Array objs;
if (tracked_objects.has(id)) { if (tracked_objects.has(id)) {
objs.resize(tracked_objects[id].size());
int idx = 0;
for (const ObjectID &obj_id : tracked_objects[id]) { 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; 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()) { if (cfg.on_sync_send.is_valid()) {
Array objs; Array objs;
if (tracked_objects.has(p_scene_id)) { 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]) { 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 }; Variant args[3] = { p_scene_id, objs, p_peer };

View File

@ -32,6 +32,8 @@
#define MULTIPLAYER_REPLICATOR_H #define MULTIPLAYER_REPLICATOR_H
#include "core/io/multiplayer_api.h" #include "core/io/multiplayer_api.h"
#include "core/templates/hash_map.h"
#include "core/variant/typed_array.h" #include "core/variant/typed_array.h"
class MultiplayerReplicator : public Object { class MultiplayerReplicator : public Object {
@ -70,7 +72,7 @@ private:
Vector<uint8_t> packet_cache; Vector<uint8_t> packet_cache;
Map<ResourceUID::ID, SceneConfig> replications; Map<ResourceUID::ID, SceneConfig> replications;
Map<ObjectID, ResourceUID::ID> replicated_nodes; Map<ObjectID, ResourceUID::ID> replicated_nodes;
Map<ResourceUID::ID, List<ObjectID>> tracked_objects; HashMap<ResourceUID::ID, List<ObjectID>> tracked_objects;
// Encoding // Encoding
Error _get_state(const List<StringName> &p_properties, const Object *p_obj, List<Variant> &r_variant); Error _get_state(const List<StringName> &p_properties, const Object *p_obj, List<Variant> &r_variant);