Skip to content

Commit 61d470b

Browse files
committed
Optimize Node.find_children
1 parent 68410ac commit 61d470b

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

scene/main/node.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,34 +2008,38 @@ TypedArray<Node> Node::find_children(const String &p_pattern, const String &p_ty
20082008
TypedArray<Node> ret;
20092009
ERR_FAIL_COND_V(p_pattern.is_empty() && p_type.is_empty(), ret);
20102010
_update_children_cache();
2011-
Node *const *cptr = data.children_cache.ptr();
2012-
int ccount = data.children_cache.size();
2013-
for (int i = 0; i < ccount; i++) {
2014-
if (p_owned && !cptr[i]->data.owner) {
2015-
continue;
2016-
}
20172011

2018-
if (p_pattern.is_empty() || cptr[i]->data.name.operator String().match(p_pattern)) {
2019-
if (p_type.is_empty() || cptr[i]->is_class(p_type)) {
2020-
ret.append(cptr[i]);
2021-
} else if (cptr[i]->get_script_instance()) {
2022-
Ref<Script> scr = cptr[i]->get_script_instance()->get_script();
2023-
while (scr.is_valid()) {
2024-
if ((ScriptServer::is_global_class(p_type) && ScriptServer::get_global_class_path(p_type) == scr->get_path()) || p_type == scr->get_path()) {
2025-
ret.append(cptr[i]);
2026-
break;
2027-
}
2012+
const auto find_children_add = [&](const auto &find_children_add, TypedArray<Node> &p_array, const Node *p_current_node) -> void {
2013+
Node *const *cptr = p_current_node->data.children_cache.ptr();
2014+
int ccount = p_current_node->data.children_cache.size();
2015+
for (int i = 0; i < ccount; i++) {
2016+
if (p_owned && !cptr[i]->data.owner) {
2017+
continue;
2018+
}
2019+
2020+
if (p_pattern.is_empty() || cptr[i]->data.name.operator String().match(p_pattern)) {
2021+
if (p_type.is_empty() || cptr[i]->is_class(p_type)) {
2022+
p_array.append(cptr[i]);
2023+
} else if (cptr[i]->get_script_instance()) {
2024+
Ref<Script> scr = cptr[i]->get_script_instance()->get_script();
2025+
while (scr.is_valid()) {
2026+
if ((ScriptServer::is_global_class(p_type) && ScriptServer::get_global_class_path(p_type) == scr->get_path()) || p_type == scr->get_path()) {
2027+
p_array.append(cptr[i]);
2028+
break;
2029+
}
20282030

2029-
scr = scr->get_base_script();
2031+
scr = scr->get_base_script();
2032+
}
20302033
}
20312034
}
2032-
}
20332035

2034-
if (p_recursive) {
2035-
ret.append_array(cptr[i]->find_children(p_pattern, p_type, true, p_owned));
2036+
if (p_recursive) {
2037+
find_children_add(find_children_add, p_array, cptr[i]);
2038+
}
20362039
}
2037-
}
2040+
};
20382041

2042+
find_children_add(find_children_add, ret, this);
20392043
return ret;
20402044
}
20412045

0 commit comments

Comments
 (0)