Skip to content

Commit 3335708

Browse files
committed
Refine children cache invalidation to skip more special cases.
1 parent 8b4b93a commit 3335708

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

scene/main/node.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,22 +1643,29 @@ void Node::_add_child_nocheck(Node *p_child, const StringName &p_name, InternalM
16431643
data.children.insert(p_name, p_child);
16441644

16451645
p_child->data.internal_mode = p_internal_mode;
1646+
1647+
bool can_push_back = false;
16461648
switch (p_internal_mode) {
16471649
case INTERNAL_MODE_FRONT: {
16481650
p_child->data.index = data.internal_children_front_count_cache++;
1651+
// Safe to push back when ordinary and back children are empty.
1652+
can_push_back = (data.external_children_count_cache + data.internal_children_back_count_cache) == 0;
16491653
} break;
16501654
case INTERNAL_MODE_BACK: {
16511655
p_child->data.index = data.internal_children_back_count_cache++;
1656+
// Safe to push back when cache is valid.
1657+
can_push_back = true;
16521658
} break;
16531659
case INTERNAL_MODE_DISABLED: {
16541660
p_child->data.index = data.external_children_count_cache++;
1661+
// Safe to push back when back children are empty.
1662+
can_push_back = data.internal_children_back_count_cache == 0;
16551663
} break;
16561664
}
16571665

16581666
p_child->data.parent = this;
16591667

1660-
if (!data.children_cache_dirty && p_internal_mode == INTERNAL_MODE_DISABLED && data.internal_children_back_count_cache == 0) {
1661-
// Special case, also add to the cached children array since its cheap.
1668+
if (!data.children_cache_dirty && can_push_back) {
16621669
data.children_cache.push_back(p_child);
16631670
} else {
16641671
data.children_cache_dirty = true;

scene/main/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class Node : public Object {
167167
Node *parent = nullptr;
168168
Node *owner = nullptr;
169169
HashMap<StringName, Node *> children;
170-
mutable bool children_cache_dirty = true;
170+
mutable bool children_cache_dirty = false;
171171
mutable LocalVector<Node *> children_cache;
172172
HashMap<StringName, Node *> owned_unique_nodes;
173173
bool unique_name_in_owner = false;

0 commit comments

Comments
 (0)