Skip to content

Commit

Permalink
Improve Editor Inspector/Theme item lookup performance
Browse files Browse the repository at this point in the history
Changes to reduce the latency between changing node selection in the editor and seeing the new node reflected in the Inspector tab

- Use Vector instead of List for ThemeOwner::get_theme_type_dependencies and related functions
- Use Vector instead of List for ThemeContext::themes, set_themes(), and get_themes()
- Add ClassDB:get_inheritance_chain_nocheck to get all parent/ancestor classes at once, to avoid repeated ClassDB locking overhead
- Update BIND_THEME_ITEM macros and ThemeDB::update_class_instance_items to use provided StringNames for call to ThemeItemSetter, instead of creating a new StringName in each call

These changes reduce the time taken by EditorInspector::update_tree by around 30-35%
  • Loading branch information
aaronp64 committed Aug 20, 2024
1 parent 6b281c0 commit 7593e55
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 116 deletions.
23 changes: 23 additions & 0 deletions core/object/class_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,29 @@ StringName ClassDB::get_parent_class_nocheck(const StringName &p_class) {
return ti->inherits;
}

bool ClassDB::get_inheritance_chain_nocheck(const StringName &p_class, Vector<StringName> &r_result) {
OBJTYPE_RLOCK;

ClassInfo *start = classes.getptr(p_class);
if (!start) {
return false;
}

int classes_to_add = 0;
for (ClassInfo *ti = start; ti; ti = ti->inherits_ptr) {
classes_to_add++;
}

int64_t old_size = r_result.size();
r_result.resize(old_size + classes_to_add);
StringName *w = r_result.ptrw() + old_size;
for (ClassInfo *ti = start; ti; ti = ti->inherits_ptr) {
*w++ = ti->name;
}

return true;
}

StringName ClassDB::get_compatibility_remapped_class(const StringName &p_class) {
if (classes.has(p_class)) {
return p_class;
Expand Down
1 change: 1 addition & 0 deletions core/object/class_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ class ClassDB {
static void get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
static void get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
static StringName get_parent_class_nocheck(const StringName &p_class);
static bool get_inheritance_chain_nocheck(const StringName &p_class, Vector<StringName> &r_result);
static StringName get_parent_class(const StringName &p_class);
static StringName get_compatibility_remapped_class(const StringName &p_class);
static bool class_exists(const StringName &p_class);
Expand Down
4 changes: 2 additions & 2 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ void EditorNode::_update_theme(bool p_skip_creation) {
DisplayServer::set_early_window_clear_color_override(true, theme->get_color(SNAME("background"), EditorStringName(Editor)));
}

List<Ref<Theme>> editor_themes;
Vector<Ref<Theme>> editor_themes;
editor_themes.push_back(theme);
editor_themes.push_back(ThemeDB::get_singleton()->get_default_theme());

Expand Down Expand Up @@ -556,7 +556,7 @@ void EditorNode::update_preview_themes(int p_mode) {
return; // Too early.
}

List<Ref<Theme>> preview_themes;
Vector<Ref<Theme>> preview_themes;

switch (p_mode) {
case CanvasItemEditor::THEME_PREVIEW_PROJECT:
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/theme_editor_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void ThemeEditorPreview::_notification(int p_what) {
} break;

case NOTIFICATION_READY: {
List<Ref<Theme>> preview_themes;
Vector<Ref<Theme>> preview_themes;
preview_themes.push_back(ThemeDB::get_singleton()->get_default_theme());
ThemeDB::get_singleton()->create_theme_context(preview_root, preview_themes);
} break;
Expand Down
2 changes: 1 addition & 1 deletion editor/project_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void ProjectManager::_update_theme(bool p_skip_creation) {
DisplayServer::set_early_window_clear_color_override(true, theme->get_color(SNAME("background"), EditorStringName(Editor)));
}

List<Ref<Theme>> editor_themes;
Vector<Ref<Theme>> editor_themes;
editor_themes.push_back(theme);
editor_themes.push_back(ThemeDB::get_singleton()->get_default_theme());

Expand Down
8 changes: 4 additions & 4 deletions scene/3d/label_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,13 +797,13 @@ Ref<Font> Label3D::_get_font_or_default() const {
}

const StringName theme_name = SceneStringName(font);
List<StringName> theme_types;
ThemeDB::get_singleton()->get_native_type_dependencies(get_class_name(), &theme_types);
Vector<StringName> theme_types;
ThemeDB::get_singleton()->get_native_type_dependencies(get_class_name(), theme_types);

ThemeContext *global_context = ThemeDB::get_singleton()->get_default_theme_context();
List<Ref<Theme>> themes = global_context->get_themes();
Vector<Ref<Theme>> themes = global_context->get_themes();
if (Engine::get_singleton()->is_editor_hint()) {
themes.push_front(ThemeDB::get_singleton()->get_project_theme());
themes.insert(0, ThemeDB::get_singleton()->get_project_theme());
}

for (const Ref<Theme> &theme : themes) {
Expand Down
48 changes: 24 additions & 24 deletions scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2575,8 +2575,8 @@ Ref<Texture2D> Control::get_theme_icon(const StringName &p_name, const StringNam
return data.theme_icon_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
Ref<Texture2D> icon = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
data.theme_icon_cache[p_theme_type][p_name] = icon;
return icon;
Expand All @@ -2599,8 +2599,8 @@ Ref<StyleBox> Control::get_theme_stylebox(const StringName &p_name, const String
return data.theme_style_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
Ref<StyleBox> style = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
data.theme_style_cache[p_theme_type][p_name] = style;
return style;
Expand All @@ -2623,8 +2623,8 @@ Ref<Font> Control::get_theme_font(const StringName &p_name, const StringName &p_
return data.theme_font_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
Ref<Font> font = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
data.theme_font_cache[p_theme_type][p_name] = font;
return font;
Expand All @@ -2647,8 +2647,8 @@ int Control::get_theme_font_size(const StringName &p_name, const StringName &p_t
return data.theme_font_size_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
int font_size = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
data.theme_font_size_cache[p_theme_type][p_name] = font_size;
return font_size;
Expand All @@ -2671,8 +2671,8 @@ Color Control::get_theme_color(const StringName &p_name, const StringName &p_the
return data.theme_color_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
Color color = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
data.theme_color_cache[p_theme_type][p_name] = color;
return color;
Expand All @@ -2695,8 +2695,8 @@ int Control::get_theme_constant(const StringName &p_name, const StringName &p_th
return data.theme_constant_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
int constant = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
data.theme_constant_cache[p_theme_type][p_name] = constant;
return constant;
Expand Down Expand Up @@ -2741,8 +2741,8 @@ bool Control::has_theme_icon(const StringName &p_name, const StringName &p_theme
}
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
}

Expand All @@ -2758,8 +2758,8 @@ bool Control::has_theme_stylebox(const StringName &p_name, const StringName &p_t
}
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
}

Expand All @@ -2775,8 +2775,8 @@ bool Control::has_theme_font(const StringName &p_name, const StringName &p_theme
}
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
}

Expand All @@ -2792,8 +2792,8 @@ bool Control::has_theme_font_size(const StringName &p_name, const StringName &p_
}
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
}

Expand All @@ -2809,8 +2809,8 @@ bool Control::has_theme_color(const StringName &p_name, const StringName &p_them
}
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
}

Expand All @@ -2826,8 +2826,8 @@ bool Control::has_theme_constant(const StringName &p_name, const StringName &p_t
}
}

List<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
}

Expand Down
48 changes: 24 additions & 24 deletions scene/main/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2159,8 +2159,8 @@ Ref<Texture2D> Window::get_theme_icon(const StringName &p_name, const StringName
return theme_icon_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
Ref<Texture2D> icon = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
theme_icon_cache[p_theme_type][p_name] = icon;
return icon;
Expand All @@ -2183,8 +2183,8 @@ Ref<StyleBox> Window::get_theme_stylebox(const StringName &p_name, const StringN
return theme_style_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
Ref<StyleBox> style = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
theme_style_cache[p_theme_type][p_name] = style;
return style;
Expand All @@ -2207,8 +2207,8 @@ Ref<Font> Window::get_theme_font(const StringName &p_name, const StringName &p_t
return theme_font_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
Ref<Font> font = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
theme_font_cache[p_theme_type][p_name] = font;
return font;
Expand All @@ -2231,8 +2231,8 @@ int Window::get_theme_font_size(const StringName &p_name, const StringName &p_th
return theme_font_size_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
int font_size = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
theme_font_size_cache[p_theme_type][p_name] = font_size;
return font_size;
Expand All @@ -2255,8 +2255,8 @@ Color Window::get_theme_color(const StringName &p_name, const StringName &p_them
return theme_color_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
Color color = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
theme_color_cache[p_theme_type][p_name] = color;
return color;
Expand All @@ -2279,8 +2279,8 @@ int Window::get_theme_constant(const StringName &p_name, const StringName &p_the
return theme_constant_cache[p_theme_type][p_name];
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
int constant = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
theme_constant_cache[p_theme_type][p_name] = constant;
return constant;
Expand Down Expand Up @@ -2325,8 +2325,8 @@ bool Window::has_theme_icon(const StringName &p_name, const StringName &p_theme_
}
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
}

Expand All @@ -2342,8 +2342,8 @@ bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_th
}
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
}

Expand All @@ -2359,8 +2359,8 @@ bool Window::has_theme_font(const StringName &p_name, const StringName &p_theme_
}
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
}

Expand All @@ -2376,8 +2376,8 @@ bool Window::has_theme_font_size(const StringName &p_name, const StringName &p_t
}
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
}

Expand All @@ -2393,8 +2393,8 @@ bool Window::has_theme_color(const StringName &p_name, const StringName &p_theme
}
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
}

Expand All @@ -2410,8 +2410,8 @@ bool Window::has_theme_constant(const StringName &p_name, const StringName &p_th
}
}

List<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
Vector<StringName> theme_types;
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
}

Expand Down
8 changes: 4 additions & 4 deletions scene/resources/3d/primitive_meshes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3468,13 +3468,13 @@ Ref<Font> TextMesh::_get_font_or_default() const {
}

StringName theme_name = "font";
List<StringName> theme_types;
ThemeDB::get_singleton()->get_native_type_dependencies(get_class_name(), &theme_types);
Vector<StringName> theme_types;
ThemeDB::get_singleton()->get_native_type_dependencies(get_class_name(), theme_types);

ThemeContext *global_context = ThemeDB::get_singleton()->get_default_theme_context();
List<Ref<Theme>> themes = global_context->get_themes();
Vector<Ref<Theme>> themes = global_context->get_themes();
if (Engine::get_singleton()->is_editor_hint()) {
themes.push_front(ThemeDB::get_singleton()->get_project_theme());
themes.insert(0, ThemeDB::get_singleton()->get_project_theme());
}

for (const Ref<Theme> &theme : themes) {
Expand Down
Loading

0 comments on commit 7593e55

Please sign in to comment.