You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-12-04 17:04:49 +00:00
Speedup Node::is_greater_than by avoiding alloca
This commit is contained in:
@@ -2102,59 +2102,40 @@ bool Node::is_ancestor_of(const Node *p_node) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Node::is_greater_than(const Node *p_node) const {
|
bool Node::is_greater_than(const Node *p_node) const {
|
||||||
|
// parent->get_child(1) > parent->get_child(0) > parent
|
||||||
|
|
||||||
ERR_FAIL_NULL_V(p_node, false);
|
ERR_FAIL_NULL_V(p_node, false);
|
||||||
ERR_FAIL_COND_V(!data.tree, false);
|
ERR_FAIL_COND_V(!data.tree, false);
|
||||||
ERR_FAIL_COND_V(!p_node->data.tree, false);
|
ERR_FAIL_COND_V(p_node->data.tree != data.tree, false);
|
||||||
|
|
||||||
ERR_FAIL_COND_V(data.depth < 0, false);
|
ERR_FAIL_COND_V(data.depth < 0, false);
|
||||||
ERR_FAIL_COND_V(p_node->data.depth < 0, false);
|
ERR_FAIL_COND_V(p_node->data.depth < 0, false);
|
||||||
|
|
||||||
_update_children_cache();
|
_update_children_cache();
|
||||||
|
|
||||||
int *this_stack = (int *)alloca(sizeof(int) * data.depth);
|
bool this_is_deeper = this->data.depth > p_node->data.depth;
|
||||||
int *that_stack = (int *)alloca(sizeof(int) * p_node->data.depth);
|
|
||||||
|
|
||||||
const Node *n = this;
|
const Node *deep = this;
|
||||||
|
const Node *shallow = p_node;
|
||||||
int idx = data.depth - 1;
|
if (!this_is_deeper) {
|
||||||
while (n) {
|
deep = p_node;
|
||||||
ERR_FAIL_INDEX_V(idx, data.depth, false);
|
shallow = this;
|
||||||
this_stack[idx--] = n->get_index();
|
|
||||||
n = n->data.parent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ERR_FAIL_COND_V(idx != -1, false);
|
while (deep->data.depth > shallow->data.depth) {
|
||||||
n = p_node;
|
deep = deep->data.parent;
|
||||||
idx = p_node->data.depth - 1;
|
|
||||||
while (n) {
|
|
||||||
ERR_FAIL_INDEX_V(idx, p_node->data.depth, false);
|
|
||||||
that_stack[idx--] = n->get_index();
|
|
||||||
|
|
||||||
n = n->data.parent;
|
|
||||||
}
|
|
||||||
ERR_FAIL_COND_V(idx != -1, false);
|
|
||||||
idx = 0;
|
|
||||||
|
|
||||||
bool res;
|
|
||||||
while (true) {
|
|
||||||
// using -2 since out-of-tree or nonroot nodes have -1
|
|
||||||
int this_idx = (idx >= data.depth) ? -2 : this_stack[idx];
|
|
||||||
int that_idx = (idx >= p_node->data.depth) ? -2 : that_stack[idx];
|
|
||||||
|
|
||||||
if (this_idx > that_idx) {
|
|
||||||
res = true;
|
|
||||||
break;
|
|
||||||
} else if (this_idx < that_idx) {
|
|
||||||
res = false;
|
|
||||||
break;
|
|
||||||
} else if (this_idx == -2) {
|
|
||||||
res = false; // equal
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
idx++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
if (deep == shallow) { // Shallow is ancestor of deep.
|
||||||
|
return this_is_deeper;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (deep->data.parent != shallow->data.parent) {
|
||||||
|
deep = deep->data.parent;
|
||||||
|
shallow = shallow->data.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (deep->get_index() > shallow->get_index()) == this_is_deeper;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::get_owned_by(Node *p_by, List<Node *> *p_owned) {
|
void Node::get_owned_by(Node *p_by, List<Node *> *p_owned) {
|
||||||
|
|||||||
Reference in New Issue
Block a user