Skip to content

Commit 8c5e67a

Browse files
Speedup Node::is_greater_than by avoiding alloca
1 parent 8d8041b commit 8c5e67a

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
@@ -2102,59 +2102,40 @@ bool Node::is_ancestor_of(const Node *p_node) const {
21022102
}
21032103

21042104
bool Node::is_greater_than(const Node *p_node) const {
2105+
// parent->get_child(1) > parent->get_child(0) > parent
2106+
21052107
ERR_FAIL_NULL_V(p_node, false);
21062108
ERR_FAIL_COND_V(!data.tree, false);
2107-
ERR_FAIL_COND_V(!p_node->data.tree, false);
2109+
ERR_FAIL_COND_V(p_node->data.tree != data.tree, false);
21082110

21092111
ERR_FAIL_COND_V(data.depth < 0, false);
21102112
ERR_FAIL_COND_V(p_node->data.depth < 0, false);
21112113

21122114
_update_children_cache();
21132115

2114-
int *this_stack = (int *)alloca(sizeof(int) * data.depth);
2115-
int *that_stack = (int *)alloca(sizeof(int) * p_node->data.depth);
2116-
2117-
const Node *n = this;
2116+
bool this_is_deeper = this->data.depth > p_node->data.depth;
21182117

2119-
int idx = data.depth - 1;
2120-
while (n) {
2121-
ERR_FAIL_INDEX_V(idx, data.depth, false);
2122-
this_stack[idx--] = n->get_index();
2123-
n = n->data.parent;
2118+
const Node *deep = this;
2119+
const Node *shallow = p_node;
2120+
if (!this_is_deeper) {
2121+
deep = p_node;
2122+
shallow = this;
21242123
}
21252124

2126-
ERR_FAIL_COND_V(idx != -1, false);
2127-
n = p_node;
2128-
idx = p_node->data.depth - 1;
2129-
while (n) {
2130-
ERR_FAIL_INDEX_V(idx, p_node->data.depth, false);
2131-
that_stack[idx--] = n->get_index();
2132-
2133-
n = n->data.parent;
2125+
while (deep->data.depth > shallow->data.depth) {
2126+
deep = deep->data.parent;
21342127
}
2135-
ERR_FAIL_COND_V(idx != -1, false);
2136-
idx = 0;
21372128

2138-
bool res;
2139-
while (true) {
2140-
// using -2 since out-of-tree or nonroot nodes have -1
2141-
int this_idx = (idx >= data.depth) ? -2 : this_stack[idx];
2142-
int that_idx = (idx >= p_node->data.depth) ? -2 : that_stack[idx];
2129+
if (deep == shallow) { // Shallow is ancestor of deep.
2130+
return this_is_deeper;
2131+
}
21432132

2144-
if (this_idx > that_idx) {
2145-
res = true;
2146-
break;
2147-
} else if (this_idx < that_idx) {
2148-
res = false;
2149-
break;
2150-
} else if (this_idx == -2) {
2151-
res = false; // equal
2152-
break;
2153-
}
2154-
idx++;
2133+
while (deep->data.parent != shallow->data.parent) {
2134+
deep = deep->data.parent;
2135+
shallow = shallow->data.parent;
21552136
}
21562137

2157-
return res;
2138+
return (deep->get_index() > shallow->get_index()) == this_is_deeper;
21582139
}
21592140

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

0 commit comments

Comments
 (0)