Skip to content

Commit f648eea

Browse files
committed
Merge pull request godotengine#106209 from YYF233333/opt_get_actions
Optimize `InputMap::get_actions`
2 parents 93140c0 + 55a61cb commit f648eea

File tree

3 files changed

+13
-29
lines changed

3 files changed

+13
-29
lines changed

core/input/input_map.cpp

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
void InputMap::_bind_methods() {
4141
ClassDB::bind_method(D_METHOD("has_action", "action"), &InputMap::has_action);
42-
ClassDB::bind_method(D_METHOD("get_actions"), &InputMap::_get_actions);
42+
ClassDB::bind_method(D_METHOD("get_actions"), &InputMap::get_actions);
4343
ClassDB::bind_method(D_METHOD("add_action", "action", "deadzone"), &InputMap::add_action, DEFVAL(DEFAULT_DEADZONE));
4444
ClassDB::bind_method(D_METHOD("erase_action", "action"), &InputMap::erase_action);
4545

@@ -61,16 +61,15 @@ void InputMap::_bind_methods() {
6161
* matching action name (if possible).
6262
*/
6363
String InputMap::suggest_actions(const StringName &p_action) const {
64-
List<StringName> actions = get_actions();
6564
StringName closest_action;
6665
float closest_similarity = 0.0;
6766

6867
// Find the most action with the most similar name.
69-
for (const StringName &action : actions) {
70-
const float similarity = String(action).similarity(p_action);
68+
for (const KeyValue<StringName, Action> &kv : input_map) {
69+
const float similarity = String(kv.key).similarity(p_action);
7170

7271
if (similarity > closest_similarity) {
73-
closest_action = action;
72+
closest_action = kv.key;
7473
closest_similarity = similarity;
7574
}
7675
}
@@ -128,31 +127,18 @@ void InputMap::erase_action(const StringName &p_action) {
128127
input_map.erase(p_action);
129128
}
130129

131-
TypedArray<StringName> InputMap::_get_actions() {
130+
TypedArray<StringName> InputMap::get_actions() {
132131
TypedArray<StringName> ret;
133-
List<StringName> actions = get_actions();
134-
if (actions.is_empty()) {
135-
return ret;
136-
}
137-
138-
for (const StringName &E : actions) {
139-
ret.push_back(E);
140-
}
141132

142-
return ret;
143-
}
133+
ret.resize(input_map.size());
144134

145-
List<StringName> InputMap::get_actions() const {
146-
List<StringName> actions = List<StringName>();
147-
if (input_map.is_empty()) {
148-
return actions;
149-
}
150-
151-
for (const KeyValue<StringName, Action> &E : input_map) {
152-
actions.push_back(E.key);
135+
uint32_t i = 0;
136+
for (const KeyValue<StringName, Action> &kv : input_map) {
137+
ret[i] = kv.key;
138+
i++;
153139
}
154140

155-
return actions;
141+
return ret;
156142
}
157143

158144
List<Ref<InputEvent>>::Element *InputMap::_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool p_exact_match, bool *r_pressed, float *r_strength, float *r_raw_strength, int *r_event_index) const {

core/input/input_map.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#pragma once
3232

3333
#include "core/input/input_event.h"
34-
#include "core/object/class_db.h"
3534
#include "core/object/object.h"
3635
#include "core/templates/hash_map.h"
3736

@@ -67,7 +66,6 @@ class InputMap : public Object {
6766
List<Ref<InputEvent>>::Element *_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool p_exact_match = false, bool *r_pressed = nullptr, float *r_strength = nullptr, float *r_raw_strength = nullptr, int *r_event_index = nullptr) const;
6867

6968
TypedArray<InputEvent> _action_get_events(const StringName &p_action);
70-
TypedArray<StringName> _get_actions();
7169

7270
protected:
7371
static void _bind_methods();
@@ -81,7 +79,7 @@ class InputMap : public Object {
8179
static _FORCE_INLINE_ InputMap *get_singleton() { return singleton; }
8280

8381
bool has_action(const StringName &p_action) const;
84-
List<StringName> get_actions() const;
82+
TypedArray<StringName> get_actions();
8583
void add_action(const StringName &p_action, float p_deadzone = DEFAULT_DEADZONE);
8684
void erase_action(const StringName &p_action);
8785

editor/editor_settings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,7 @@ void EditorSettings::get_argument_options(const StringName &p_function, int p_id
21662166
r_options->push_back(section.quote());
21672167
}
21682168
} else if (pf == "set_builtin_action_override") {
2169-
for (const StringName &action : InputMap::get_singleton()->get_actions()) {
2169+
for (const Variant &action : InputMap::get_singleton()->get_actions()) {
21702170
r_options->push_back(String(action).quote());
21712171
}
21722172
}

0 commit comments

Comments
 (0)