-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize Node::add_child
validation
#75760
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
reduz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This buffer isn't deleted (the string constructor doesn't take ownership of it) Also would it make sense to use a thread static shared buffer of like size 64 or so and only allocate this temporary buffer if that is too small? Alternatively make this buffer a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alloca() allocates on the stack. It's the most efficient way of allocation for this kind of usage and it does not need to be freed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or you can just simplest solution that is already implemented; nvm then |
||
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); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be faster because it doesn't do divisions, but I didn't test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is probably faster, but the code is a mess, specially because this is int64 :P Still, in this context the code should be ok as-is, you won't get a really meaningful performance gain.