Skip to content

Commit 89c51cb

Browse files
committed
Merge pull request godotengine#110571 from WhalesState/get-children
Improve `Node::get_children` performance.
2 parents 035f5d3 + 7dc6df3 commit 89c51cb

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

scene/main/node.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,14 +1814,30 @@ Node *Node::get_child(int p_index, bool p_include_internal) const {
18141814

18151815
TypedArray<Node> Node::get_children(bool p_include_internal) const {
18161816
ERR_THREAD_GUARD_V(TypedArray<Node>());
1817-
TypedArray<Node> arr;
1818-
int cc = get_child_count(p_include_internal);
1819-
arr.resize(cc);
1820-
for (int i = 0; i < cc; i++) {
1821-
arr[i] = get_child(i, p_include_internal);
1817+
_update_children_cache();
1818+
1819+
TypedArray<Node> children;
1820+
1821+
if (p_include_internal) {
1822+
children.resize(data.children_cache.size());
1823+
1824+
Array::Iterator itr = children.begin();
1825+
for (const Node *child : data.children_cache) {
1826+
*itr = child;
1827+
++itr;
1828+
}
1829+
} else {
1830+
const int size = data.children_cache.size() - data.internal_children_back_count_cache;
1831+
children.resize(size - data.internal_children_front_count_cache);
1832+
1833+
Array::Iterator itr = children.begin();
1834+
for (int i = data.internal_children_front_count_cache; i < size; i++) {
1835+
*itr = data.children_cache[i];
1836+
++itr;
1837+
}
18221838
}
18231839

1824-
return arr;
1840+
return children;
18251841
}
18261842

18271843
Node *Node::_get_child_by_name(const StringName &p_name) const {

0 commit comments

Comments
 (0)