summaryrefslogtreecommitdiffstats
path: root/scene/main/node.cpp
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2023-04-06 17:54:56 +0200
committerJuan Linietsky <reduzio@gmail.com>2023-04-07 13:18:47 +0200
commit223ce4fcb958619d0f3c62e79a2b5846240e7ff3 (patch)
tree89536f9be2d90bbeee7f74b67032076fc0357e39 /scene/main/node.cpp
parent92b7a9603aa2395be6bf361067096538ba393c45 (diff)
downloadredot-engine-223ce4fcb958619d0f3c62e79a2b5846240e7ff3.tar.gz
Optimize Node::add_child validation
Adding 10k nodes is almost twice as fast.
Diffstat (limited to 'scene/main/node.cpp')
-rw-r--r--scene/main/node.cpp25
1 files changed, 23 insertions, 2 deletions
diff --git a/scene/main/node.cpp b/scene/main/node.cpp
index d8375dbc9f..9b3cc54460 100644
--- a/scene/main/node.cpp
+++ b/scene/main/node.cpp
@@ -994,8 +994,29 @@ void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) {
if (!unique) {
ERR_FAIL_COND(!node_hrcr_count.ref());
- String name = "@" + String(p_child->get_name()) + "@" + itos(node_hrcr_count.get());
- p_child->data.name = name;
+ // Optimized version of the code below:
+ // String name = "@" + String(p_child->get_name()) + "@" + itos(node_hrcr_count.get());
+ uint32_t c = node_hrcr_count.get();
+ String cn = p_child->get_class_name().operator String();
+ const char32_t *cn_ptr = cn.ptr();
+ uint32_t cn_length = cn.length();
+ uint32_t c_chars = String::num_characters(c);
+ uint32_t len = 2 + cn_length + c_chars;
+ char32_t *str = (char32_t *)alloca(sizeof(char32_t) * (len + 1));
+ uint32_t idx = 0;
+ str[idx++] = '@';
+ for (uint32_t i = 0; i < cn_length; i++) {
+ str[idx++] = cn_ptr[i];
+ }
+ str[idx++] = '@';
+ idx += c_chars;
+ ERR_FAIL_COND(idx != len);
+ str[idx] = 0;
+ while (c) {
+ str[--idx] = '0' + (c % 10);
+ c /= 10;
+ }
+ p_child->data.name = String(str);
}
}
}