Skip to content

Commit 80b82af

Browse files
committed
Merge pull request #111163 from HolonProduction/node-greater-than
Speed up `Node::is_greater_than` by avoiding `alloca`
2 parents 2ac27b2 + 8c5e67a commit 80b82af

File tree

1 file changed

+18
-37
lines changed

1 file changed

+18
-37
lines changed

scene/main/node.cpp

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,59 +2147,40 @@ bool Node::is_ancestor_of(const Node *p_node) const {
21472147
}
21482148

21492149
bool Node::is_greater_than(const Node *p_node) const {
2150+
// parent->get_child(1) > parent->get_child(0) > parent
2151+
21502152
ERR_FAIL_NULL_V(p_node, false);
21512153
ERR_FAIL_COND_V(!data.tree, false);
2152-
ERR_FAIL_COND_V(!p_node->data.tree, false);
2154+
ERR_FAIL_COND_V(p_node->data.tree != data.tree, false);
21532155

21542156
ERR_FAIL_COND_V(data.depth < 0, false);
21552157
ERR_FAIL_COND_V(p_node->data.depth < 0, false);
21562158

21572159
_update_children_cache();
21582160

2159-
int *this_stack = (int *)alloca(sizeof(int) * data.depth);
2160-
int *that_stack = (int *)alloca(sizeof(int) * p_node->data.depth);
2161-
2162-
const Node *n = this;
2161+
bool this_is_deeper = this->data.depth > p_node->data.depth;
21632162

2164-
int idx = data.depth - 1;
2165-
while (n) {
2166-
ERR_FAIL_INDEX_V(idx, data.depth, false);
2167-
this_stack[idx--] = n->get_index();
2168-
n = n->data.parent;
2163+
const Node *deep = this;
2164+
const Node *shallow = p_node;
2165+
if (!this_is_deeper) {
2166+
deep = p_node;
2167+
shallow = this;
21692168
}
21702169

2171-
ERR_FAIL_COND_V(idx != -1, false);
2172-
n = p_node;
2173-
idx = p_node->data.depth - 1;
2174-
while (n) {
2175-
ERR_FAIL_INDEX_V(idx, p_node->data.depth, false);
2176-
that_stack[idx--] = n->get_index();
2177-
2178-
n = n->data.parent;
2170+
while (deep->data.depth > shallow->data.depth) {
2171+
deep = deep->data.parent;
21792172
}
2180-
ERR_FAIL_COND_V(idx != -1, false);
2181-
idx = 0;
21822173

2183-
bool res;
2184-
while (true) {
2185-
// using -2 since out-of-tree or nonroot nodes have -1
2186-
int this_idx = (idx >= data.depth) ? -2 : this_stack[idx];
2187-
int that_idx = (idx >= p_node->data.depth) ? -2 : that_stack[idx];
2174+
if (deep == shallow) { // Shallow is ancestor of deep.
2175+
return this_is_deeper;
2176+
}
21882177

2189-
if (this_idx > that_idx) {
2190-
res = true;
2191-
break;
2192-
} else if (this_idx < that_idx) {
2193-
res = false;
2194-
break;
2195-
} else if (this_idx == -2) {
2196-
res = false; // equal
2197-
break;
2198-
}
2199-
idx++;
2178+
while (deep->data.parent != shallow->data.parent) {
2179+
deep = deep->data.parent;
2180+
shallow = shallow->data.parent;
22002181
}
22012182

2202-
return res;
2183+
return (deep->get_index() > shallow->get_index()) == this_is_deeper;
22032184
}
22042185

22052186
void Node::get_owned_by(Node *p_by, List<Node *> *p_owned) {

0 commit comments

Comments
 (0)