-
-
Notifications
You must be signed in to change notification settings - Fork 21.8k
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
Improve CowData::insert
performance
#94353
Conversation
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.
Looks good, but let me suggest that while you're at it, you also avoid calling size()
twice.
Replacing the first two Should I leave the |
I'd go for all of them. And if you want to handle an error from I'd go for something like this: Error insert(Size p_pos, const T &p_val) {
Size curr_size = size();
ERR_FAIL_INDEX_V(p_pos, curr_size + 1, ERR_INVALID_PARAMETER);
Error err = resize(curr_size + 1);
ERR_FAIL_COND_V(err, err);
T *p = ptrw();
for (Size i = curr_size; i > p_pos; i--) {
p[i] = p[i - 1];
}
p[p_pos] = p_val;
return OK;
} |
Update CowData::insert to call ptrw() before loop, to avoid calling _copy_on_write for each item in the array, as well as repeated index checks in set and get. For larger Vectors/Arrays, this makes inserts around 10x faster for ints, 3x faster for Strings, and 2x faster for Variants. Less of an impact on smaller Vectors/Arrays, as a larger percentage of the time is spent allocating.
Updated and added error check |
For a user who doesn't understand engine core stuff much, what will be sped up by this optimization? |
This will improve things like |
Presumably this will affect things all over the engine where Vector.insert() is used? |
Might not be significant given other performance considerations, but it should affect them at least somewhat |
Thanks! |
Update
CowData::insert
to callptrw()
before loop, to avoid calling_copy_on_write
for each item in the array, as well as repeated index checks inset
andget
. For largerVector
s/Array
s, this makes inserts around 10x faster for ints, 3x faster forString
s, and 2x faster forVariant
s. Less of an impact on smallerVector
s/Array
s, as a larger percentage of the time is spent allocating.Compared old/new code times with below gdscript:
old:
new: