Skip to content

Commit aa3dc85

Browse files
committed
Fast child iteration in Node, Spatial, CanvasItem
1 parent 84f761b commit aa3dc85

File tree

5 files changed

+34
-25
lines changed

5 files changed

+34
-25
lines changed

core/local_vector.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ class LocalVector {
194194
return data[p_index];
195195
}
196196

197+
_FORCE_INLINE_ const T &get_unchecked(U p_index) const {
198+
return data[p_index];
199+
}
200+
_FORCE_INLINE_ T &get_unchecked(U p_index) {
201+
return data[p_index];
202+
}
203+
197204
void fill(T p_val) {
198205
for (U i = 0; i < count; i++) {
199206
data[i] = p_val;

scene/2d/canvas_item.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,8 +1028,10 @@ void CanvasItem::_notify_transform(CanvasItem *p_node) {
10281028
}
10291029
}
10301030

1031+
CanvasItem **children = p_node->data.canvas_item_children.ptr();
1032+
10311033
for (uint32_t n = 0; n < p_node->data.canvas_item_children.size(); n++) {
1032-
CanvasItem *ci = p_node->data.canvas_item_children[n];
1034+
CanvasItem *ci = children[n];
10331035
if (!ci->toplevel) {
10341036
_notify_transform(ci);
10351037
}

scene/3d/spatial.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ void Spatial::_propagate_transform_changed(Spatial *p_origin) {
104104
return; //already dirty
105105
*/
106106

107-
data.children_lock++;
107+
Spatial **children = data.spatial_children.ptr();
108108

109109
for (uint32_t n = 0; n < data.spatial_children.size(); n++) {
110-
Spatial *s = data.spatial_children[n];
110+
Spatial *s = children[n];
111111

112112
// Don't propagate to a toplevel.
113113
if (!s->data.toplevel_active) {
@@ -123,8 +123,6 @@ void Spatial::_propagate_transform_changed(Spatial *p_origin) {
123123
get_tree()->xform_change_list.add(&xform_change);
124124
}
125125
data.dirty |= DIRTY_GLOBAL | DIRTY_GLOBAL_INTERPOLATED;
126-
127-
data.children_lock--;
128126
}
129127

130128
void Spatial::notification_callback(int p_message_type) {
@@ -871,7 +869,7 @@ void Spatial::_propagate_visibility_changed() {
871869
#endif
872870

873871
for (uint32_t n = 0; n < data.spatial_children.size(); n++) {
874-
Spatial *s = data.spatial_children[n];
872+
Spatial *s = data.spatial_children.get_unchecked(n);
875873

876874
if (s->data.visible) {
877875
s->_propagate_visibility_changed();
@@ -900,7 +898,7 @@ void Spatial::_propagate_merging_allowed(bool p_merging_allowed) {
900898
data.merging_allowed = p_merging_allowed;
901899

902900
for (uint32_t n = 0; n < data.spatial_children.size(); n++) {
903-
Spatial *s = data.spatial_children[n];
901+
Spatial *s = data.spatial_children.get_unchecked(n);
904902
s->_propagate_merging_allowed(p_merging_allowed);
905903
}
906904
}
@@ -1257,7 +1255,6 @@ Spatial::Spatial() :
12571255
_define_ancestry(AncestralClass::SPATIAL);
12581256

12591257
data.dirty = DIRTY_NONE;
1260-
data.children_lock = 0;
12611258

12621259
data.ignore_notification = false;
12631260
data.toplevel = false;

scene/3d/spatial.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ class Spatial : public Node {
134134

135135
bool merging_allowed : 1;
136136

137-
int children_lock;
138137
Spatial *parent;
139138

140139
// An unordered vector of `Spatial` children only.

scene/main/node.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void Node::_propagate_ready() {
183183
data.ready_notified = true;
184184
data.blocked++;
185185
for (int i = 0; i < data.children.size(); i++) {
186-
data.children[i]->_propagate_ready();
186+
data.children.get_unchecked(i)->_propagate_ready();
187187
}
188188
data.blocked--;
189189

@@ -235,7 +235,7 @@ void Node::_propagate_physics_interpolation_reset_requested(bool p_requested) {
235235

236236
data.blocked++;
237237
for (int i = 0; i < data.children.size(); i++) {
238-
data.children[i]->_propagate_physics_interpolation_reset_requested(p_requested);
238+
data.children.get_unchecked(i)->_propagate_physics_interpolation_reset_requested(p_requested);
239239
}
240240
data.blocked--;
241241
}
@@ -281,8 +281,9 @@ void Node::_propagate_enter_tree() {
281281
//block while adding children
282282

283283
for (int i = 0; i < data.children.size(); i++) {
284-
if (!data.children[i]->is_inside_tree()) { // could have been added in enter_tree
285-
data.children[i]->_propagate_enter_tree();
284+
Node *child = data.children.get_unchecked(i);
285+
if (!child->is_inside_tree()) { // could have been added in enter_tree
286+
child->_propagate_enter_tree();
286287
}
287288
}
288289

@@ -429,7 +430,7 @@ void Node::move_child(Node *p_child, int p_pos) {
429430
data.blocked++;
430431
//new pos first
431432
for (int i = motion_from; i <= motion_to; i++) {
432-
data.children[i]->data.pos = i;
433+
data.children.get_unchecked(i)->data.pos = i;
433434
}
434435
// notification second
435436
move_child_notify(p_child);
@@ -456,7 +457,7 @@ void Node::_propagate_groups_dirty() {
456457
}
457458

458459
for (int i = 0; i < data.children.size(); i++) {
459-
data.children[i]->_propagate_groups_dirty();
460+
data.children.get_unchecked(i)->_propagate_groups_dirty();
460461
}
461462
}
462463

@@ -563,9 +564,11 @@ void Node::_propagate_pause_owner(Node *p_owner) {
563564
void Node::_propagate_pause_change_notification(int p_notification) {
564565
notification(p_notification);
565566

567+
Node **children = data.children.ptr();
568+
566569
for (int i = 0; i < data.children.size(); i++) {
567-
if (data.children[i]->data.pause_mode == PAUSE_MODE_INHERIT) {
568-
data.children[i]->_propagate_pause_change_notification(p_notification);
570+
if (children[i]->data.pause_mode == PAUSE_MODE_INHERIT) {
571+
children[i]->_propagate_pause_change_notification(p_notification);
569572
}
570573
}
571574
}
@@ -575,7 +578,7 @@ void Node::set_network_master(int p_peer_id, bool p_recursive) {
575578

576579
if (p_recursive) {
577580
for (int i = 0; i < data.children.size(); i++) {
578-
data.children[i]->set_network_master(p_peer_id, true);
581+
data.children.get_unchecked(i)->set_network_master(p_peer_id, true);
579582
}
580583
}
581584
}
@@ -1400,7 +1403,8 @@ int Node::get_child_count() const {
14001403
Node *Node::get_child(int p_index) const {
14011404
ERR_FAIL_INDEX_V(p_index, data.children.size(), nullptr);
14021405

1403-
return data.children[p_index];
1406+
// Index already checked above, can use unsafe version here.
1407+
return data.children.get_unchecked(p_index);
14041408
}
14051409

14061410
Node *Node::_get_child_by_name(const StringName &p_name) const {
@@ -1468,7 +1472,7 @@ Node *Node::get_node_or_null(const NodePath &p_path) const {
14681472
next = nullptr;
14691473

14701474
for (int j = 0; j < current->data.children.size(); j++) {
1471-
Node *child = current->data.children[j];
1475+
Node *child = current->data.children.get_unchecked(j);
14721476

14731477
if (child->data.name == name) {
14741478
next = child;
@@ -1936,7 +1940,7 @@ void Node::_print_tree(const Node *p_node) {
19361940
void Node::_propagate_reverse_notification(int p_notification) {
19371941
data.blocked++;
19381942
for (int i = data.children.size() - 1; i >= 0; i--) {
1939-
data.children[i]->_propagate_reverse_notification(p_notification);
1943+
data.children.get_unchecked(i)->_propagate_reverse_notification(p_notification);
19401944
}
19411945

19421946
notification(p_notification, true);
@@ -1953,7 +1957,7 @@ void Node::_propagate_deferred_notification(int p_notification, bool p_reverse)
19531957
}
19541958

19551959
for (int i = 0; i < data.children.size(); i++) {
1956-
data.children[i]->_propagate_deferred_notification(p_notification, p_reverse);
1960+
data.children.get_unchecked(i)->_propagate_deferred_notification(p_notification, p_reverse);
19571961
}
19581962

19591963
if (p_reverse) {
@@ -1968,7 +1972,7 @@ void Node::propagate_notification(int p_notification) {
19681972
notification(p_notification);
19691973

19701974
for (int i = 0; i < data.children.size(); i++) {
1971-
data.children[i]->propagate_notification(p_notification);
1975+
data.children.get_unchecked(i)->propagate_notification(p_notification);
19721976
}
19731977
data.blocked--;
19741978
}
@@ -1981,7 +1985,7 @@ void Node::propagate_call(const StringName &p_method, const Array &p_args, const
19811985
}
19821986

19831987
for (int i = 0; i < data.children.size(); i++) {
1984-
data.children[i]->propagate_call(p_method, p_args, p_parent_first);
1988+
data.children.get_unchecked(i)->propagate_call(p_method, p_args, p_parent_first);
19851989
}
19861990

19871991
if (!p_parent_first && has_method(p_method)) {
@@ -1998,7 +2002,7 @@ void Node::_propagate_replace_owner(Node *p_owner, Node *p_by_owner) {
19982002

19992003
data.blocked++;
20002004
for (int i = 0; i < data.children.size(); i++) {
2001-
data.children[i]->_propagate_replace_owner(p_owner, p_by_owner);
2005+
data.children.get_unchecked(i)->_propagate_replace_owner(p_owner, p_by_owner);
20022006
}
20032007
data.blocked--;
20042008
}

0 commit comments

Comments
 (0)