Skip to content

Commit 31f9ed0

Browse files
committed
Merge pull request #110650 from WhalesState/node-cache-less-dirty
Optimize children cache updates and refine special-case handling
2 parents c9b72fc + 3335708 commit 31f9ed0

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
@@ -1647,22 +1647,29 @@ void Node::_add_child_nocheck(Node *p_child, const StringName &p_name, InternalM
16471647
data.children.insert(p_name, p_child);
16481648

16491649
p_child->data.internal_mode = p_internal_mode;
1650+
1651+
bool can_push_back = false;
16501652
switch (p_internal_mode) {
16511653
case INTERNAL_MODE_FRONT: {
16521654
p_child->data.index = data.internal_children_front_count_cache++;
1655+
// Safe to push back when ordinary and back children are empty.
1656+
can_push_back = (data.external_children_count_cache + data.internal_children_back_count_cache) == 0;
16531657
} break;
16541658
case INTERNAL_MODE_BACK: {
16551659
p_child->data.index = data.internal_children_back_count_cache++;
1660+
// Safe to push back when cache is valid.
1661+
can_push_back = true;
16561662
} break;
16571663
case INTERNAL_MODE_DISABLED: {
16581664
p_child->data.index = data.external_children_count_cache++;
1665+
// Safe to push back when back children are empty.
1666+
can_push_back = data.internal_children_back_count_cache == 0;
16591667
} break;
16601668
}
16611669

16621670
p_child->data.parent = this;
16631671

1664-
if (!data.children_cache_dirty && p_internal_mode == INTERNAL_MODE_DISABLED && data.internal_children_back_count_cache == 0) {
1665-
// Special case, also add to the cached children array since its cheap.
1672+
if (!data.children_cache_dirty && can_push_back) {
16661673
data.children_cache.push_back(p_child);
16671674
} else {
16681675
data.children_cache_dirty = true;

scene/main/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class Node : public Object {
196196
Node *parent = nullptr;
197197
Node *owner = nullptr;
198198
HashMap<StringName, Node *> children;
199-
mutable bool children_cache_dirty = true;
199+
mutable bool children_cache_dirty = false;
200200
mutable LocalVector<Node *> children_cache;
201201
HashMap<StringName, Node *> owned_unique_nodes;
202202
bool unique_name_in_owner = false;

0 commit comments

Comments
 (0)