Skip to content

Commit a5d9659

Browse files
committed
Optimize animation performance and improve internals
1 parent 0fdbf05 commit a5d9659

40 files changed

+1232
-891
lines changed

core/string/node_path.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ bool NodePath::operator==(const NodePath &p_path) const {
117117
return false;
118118
}
119119

120+
if (data->hash_cache_valid && p_path.data->hash_cache_valid) {
121+
if (data->hash_cache != p_path.data->hash_cache) {
122+
return false;
123+
}
124+
}
125+
120126
if (data->absolute != p_path.data->absolute) {
121127
return false;
122128
}

core/templates/a_hash_map.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,22 @@ class AHashMap {
339339
return nullptr;
340340
}
341341

342+
TValue &get_value_ref_or_add_default(const TKey &p_key, bool &r_was_added) {
343+
uint32_t element_idx = 0;
344+
uint32_t meta_idx = 0;
345+
uint32_t hash = _hash(p_key);
346+
bool exists = _lookup_idx_with_hash(p_key, element_idx, meta_idx, hash);
347+
348+
if (exists) {
349+
r_was_added = false;
350+
return _elements[element_idx].value;
351+
} else {
352+
r_was_added = true;
353+
element_idx = _insert_element(p_key, TValue(), hash);
354+
return _elements[element_idx].value;
355+
}
356+
}
357+
342358
bool has(const TKey &p_key) const {
343359
uint32_t _idx = 0;
344360
uint32_t meta_idx = 0;
@@ -592,17 +608,8 @@ class AHashMap {
592608
}
593609

594610
TValue &operator[](const TKey &p_key) {
595-
uint32_t element_idx = 0;
596-
uint32_t meta_idx = 0;
597-
uint32_t hash = _hash(p_key);
598-
bool exists = _lookup_idx_with_hash(p_key, element_idx, meta_idx, hash);
599-
600-
if (exists) {
601-
return _elements[element_idx].value;
602-
} else {
603-
element_idx = _insert_element(p_key, TValue(), hash);
604-
return _elements[element_idx].value;
605-
}
611+
bool dummy;
612+
return get_value_ref_or_add_default(p_key, dummy);
606613
}
607614

608615
/* Insert */

editor/animation/animation_blend_space_1d_editor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEven
7777
animations_to_add.clear();
7878

7979
LocalVector<StringName> classes;
80-
ClassDB::get_inheriters_from_class("AnimationRootNode", classes);
80+
ClassDB::get_inheriters_from_class(SNAME("AnimationRootNode"), classes);
8181
classes.sort_custom<StringName::AlphCompare>();
8282

8383
menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);
8484

85-
List<StringName> names;
85+
LocalVector<StringName> names;
8686
tree->get_animation_list(&names);
8787

8888
for (const StringName &E : names) {

editor/animation/animation_blend_space_1d_editor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class AnimationNodeBlendSpace1DEditor : public AnimationTreeNodeEditorPlugin {
9494

9595
PopupMenu *menu = nullptr;
9696
PopupMenu *animations_menu = nullptr;
97-
Vector<String> animations_to_add;
97+
Vector<StringName> animations_to_add;
9898
float add_point_pos = 0.0f;
9999
Vector<real_t> points;
100100

editor/animation/animation_blend_space_2d_editor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void AnimationNodeBlendSpace2DEditor::_blend_space_gui_input(const Ref<InputEven
126126

127127
menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);
128128

129-
List<StringName> names;
129+
LocalVector<StringName> names;
130130
tree->get_animation_list(&names);
131131
for (const StringName &E : names) {
132132
animations_menu->add_icon_item(get_editor_theme_icon(SNAME("Animation")), E);

editor/animation/animation_blend_space_2d_editor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class AnimationNodeBlendSpace2DEditor : public AnimationTreeNodeEditorPlugin {
9999

100100
PopupMenu *menu = nullptr;
101101
PopupMenu *animations_menu = nullptr;
102-
Vector<String> animations_to_add;
102+
Vector<StringName> animations_to_add;
103103
Vector2 add_point_pos;
104104
Vector<Vector2> points;
105105

editor/animation/animation_blend_tree_editor_plugin.cpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ void AnimationNodeBlendTreeEditor::update_graph() {
119119
if (updating || blend_tree.is_null()) {
120120
return;
121121
}
122+
if (graph_update_queued) {
123+
return;
124+
}
125+
graph_update_queued = true;
126+
// Defer to idle time, so multiple requests can be merged.
127+
callable_mp(this, &AnimationNodeBlendTreeEditor::update_graph_immediately).call_deferred();
128+
}
129+
130+
void AnimationNodeBlendTreeEditor::update_graph_immediately() {
131+
if (updating || blend_tree.is_null()) {
132+
return;
133+
}
122134

123135
AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
124136
if (!tree) {
@@ -174,8 +186,8 @@ void AnimationNodeBlendTreeEditor::update_graph() {
174186
name->set_custom_minimum_size(Vector2(100, 0) * EDSCALE);
175187
node->add_child(name);
176188
node->set_slot(0, false, 0, Color(), true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")));
177-
name->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed).bind(agnode), CONNECT_DEFERRED);
178-
name->connect(SceneStringName(focus_exited), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed_focus_out).bind(agnode), CONNECT_DEFERRED);
189+
name->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed).bind(agnode, E), CONNECT_DEFERRED);
190+
name->connect(SceneStringName(focus_exited), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed_focus_out).bind(agnode, E), CONNECT_DEFERRED);
179191
name->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed), CONNECT_DEFERRED);
180192
base = 1;
181193
agnode->set_deletable(true);
@@ -198,7 +210,7 @@ void AnimationNodeBlendTreeEditor::update_graph() {
198210
node->set_slot(base + i, true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")), false, 0, Color());
199211
}
200212

201-
List<PropertyInfo> pinfo;
213+
LocalVector<PropertyInfo> pinfo;
202214
agnode->get_parameter_list(&pinfo);
203215
for (const PropertyInfo &F : pinfo) {
204216
if (!(F.usage & PROPERTY_USAGE_EDITOR)) {
@@ -218,7 +230,7 @@ void AnimationNodeBlendTreeEditor::update_graph() {
218230
}
219231
prop->set_name_split_ratio(ratio);
220232
prop->update_property();
221-
prop->connect("property_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_property_changed));
233+
prop->connect(SNAME("property_changed"), callable_mp(this, &AnimationNodeBlendTreeEditor::_property_changed));
222234

223235
if (F.hint == PROPERTY_HINT_RESOURCE_TYPE) {
224236
// Give the resource editor some more space to make the inside readable.
@@ -232,7 +244,7 @@ void AnimationNodeBlendTreeEditor::update_graph() {
232244
}
233245
}
234246

235-
node->connect("dragged", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_dragged).bind(E));
247+
node->connect(SNAME("dragged"), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_dragged).bind(E));
236248

237249
if (AnimationTreeEditor::get_singleton()->can_edit(agnode)) {
238250
node->add_child(memnew(HSeparator));
@@ -272,7 +284,7 @@ void AnimationNodeBlendTreeEditor::update_graph() {
272284

273285
ProgressBar *pb = memnew(ProgressBar);
274286

275-
List<StringName> anims;
287+
LocalVector<StringName> anims;
276288
tree->get_animation_list(&anims);
277289

278290
for (const StringName &F : anims) {
@@ -285,25 +297,25 @@ void AnimationNodeBlendTreeEditor::update_graph() {
285297
animations[E] = pb;
286298
node->add_child(pb);
287299

288-
mb->get_popup()->connect("index_pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_anim_selected).bind(options, E), CONNECT_DEFERRED);
300+
mb->get_popup()->connect(SNAME("index_pressed"), callable_mp(this, &AnimationNodeBlendTreeEditor::_anim_selected).bind(options, E), CONNECT_DEFERRED);
289301
}
290302

291-
Ref<StyleBox> sb_panel = node->get_theme_stylebox(SceneStringName(panel), "GraphNode")->duplicate();
303+
Ref<StyleBox> sb_panel = node->get_theme_stylebox(SceneStringName(panel), SNAME("GraphNode"))->duplicate();
292304
if (sb_panel.is_valid()) {
293305
sb_panel->set_content_margin(SIDE_TOP, 12 * EDSCALE);
294306
sb_panel->set_content_margin(SIDE_BOTTOM, 12 * EDSCALE);
295307
node->add_theme_style_override(SceneStringName(panel), sb_panel);
296308
}
297309

298-
node->add_theme_constant_override("separation", 4 * EDSCALE);
310+
node->add_theme_constant_override(SNAME("separation"), 4 * EDSCALE);
299311
}
300312

301313
List<AnimationNodeBlendTree::NodeConnection> node_connections;
302314
blend_tree->get_node_connections(&node_connections);
303315

304316
for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
305-
StringName from = E.output_node;
306-
StringName to = E.input_node;
317+
const StringName &from = E.output_node;
318+
const StringName &to = E.input_node;
307319
int to_idx = E.input_index;
308320

309321
graph->connect_node(from, 0, to, to_idx);
@@ -324,6 +336,8 @@ void AnimationNodeBlendTreeEditor::update_graph() {
324336
}
325337
}
326338
}
339+
340+
graph_update_queued = false;
327341
}
328342

329343
void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {
@@ -795,16 +809,16 @@ bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &ano
795809

796810
updating = true;
797811

798-
HashSet<String> paths;
799-
HashMap<String, RBSet<String>> types;
812+
HashSet<NodePath> paths;
813+
HashMap<NodePath, RBSet<String>> types;
800814
{
801-
List<StringName> animation_list;
815+
LocalVector<StringName> animation_list;
802816
tree->get_animation_list(&animation_list);
803817

804818
for (const StringName &E : animation_list) {
805819
Ref<Animation> anim = tree->get_animation(E);
806820
for (int i = 0; i < anim->get_track_count(); i++) {
807-
String track_path = String(anim->track_get_path(i));
821+
NodePath track_path = anim->track_get_path(i);
808822
paths.insert(track_path);
809823

810824
String track_type_name;
@@ -835,8 +849,7 @@ bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &ano
835849

836850
HashMap<String, TreeItem *> parenthood;
837851

838-
for (const String &E : paths) {
839-
NodePath path = E;
852+
for (const NodePath &path : paths) {
840853
TreeItem *ti = nullptr;
841854
String accum;
842855
for (int i = 0; i < path.get_name_count(); i++) {
@@ -856,8 +869,8 @@ bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &ano
856869
ti->set_selectable(0, false);
857870
ti->set_editable(0, false);
858871

859-
if (base->has_node(accum)) {
860-
Node *node = base->get_node(accum);
872+
Node *node = base->get_node_or_null(accum);
873+
if (node) {
861874
ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node));
862875
}
863876

@@ -866,10 +879,7 @@ bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &ano
866879
}
867880
}
868881

869-
Node *node = nullptr;
870-
if (base->has_node(accum)) {
871-
node = base->get_node(accum);
872-
}
882+
Node *node = base->get_node_or_null(accum);
873883
if (!node) {
874884
continue; //no node, can't edit
875885
}
@@ -1007,6 +1017,10 @@ void AnimationNodeBlendTreeEditor::_notification(int p_what) {
10071017
return; // Node has been changed.
10081018
}
10091019

1020+
if (graph_update_queued) {
1021+
return;
1022+
}
1023+
10101024
String error;
10111025

10121026
error = tree->get_editor_error_message();
@@ -1094,7 +1108,7 @@ void AnimationNodeBlendTreeEditor::_node_changed(const StringName &p_node_name)
10941108
update_graph();
10951109
}
10961110

1097-
void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node) {
1111+
void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node, const StringName p_name) {
10981112
if (blend_tree.is_null()) {
10991113
return;
11001114
}
@@ -1104,7 +1118,7 @@ void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<Anima
11041118
return;
11051119
}
11061120

1107-
String prev_name = blend_tree->get_node_name(p_node);
1121+
String prev_name = p_name;
11081122
ERR_FAIL_COND(prev_name.is_empty());
11091123
GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(prev_name));
11101124
ERR_FAIL_NULL(gn);
@@ -1175,11 +1189,11 @@ void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<Anima
11751189
current_node_rename_text = String();
11761190
}
11771191

1178-
void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Ref<AnimationNode> p_node) {
1192+
void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Ref<AnimationNode> p_node, const StringName p_name) {
11791193
if (current_node_rename_text.is_empty()) {
11801194
return; // The text_submitted signal triggered the graph update and freed the LineEdit.
11811195
}
1182-
_node_renamed(current_node_rename_text, p_node);
1196+
_node_renamed(current_node_rename_text, p_node, p_name);
11831197
}
11841198

11851199
void AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed(const String &p_text) {

editor/animation/animation_blend_tree_editor_plugin.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ class AnimationNodeBlendTreeEditor : public AnimationTreeNodeEditorPlugin {
9898
static AnimationNodeBlendTreeEditor *singleton;
9999

100100
void _node_dragged(const Vector2 &p_from, const Vector2 &p_to, const StringName &p_which);
101-
void _node_renamed(const String &p_text, Ref<AnimationNode> p_node);
102-
void _node_renamed_focus_out(Ref<AnimationNode> p_node);
101+
void _node_renamed(const String &p_text, Ref<AnimationNode> p_node, StringName p_name);
102+
void _node_renamed_focus_out(Ref<AnimationNode> p_node, StringName p_name);
103103
void _node_rename_lineedit_changed(const String &p_text);
104104
void _node_changed(const StringName &p_node_name);
105105

106106
String current_node_rename_text;
107+
bool graph_update_queued;
107108
bool updating;
108109

109110
void _connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index);
@@ -167,6 +168,7 @@ class AnimationNodeBlendTreeEditor : public AnimationTreeNodeEditorPlugin {
167168
virtual void edit(const Ref<AnimationNode> &p_node) override;
168169

169170
void update_graph();
171+
void update_graph_immediately();
170172

171173
AnimationNodeBlendTreeEditor();
172174
};

editor/animation/animation_library_editor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ void AnimationLibraryEditor::_load_files(const PackedStringArray &p_paths) {
374374
continue;
375375
}
376376

377-
List<StringName> libs;
377+
LocalVector<StringName> libs;
378378
mixer->get_animation_library_list(&libs);
379379
bool is_already_added = false;
380380
for (const StringName &K : libs) {
@@ -684,7 +684,7 @@ void AnimationLibraryEditor::update_tree() {
684684
Color ss_color = get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));
685685

686686
TreeItem *root = tree->create_item();
687-
List<StringName> libs;
687+
LocalVector<StringName> libs;
688688
Vector<uint64_t> collapsed_lib_ids = _load_mixer_libs_folding();
689689

690690
mixer->get_animation_library_list(&libs);
@@ -951,7 +951,7 @@ String AnimationLibraryEditor::_get_mixer_signature() const {
951951
String signature = String();
952952

953953
// Get all libraries sorted for consistency
954-
List<StringName> libs;
954+
LocalVector<StringName> libs;
955955
mixer->get_animation_library_list(&libs);
956956
libs.sort_custom<StringName::AlphCompare>();
957957

editor/animation/animation_player_editor_plugin.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,9 @@ void AnimationPlayerEditor::_update_animation_blend() {
811811

812812
blend_editor.tree->clear();
813813

814-
String current = animation->get_item_text(animation->get_selected());
814+
StringName current = animation->get_item_text(animation->get_selected());
815815

816-
List<StringName> anims;
816+
LocalVector<StringName> anims;
817817
player->get_animation_list(&anims);
818818
TreeItem *root = blend_editor.tree->create_item();
819819
updating_blends = true;
@@ -1020,7 +1020,7 @@ void AnimationPlayerEditor::_update_player() {
10201020
return;
10211021
}
10221022

1023-
List<StringName> libraries;
1023+
LocalVector<StringName> libraries;
10241024
player->get_animation_library_list(&libraries);
10251025

10261026
int active_idx = -1;
@@ -1144,7 +1144,7 @@ void AnimationPlayerEditor::_update_name_dialog_library_dropdown() {
11441144
}
11451145
}
11461146

1147-
List<StringName> libraries;
1147+
LocalVector<StringName> libraries;
11481148
player->get_animation_library_list(&libraries);
11491149
library->clear();
11501150

@@ -1478,7 +1478,7 @@ void AnimationPlayerEditor::_current_animation_changed(const StringName &p_name)
14781478
}
14791479

14801480
// Determine the read-only status of the animation's library and the libraries as a whole.
1481-
List<StringName> libraries;
1481+
LocalVector<StringName> libraries;
14821482
player->get_animation_library_list(&libraries);
14831483

14841484
bool current_animation_library_is_readonly = false;

0 commit comments

Comments
 (0)