Refine children cache invalidation to skip more special cases.

This commit is contained in:
Mounir Tohami
2025-09-18 18:28:59 +03:00
parent 8b4b93a82e
commit 3335708ce0
2 changed files with 10 additions and 3 deletions

View File

@ -1643,22 +1643,29 @@ void Node::_add_child_nocheck(Node *p_child, const StringName &p_name, InternalM
data.children.insert(p_name, p_child);
p_child->data.internal_mode = p_internal_mode;
bool can_push_back = false;
switch (p_internal_mode) {
case INTERNAL_MODE_FRONT: {
p_child->data.index = data.internal_children_front_count_cache++;
// Safe to push back when ordinary and back children are empty.
can_push_back = (data.external_children_count_cache + data.internal_children_back_count_cache) == 0;
} break;
case INTERNAL_MODE_BACK: {
p_child->data.index = data.internal_children_back_count_cache++;
// Safe to push back when cache is valid.
can_push_back = true;
} break;
case INTERNAL_MODE_DISABLED: {
p_child->data.index = data.external_children_count_cache++;
// Safe to push back when back children are empty.
can_push_back = data.internal_children_back_count_cache == 0;
} break;
}
p_child->data.parent = this;
if (!data.children_cache_dirty && p_internal_mode == INTERNAL_MODE_DISABLED && data.internal_children_back_count_cache == 0) {
// Special case, also add to the cached children array since its cheap.
if (!data.children_cache_dirty && can_push_back) {
data.children_cache.push_back(p_child);
} else {
data.children_cache_dirty = true;

View File

@ -167,7 +167,7 @@ private:
Node *parent = nullptr;
Node *owner = nullptr;
HashMap<StringName, Node *> children;
mutable bool children_cache_dirty = true;
mutable bool children_cache_dirty = false;
mutable LocalVector<Node *> children_cache;
HashMap<StringName, Node *> owned_unique_nodes;
bool unique_name_in_owner = false;