Skip to content

Commit

Permalink
Merge pull request #62 from blazium-engine/revert-59-blazium_faster_f…
Browse files Browse the repository at this point in the history
…ind_children

Revert "Optimized Node.find_children:"
  • Loading branch information
Bioblaze authored Oct 18, 2024
2 parents 8a45f85 + 051acce commit 68cc0d2
Showing 1 changed file with 18 additions and 45 deletions.
63 changes: 18 additions & 45 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1834,50 +1834,24 @@ Node *Node::find_child(const String &p_pattern, bool p_recursive, bool p_owned)
// Can be recursive or not, and limited to owned nodes.
TypedArray<Node> Node::find_children(const String &p_pattern, const String &p_type, bool p_recursive, bool p_owned) const {
ERR_THREAD_GUARD_V(TypedArray<Node>());
TypedArray<Node> matches;
ERR_FAIL_COND_V(p_pattern.is_empty() && p_type.is_empty(), matches);

// Save basic pattern and type info for faster lookup
bool is_pattern_empty = p_pattern.is_empty();
bool is_type_empty = p_type.is_empty();
bool is_type_global_class = !is_type_empty && ScriptServer::is_global_class(p_type);
String type_global_path = is_type_global_class ? ScriptServer::get_global_class_path(p_type) : "";

TypedArray<Node> to_search;
to_search.append(this);
bool is_adding_children = true;
while (!to_search.is_empty()) {
Node *entry = Object::cast_to<Node>(to_search.pop_front());

// Add all the children to the list to search
entry->_update_children_cache();
if (is_adding_children) {
Node *const *cptr = entry->data.children_cache.ptr();
int ccount = entry->data.children_cache.size();
for (int i = 0; i < ccount; i++) {
if (p_owned && !cptr[i]->data.owner) {
continue;
}

to_search.append(cptr[i]);
}

// Stop further child adding if we don't want recursive
if (!p_recursive) {
is_adding_children = false;
}
TypedArray<Node> ret;
ERR_FAIL_COND_V(p_pattern.is_empty() && p_type.is_empty(), ret);
_update_children_cache();
Node *const *cptr = data.children_cache.ptr();
int ccount = data.children_cache.size();
for (int i = 0; i < ccount; i++) {
if (p_owned && !cptr[i]->data.owner) {
continue;
}

// Check if the entry matches
bool is_pattern_match = is_pattern_empty || entry->data.name.operator String().match(p_pattern);
bool is_type_match = is_type_empty || entry->is_class(p_type);
bool is_script_type_match = false;
if (!is_type_match) {
if (ScriptInstance *script_inst = entry->get_script_instance()) {
Ref<Script> scr = script_inst->get_script();
if (p_pattern.is_empty() || cptr[i]->data.name.operator String().match(p_pattern)) {
if (p_type.is_empty() || cptr[i]->is_class(p_type)) {
ret.append(cptr[i]);
} else if (cptr[i]->get_script_instance()) {
Ref<Script> scr = cptr[i]->get_script_instance()->get_script();
while (scr.is_valid()) {
if ((is_type_global_class && type_global_path == scr->get_path()) || p_type == scr->get_path()) {
is_script_type_match = true;
if ((ScriptServer::is_global_class(p_type) && ScriptServer::get_global_class_path(p_type) == scr->get_path()) || p_type == scr->get_path()) {
ret.append(cptr[i]);
break;
}

Expand All @@ -1886,13 +1860,12 @@ TypedArray<Node> Node::find_children(const String &p_pattern, const String &p_ty
}
}

// Save it if it matches the pattern and at least one type
if (is_pattern_match && (is_type_match || is_script_type_match)) {
matches.append(entry);
if (p_recursive) {
ret.append_array(cptr[i]->find_children(p_pattern, p_type, true, p_owned));
}
}

return matches;
return ret;
}

void Node::reparent(Node *p_parent, bool p_keep_global_transform) {
Expand Down

0 comments on commit 68cc0d2

Please sign in to comment.