Skip to content

Commit 97d3916

Browse files
authored
Merge pull request #108169 from lawnjelly/editor_get_cached
[3.x] Add `EditorSettings` `EDITOR_GET_CACHED`
2 parents 1a7d99e + 0718852 commit 97d3916

38 files changed

+153
-88
lines changed

core/os/input.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void Input::_bind_methods() {
140140
void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
141141
#ifdef TOOLS_ENABLED
142142

143-
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
143+
const String quote_style = EditorSettingsQuick::get_text_editor_completion_use_single_quotes() ? "'" : "\"";
144144

145145
String pf = p_function;
146146
if ((p_idx == 0 && (pf == "is_action_pressed" || pf == "action_press" || pf == "action_release" || pf == "is_action_just_pressed" || pf == "is_action_just_released" || pf == "get_action_strength" || pf == "get_action_raw_strength")) ||

editor/animation_track_editor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,14 +3458,14 @@ void AnimationTrackEditor::_query_insert(const InsertData &p_id) {
34583458
insert_confirm->popup_centered_minsize();
34593459
insert_query = true;
34603460
} else {
3461-
call_deferred("_insert_delay", reset_allowed && EDITOR_GET("editors/animation/default_create_reset_tracks"), all_bezier && EDITOR_GET("editors/animation/default_create_bezier_tracks"));
3461+
call_deferred("_insert_delay", reset_allowed && EDITOR_GET_CACHED(bool, "editors/animation/default_create_reset_tracks"), all_bezier && EDITOR_GET_CACHED(bool, "editors/animation/default_create_bezier_tracks"));
34623462
insert_queue = true;
34633463
}
34643464

34653465
} else {
34663466
if (!insert_query && !insert_queue) {
34673467
// Create Beziers wouldn't make sense in this case, where no tracks are being created
3468-
call_deferred("_insert_delay", reset_allowed && EDITOR_GET("editors/animation/default_create_reset_tracks"), false);
3468+
call_deferred("_insert_delay", reset_allowed && EDITOR_GET_CACHED(bool, "editors/animation/default_create_reset_tracks"), false);
34693469
insert_queue = true;
34703470
}
34713471
}

editor/code_editor.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,17 +1507,17 @@ void CodeTextEditor::_on_settings_change() {
15071507

15081508
// Auto brace completion.
15091509
text_editor->set_auto_brace_completion(
1510-
EDITOR_GET("text_editor/completion/auto_brace_complete"));
1510+
EDITOR_GET_CACHED(bool, "text_editor/completion/auto_brace_complete"));
15111511

15121512
code_complete_timer->set_wait_time(
1513-
EDITOR_GET("text_editor/completion/code_complete_delay"));
1513+
EDITOR_GET_CACHED(float, "text_editor/completion/code_complete_delay"));
15141514

15151515
// Call hint settings.
15161516
text_editor->set_callhint_settings(
1517-
EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"),
1518-
EDITOR_GET("text_editor/completion/callhint_tooltip_offset"));
1517+
EDITOR_GET_CACHED(bool, "text_editor/completion/put_callhint_tooltip_below_current_line"),
1518+
EDITOR_GET_CACHED(Vector2, "text_editor/completion/callhint_tooltip_offset"));
15191519

1520-
idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
1520+
idle->set_wait_time(EDITOR_GET_CACHED(float, "text_editor/completion/idle_parse_delay"));
15211521
}
15221522

15231523
void CodeTextEditor::_text_changed_idle_timeout() {

editor/editor_node.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ void EditorNode::_notification(int p_what) {
464464

465465
editor_selection->update();
466466

467-
scene_root->set_size_override(true, Size2(ProjectSettings::get_singleton()->get("display/window/size/width"), ProjectSettings::get_singleton()->get("display/window/size/height")));
467+
scene_root->set_size_override(true, Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/width"), GLOBAL_GET_CACHED(real_t, "display/window/size/height")));
468468

469469
ResourceImporterTexture::get_singleton()->update_imports();
470470
} break;
@@ -532,20 +532,20 @@ void EditorNode::_notification(int p_what) {
532532

533533
case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
534534
// Restore the original FPS cap after focusing back on the editor.
535-
OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(int(EDITOR_GET("interface/editor/low_processor_mode_sleep_usec")));
535+
OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(EDITOR_GET_CACHED(int, "interface/editor/low_processor_mode_sleep_usec"));
536536

537537
EditorFileSystem::get_singleton()->scan_changes();
538538
_scan_external_changes();
539539
} break;
540540

541541
case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
542542
// Save on focus loss before applying the FPS limit to avoid slowing down the saving process.
543-
if (EDITOR_GET("interface/editor/save_on_focus_loss")) {
543+
if (EDITOR_GET_CACHED(bool, "interface/editor/save_on_focus_loss")) {
544544
_menu_option_confirm(FILE_SAVE_SCENE, false);
545545
}
546546

547547
// Set a low FPS cap to decrease CPU/GPU usage while the editor is unfocused.
548-
OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(int(EDITOR_GET("interface/editor/unfocused_low_processor_mode_sleep_usec")));
548+
OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(EDITOR_GET_CACHED(int, "interface/editor/unfocused_low_processor_mode_sleep_usec"));
549549
} break;
550550

551551
case MainLoop::NOTIFICATION_WM_ABOUT: {
@@ -2671,7 +2671,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
26712671
play_custom_scene_button->set_icon(gui_base->get_icon("PlayCustom", "EditorIcons"));
26722672
stop_button->set_disabled(true);
26732673

2674-
if (bool(EDITOR_GET("run/output/always_close_output_on_stop"))) {
2674+
if (EDITOR_GET_CACHED(bool, "run/output/always_close_output_on_stop")) {
26752675
for (int i = 0; i < bottom_panel_items.size(); i++) {
26762676
if (bottom_panel_items[i].control == log) {
26772677
_bottom_panel_switch(false, i);
@@ -2695,7 +2695,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
26952695

26962696
} break;
26972697
case RUN_PLAY_NATIVE: {
2698-
bool autosave = EDITOR_GET("run/auto_save/save_before_running");
2698+
bool autosave = EDITOR_GET_CACHED(bool, "run/auto_save/save_before_running");
26992699
if (autosave) {
27002700
_menu_option_confirm(FILE_SAVE_ALL_SCENES, false);
27012701
}
@@ -5006,7 +5006,7 @@ void EditorNode::_scene_tab_closed(int p_tab, int option) {
50065006
}
50075007

50085008
void EditorNode::_scene_tab_hover(int p_tab) {
5009-
if (!bool(EDITOR_GET("interface/scene_tabs/show_thumbnail_on_hover"))) {
5009+
if (!EDITOR_GET_CACHED(bool, "interface/scene_tabs/show_thumbnail_on_hover")) {
50105010
return;
50115011
}
50125012
int current_tab = scene_tabs->get_current_tab();

editor/editor_properties.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ void EditorPropertyVector2::setup(double p_min, double p_max, double p_step, boo
15991599
}
16001600

16011601
EditorPropertyVector2::EditorPropertyVector2() {
1602-
bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector2_editing");
1602+
bool horizontal = EDITOR_GET_CACHED(bool, "interface/inspector/horizontal_vector2_editing");
16031603

16041604
HBoxContainer *hb = memnew(HBoxContainer);
16051605
hb->set_h_size_flags(SIZE_EXPAND_FILL);
@@ -1693,7 +1693,7 @@ void EditorPropertyRect2::setup(double p_min, double p_max, double p_step, bool
16931693
}
16941694

16951695
EditorPropertyRect2::EditorPropertyRect2() {
1696-
bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing");
1696+
bool horizontal = EDITOR_GET_CACHED(bool, "interface/inspector/horizontal_vector_types_editing");
16971697

16981698
BoxContainer *bc;
16991699

@@ -1830,7 +1830,7 @@ void EditorPropertyVector3::setup(double p_min, double p_max, double p_step, boo
18301830
}
18311831

18321832
EditorPropertyVector3::EditorPropertyVector3() {
1833-
bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing");
1833+
bool horizontal = EDITOR_GET_CACHED(bool, "interface/inspector/horizontal_vector_types_editing");
18341834

18351835
HBoxContainer *hb = memnew(HBoxContainer);
18361836
hb->set_h_size_flags(SIZE_EXPAND_FILL);
@@ -1925,7 +1925,7 @@ void EditorPropertyPlane::setup(double p_min, double p_max, double p_step, bool
19251925
}
19261926

19271927
EditorPropertyPlane::EditorPropertyPlane() {
1928-
bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing");
1928+
bool horizontal = EDITOR_GET_CACHED(bool, "interface/inspector/horizontal_vector_types_editing");
19291929

19301930
BoxContainer *bc;
19311931

@@ -2007,7 +2007,7 @@ void EditorPropertyQuat::setup(double p_min, double p_max, double p_step, bool p
20072007
}
20082008

20092009
EditorPropertyQuat::EditorPropertyQuat() {
2010-
bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing");
2010+
bool horizontal = EDITOR_GET_CACHED(bool, "interface/inspector/horizontal_vector_types_editing");
20112011

20122012
BoxContainer *bc;
20132013

@@ -2373,7 +2373,7 @@ void EditorPropertyColor::_popup_closed() {
23732373

23742374
void EditorPropertyColor::_picker_created() {
23752375
// get default color picker mode from editor settings
2376-
int default_color_mode = EDITOR_GET("interface/inspector/default_color_picker_mode");
2376+
int default_color_mode = EDITOR_GET_CACHED(int, "interface/inspector/default_color_picker_mode");
23772377
if (default_color_mode == 1) {
23782378
picker->get_picker()->set_hsv_mode(true);
23792379
} else if (default_color_mode == 2) {
@@ -2920,7 +2920,7 @@ void EditorPropertyResource::_bind_methods() {
29202920
}
29212921

29222922
EditorPropertyResource::EditorPropertyResource() {
2923-
use_sub_inspector = bool(EDITOR_GET("interface/inspector/open_resources_in_current_inspector"));
2923+
use_sub_inspector = EDITOR_GET_CACHED(bool, "interface/inspector/open_resources_in_current_inspector");
29242924

29252925
add_to_group("_editor_resource_properties");
29262926
}
@@ -2936,7 +2936,7 @@ void EditorInspectorDefaultPlugin::parse_begin(Object *p_object) {
29362936
}
29372937

29382938
bool EditorInspectorDefaultPlugin::parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage) {
2939-
double default_float_step = EDITOR_GET("interface/inspector/default_float_step");
2939+
double default_float_step = EDITOR_GET_CACHED(double, "interface/inspector/default_float_step");
29402940

29412941
switch (p_type) {
29422942
// atomic types

editor/editor_properties_array_dict.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ void EditorPropertyArray::_bind_methods() {
702702

703703
EditorPropertyArray::EditorPropertyArray() {
704704
object.instance();
705-
page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
705+
page_length = EDITOR_GET_CACHED(int, "interface/inspector/max_array_dictionary_items_per_page");
706706
edit = memnew(Button);
707707
edit->set_flat(true);
708708
edit->set_h_size_flags(SIZE_EXPAND_FILL);
@@ -1190,7 +1190,7 @@ void EditorPropertyDictionary::_bind_methods() {
11901190

11911191
EditorPropertyDictionary::EditorPropertyDictionary() {
11921192
object.instance();
1193-
page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
1193+
page_length = EDITOR_GET_CACHED(int, "interface/inspector/max_array_dictionary_items_per_page");
11941194
edit = memnew(Button);
11951195
edit->set_flat(true);
11961196
edit->set_h_size_flags(SIZE_EXPAND_FILL);

editor/editor_property_name_processor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535
EditorPropertyNameProcessor *EditorPropertyNameProcessor::singleton = nullptr;
3636

3737
EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_default_inspector_style() {
38-
const Style style = (Style)EDITOR_GET("interface/inspector/default_property_name_style").operator int();
38+
const Style style = (Style)EDITOR_GET_CACHED(int, "interface/inspector/default_property_name_style");
3939
if (style == STYLE_LOCALIZED && !is_localization_available()) {
4040
return STYLE_CAPITALIZED;
4141
}
4242
return style;
4343
}
4444

4545
EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_settings_style() {
46-
const bool translate = EDITOR_GET("interface/editor/localize_settings");
46+
const bool translate = EDITOR_GET_CACHED(bool, "interface/editor/localize_settings");
4747
return translate ? STYLE_LOCALIZED : STYLE_CAPITALIZED;
4848
}
4949

editor/editor_settings.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
#include "scene/main/scene_tree.h"
4848
#include "scene/main/viewport.h"
4949

50+
EditorSettingsQuick::Data EditorSettingsQuick::data = EditorSettingsQuick::Data();
51+
52+
void EditorSettingsQuick::refresh() {
53+
data.text_editor_completion_use_single_quotes = EDITOR_GET_CACHED(bool, "text_editor/completion/use_single_quotes");
54+
}
55+
5056
// PRIVATE METHODS
5157

5258
Ref<EditorSettings> EditorSettings::singleton = nullptr;
@@ -108,6 +114,10 @@ bool EditorSettings::_set_only(const StringName &p_name, const Variant &p_value)
108114
}
109115
}
110116

117+
if (changed) {
118+
_version++;
119+
}
120+
111121
return changed;
112122
}
113123

@@ -1155,6 +1165,7 @@ void EditorSettings::set_initial_value(const StringName &p_setting, const Varian
11551165
}
11561166
props[p_setting].initial = p_value;
11571167
props[p_setting].has_default_value = true;
1168+
11581169
if (p_update_current) {
11591170
set(p_setting, p_value);
11601171
}
@@ -1605,6 +1616,8 @@ Ref<ShortCut> ED_SHORTCUT(const String &p_path, const String &p_name, uint32_t p
16051616
void EditorSettings::notify_changes() {
16061617
_THREAD_SAFE_METHOD_
16071618

1619+
EditorSettingsQuick::refresh();
1620+
16081621
SceneTree *sml = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
16091622

16101623
if (!sml) {

editor/editor_settings.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,37 @@
3434
#include "core/object.h"
3535

3636
#include "core/io/config_file.h"
37+
#include "core/os/spin_lock.h"
3738
#include "core/os/thread_safe.h"
3839
#include "core/resource.h"
3940
#include "core/translation.h"
4041
#include "scene/gui/shortcut.h"
4142

4243
class EditorPlugin;
4344

45+
// Fast access to frequently used settings.
46+
class EditorSettingsQuick {
47+
struct Data {
48+
bool text_editor_completion_use_single_quotes = false;
49+
};
50+
static Data data;
51+
52+
public:
53+
static bool get_text_editor_completion_use_single_quotes() { return data.text_editor_completion_use_single_quotes; }
54+
55+
static void refresh();
56+
};
57+
4458
class EditorSettings : public Resource {
4559
GDCLASS(EditorSettings, Resource);
4660

4761
private:
4862
_THREAD_SAFE_CLASS_
4963

64+
// Starting version from 1 ensures that all callers can reset their tested version to 0,
65+
// and will always detect the initial editor settings as a "change".
66+
uint32_t _version = 1;
67+
5068
public:
5169
struct Plugin {
5270
EditorPlugin *instance;
@@ -199,6 +217,9 @@ class EditorSettings : public Resource {
199217

200218
void notify_changes();
201219

220+
// Testing a version allows fast cached EDITOR_GET macros.
221+
uint32_t get_version() const { return _version; }
222+
202223
EditorSettings();
203224
~EditorSettings();
204225
};
@@ -216,4 +237,27 @@ Variant _EDITOR_GET(const String &p_setting);
216237
Ref<ShortCut> ED_SHORTCUT(const String &p_path, const String &p_name, uint32_t p_keycode = 0);
217238
Ref<ShortCut> ED_GET_SHORTCUT(const String &p_path);
218239

240+
/////////////////////////////////////////////////////////////////////////////////////////
241+
// Cached versions of EDITOR_GET.
242+
// Cached but uses a typed variable for storage, this can be more efficient.
243+
// Variables prefixed with _egc_ to avoid shadowing warnings.
244+
#define EDITOR_GET_CACHED(m_type, m_setting_name) ([](const char *p_egc_name) -> m_type {\
245+
static_assert(std::is_trivially_destructible<m_type>::value, "EDITOR_GET_CACHED must use a trivial type that allows static lifetime.");\
246+
static m_type _egc_local_var;\
247+
static uint32_t _egc_local_version = 0;\
248+
static SpinLock _egc_spin;\
249+
uint32_t _egc_new_version = EditorSettings::get_singleton()->get_version();\
250+
if (_egc_local_version != _egc_new_version) {\
251+
_egc_spin.lock();\
252+
_egc_local_version = _egc_new_version;\
253+
_egc_local_var = _EDITOR_GET(p_egc_name);\
254+
m_type _egc_temp = _egc_local_var;\
255+
_egc_spin.unlock();\
256+
return _egc_temp;\
257+
}\
258+
_egc_spin.lock();\
259+
m_type _egc_temp2 = _egc_local_var;\
260+
_egc_spin.unlock();\
261+
return _egc_temp2; })(m_setting_name)
262+
219263
#endif // EDITOR_SETTINGS_H

editor/editor_themes.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static Ref<ImageTexture> editor_generate_icon(int p_index, bool p_convert_color,
135135
#endif
136136

137137
float get_gizmo_handle_scale(const String &gizmo_handle_name = "") {
138-
const float scale_gizmo_handles_for_touch = EDITOR_GET("interface/touchscreen/scale_gizmo_handles");
138+
const float scale_gizmo_handles_for_touch = EDITOR_GET_CACHED(float, "interface/touchscreen/scale_gizmo_handles");
139139
if (scale_gizmo_handles_for_touch > 1.0f) {
140140
// The names of the icons that require custom scaling.
141141
static Set<StringName> gizmo_to_scale;
@@ -772,12 +772,14 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
772772
theme->set_icon("visibility_xray", "PopupMenu", theme->get_icon("GuiVisibilityXray", "EditorIcons"));
773773
theme->set_constant("vseparation", "PopupMenu", (extra_spacing + default_margin_size + 1) * EDSCALE);
774774

775+
float subresource_hue_tint = EDITOR_GET("docks/property_editor/subresource_hue_tint");
776+
775777
for (int i = 0; i < 16; i++) {
776778
Color si_base_color = accent_color;
777779

778780
float hue_rotate = (i * 2 % 16) / 16.0;
779781
si_base_color.set_hsv(Math::fmod(float(si_base_color.get_h() + hue_rotate), float(1.0)), si_base_color.get_s(), si_base_color.get_v());
780-
si_base_color = accent_color.linear_interpolate(si_base_color, float(EDITOR_GET("docks/property_editor/subresource_hue_tint")));
782+
si_base_color = accent_color.linear_interpolate(si_base_color, subresource_hue_tint);
781783

782784
Ref<StyleBoxFlat> sub_inspector_bg;
783785

0 commit comments

Comments
 (0)