Skip to content

Commit 0ed1c19

Browse files
committed
Merge pull request #105565 from smix8/gridmap_list
Replace GridMap legacy use of `List` with `LocalVector`
2 parents c928676 + aab8dd6 commit 0ed1c19

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

modules/gridmap/editor/grid_map_editor_plugin.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,8 @@ EditorPlugin::AfterGUIInput GridMapEditor::forward_spatial_input_event(Camera3D
815815
for (const SetItem &si : set_items) {
816816
undo_redo->add_do_method(node, "set_cell_item", si.position, si.new_value, si.new_orientation);
817817
}
818-
for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
819-
const SetItem &si = E->get();
818+
for (uint32_t i = set_items.size(); i > 0; i--) {
819+
const SetItem &si = set_items[i - 1];
820820
undo_redo->add_undo_method(node, "set_cell_item", si.position, si.old_value, si.old_orientation);
821821
}
822822

@@ -1387,6 +1387,8 @@ GridMapEditor::GridMapEditor() {
13871387
toolbar->add_child(mode_buttons);
13881388
mode_buttons_group.instantiate();
13891389

1390+
viewport_shortcut_buttons.reserve(12);
1391+
13901392
transform_mode_button = memnew(Button);
13911393
transform_mode_button->set_theme_type_variation(SceneStringName(FlatButton));
13921394
transform_mode_button->set_toggle_mode(true);

modules/gridmap/editor/grid_map_editor_plugin.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class GridMapEditor : public VBoxContainer {
7474
double accumulated_floor_delta = 0.0;
7575

7676
HBoxContainer *toolbar = nullptr;
77-
List<BaseButton *> viewport_shortcut_buttons;
77+
TightLocalVector<BaseButton *> viewport_shortcut_buttons;
7878
Ref<ButtonGroup> mode_buttons_group;
7979
// mode
8080
Button *transform_mode_button = nullptr;
@@ -110,7 +110,7 @@ class GridMapEditor : public VBoxContainer {
110110
int old_orientation = 0;
111111
};
112112

113-
List<SetItem> set_items;
113+
LocalVector<SetItem> set_items;
114114

115115
GridMap *node = nullptr;
116116
Ref<MeshLibrary> mesh_library = nullptr;
@@ -139,7 +139,7 @@ class GridMapEditor : public VBoxContainer {
139139
RID instance;
140140
};
141141

142-
List<ClipboardItem> clipboard_items;
142+
LocalVector<ClipboardItem> clipboard_items;
143143

144144
Color default_color;
145145
Color erase_color;

modules/gridmap/grid_map.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/io/marshalls.h"
3434
#include "core/math/convex_hull.h"
35+
#include "core/templates/a_hash_map.h"
3536
#include "scene/resources/3d/box_shape_3d.h"
3637
#include "scene/resources/3d/capsule_shape_3d.h"
3738
#include "scene/resources/3d/concave_polygon_shape_3d.h"
@@ -644,7 +645,11 @@ bool GridMap::_octant_update(const OctantKey &p_key) {
644645
* and set said multimesh bounding box to one containing all cells which have this item
645646
*/
646647

647-
HashMap<int, List<Pair<Transform3D, IndexKey>>> multimesh_items;
648+
struct MultiMeshItemPlacement {
649+
Transform3D transform;
650+
IndexKey index_key;
651+
};
652+
AHashMap<int, LocalVector<MultiMeshItemPlacement>> item_id_to_multimesh_item_placements;
648653

649654
RID scenario;
650655
#ifndef NAVIGATION_3D_DISABLED
@@ -676,14 +681,14 @@ bool GridMap::_octant_update(const OctantKey &p_key) {
676681
xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
677682
if (baked_meshes.is_empty()) {
678683
if (mesh_library->get_item_mesh(c.item).is_valid()) {
679-
if (!multimesh_items.has(c.item)) {
680-
multimesh_items[c.item] = List<Pair<Transform3D, IndexKey>>();
684+
if (!item_id_to_multimesh_item_placements.has(c.item)) {
685+
item_id_to_multimesh_item_placements[c.item] = LocalVector<MultiMeshItemPlacement>();
681686
}
682687

683-
Pair<Transform3D, IndexKey> p;
684-
p.first = xform * mesh_library->get_item_mesh_transform(c.item);
685-
p.second = E;
686-
multimesh_items[c.item].push_back(p);
688+
MultiMeshItemPlacement p;
689+
p.transform = xform * mesh_library->get_item_mesh_transform(c.item);
690+
p.index_key = E;
691+
item_id_to_multimesh_item_placements[c.item].push_back(p);
687692
}
688693
}
689694

@@ -754,22 +759,23 @@ bool GridMap::_octant_update(const OctantKey &p_key) {
754759

755760
//update multimeshes, only if not baked
756761
if (baked_meshes.is_empty()) {
757-
for (const KeyValue<int, List<Pair<Transform3D, IndexKey>>> &E : multimesh_items) {
762+
for (const KeyValue<int, LocalVector<MultiMeshItemPlacement>> &E : item_id_to_multimesh_item_placements) {
758763
Octant::MultimeshInstance mmi;
759764

760765
RID mm = RS::get_singleton()->multimesh_create();
761766
RS::get_singleton()->multimesh_allocate_data(mm, E.value.size(), RS::MULTIMESH_TRANSFORM_3D);
762767
RS::get_singleton()->multimesh_set_mesh(mm, mesh_library->get_item_mesh(E.key)->get_rid());
763768

764769
int idx = 0;
765-
for (const Pair<Transform3D, IndexKey> &F : E.value) {
766-
RS::get_singleton()->multimesh_instance_set_transform(mm, idx, F.first);
770+
const LocalVector<MultiMeshItemPlacement> &mm_item_placements = E.value;
771+
for (const MultiMeshItemPlacement &mm_item_placement : mm_item_placements) {
772+
RS::get_singleton()->multimesh_instance_set_transform(mm, idx, mm_item_placement.transform);
767773
#ifdef TOOLS_ENABLED
768774

769775
Octant::MultimeshInstance::Item it;
770776
it.index = idx;
771-
it.transform = F.first;
772-
it.key = F.second;
777+
it.transform = mm_item_placement.transform;
778+
it.key = mm_item_placement.index_key;
773779
mmi.items.push_back(it);
774780
#endif
775781

@@ -1131,17 +1137,19 @@ void GridMap::_update_octants_callback() {
11311137
return;
11321138
}
11331139

1134-
List<OctantKey> to_delete;
1140+
LocalVector<OctantKey> to_delete;
1141+
to_delete.reserve(octant_map.size());
11351142
for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
11361143
if (_octant_update(E.key)) {
11371144
to_delete.push_back(E.key);
11381145
}
11391146
}
11401147

1141-
while (to_delete.front()) {
1142-
memdelete(octant_map[to_delete.front()->get()]);
1143-
octant_map.erase(to_delete.front()->get());
1144-
to_delete.pop_front();
1148+
while (!to_delete.is_empty()) {
1149+
const OctantKey &octantkey = to_delete[0];
1150+
memdelete(octant_map[octantkey]);
1151+
octant_map.erase(octantkey);
1152+
to_delete.remove_at_unordered(0);
11451153
}
11461154

11471155
_update_visibility();

0 commit comments

Comments
 (0)