Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Optimize `Node::is_ancestor_of` for nodes in tree

HolonProductionc9d0c77c1f8a
diff --git a/scene/main/node.cpp b/scene/main/node.cpp
index 1de089fe82b..1335477a0b1 100644
--- a/scene/main/node.cpp
+++ b/scene/main/node.cpp
@@ -2150,12 +2150,23 @@ Window *Node::get_last_exclusive_window() const {
bool Node::is_ancestor_of(RequiredParam<const Node> rp_node) const {
EXTRACT_PARAM_OR_FAIL_V(p_node, rp_node, false);
- Node *p = p_node->data.parent;
- while (p) {
- if (p == this) {
+
+ const Node *n = p_node;
+
+ if (is_inside_tree() && p_node->data.tree == data.tree) {
+ const int depth = data.depth;
+ while (n->data.depth > depth) {
+ n = n->data.parent;
+ }
+ return n == this;
+ }
+
+ n = p_node->data.parent;
+ while (n) {
+ if (n == this) {
return true;
}
- p = p->data.parent;
+ n = n->data.parent;
}
return false;