diff options
author | aaronp64 <aaronp.code@gmail.com> | 2024-07-13 21:36:11 -0400 |
---|---|---|
committer | aaronp64 <aaronp.code@gmail.com> | 2024-07-15 14:01:19 -0400 |
commit | c1afe7dcdf98ff4f34a91e2290e86630df60c0dd (patch) | |
tree | 3778783f8645a05270101ab9919adce1ce7c4225 /core | |
parent | 97b8ad1af0f2b4a216f6f1263bef4fbc69e56c7b (diff) | |
download | redot-engine-c1afe7dcdf98ff4f34a91e2290e86630df60c0dd.tar.gz |
Improve CowData::insert performance
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.
Diffstat (limited to 'core')
-rw-r--r-- | core/templates/cowdata.h | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index f22ae1f1d3..6f818956ea 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -222,12 +222,15 @@ public: } Error insert(Size p_pos, const T &p_val) { - ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER); - resize(size() + 1); - for (Size i = (size() - 1); i > p_pos; i--) { - set(i, get(i - 1)); + Size new_size = size() + 1; + ERR_FAIL_INDEX_V(p_pos, new_size, ERR_INVALID_PARAMETER); + Error err = resize(new_size); + ERR_FAIL_COND_V(err, err); + T *p = ptrw(); + for (Size i = new_size - 1; i > p_pos; i--) { + p[i] = p[i - 1]; } - set(p_pos, p_val); + p[p_pos] = p_val; return OK; } |