Skip to content

Commit

Permalink
Array performance improvements to reduce copying/copy_on_write calls
Browse files Browse the repository at this point in the history
- Avoid temporary copy of p_array in Array::append_array when types match
- Call ptrw() once before looping in methods that return new Arrays, to avoid copy_on_write call for each item (recursive_duplicate, slice, filter, map)
  • Loading branch information
aaronp64 committed Sep 6, 2024
1 parent 514c564 commit 0ac2cf2
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,20 @@ void Array::push_back(const Variant &p_value) {

void Array::append_array(const Array &p_array) {
ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
const ContainerTypeValidate &typed = _p->typed;
const ContainerTypeValidate &other_typed = p_array._p->typed;

Vector<Variant> validated_array = p_array._p->array;
for (int i = 0; i < validated_array.size(); ++i) {
ERR_FAIL_COND(!_p->typed.validate(validated_array.write[i], "append_array"));
}
if (typed == other_typed || typed.type == Variant::NIL || (other_typed.type == Variant::OBJECT && typed.can_reference(other_typed))) {
_p->array.append_array(p_array._p->array);
} else {
Vector<Variant> validated_array = p_array._p->array;
Variant *write = validated_array.ptrw();
for (int i = 0; i < validated_array.size(); ++i) {
ERR_FAIL_COND(!_p->typed.validate(write[i], "append_array"));
}

_p->array.append_array(validated_array);
_p->array.append_array(validated_array);
}
}

Error Array::resize(int p_new_size) {
Expand Down Expand Up @@ -452,8 +459,9 @@ Array Array::recursive_duplicate(bool p_deep, int recursion_count) const {
recursion_count++;
int element_count = size();
new_arr.resize(element_count);
Variant *write = new_arr._p->array.ptrw();
for (int i = 0; i < element_count; i++) {
new_arr[i] = get(i).recursive_duplicate(true, recursion_count);
write[i] = get(i).recursive_duplicate(true, recursion_count);
}
} else {
new_arr._p->array = _p->array;
Expand Down Expand Up @@ -489,8 +497,9 @@ Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const {
int result_size = (end - begin) / p_step + (((end - begin) % p_step != 0) ? 1 : 0);
result.resize(result_size);

Variant *write = result._p->array.ptrw();
for (int src_idx = begin, dest_idx = 0; dest_idx < result_size; ++dest_idx) {
result[dest_idx] = p_deep ? get(src_idx).duplicate(true) : get(src_idx);
write[dest_idx] = p_deep ? get(src_idx).duplicate(true) : get(src_idx);
src_idx += p_step;
}

Expand All @@ -504,6 +513,7 @@ Array Array::filter(const Callable &p_callable) const {
int accepted_count = 0;

const Variant *argptrs[1];
Variant *write = new_arr._p->array.ptrw();
for (int i = 0; i < size(); i++) {
argptrs[0] = &get(i);

Expand All @@ -515,7 +525,7 @@ Array Array::filter(const Callable &p_callable) const {
}

if (result.operator bool()) {
new_arr[accepted_count] = get(i);
write[accepted_count] = get(i);
accepted_count++;
}
}
Expand All @@ -530,17 +540,15 @@ Array Array::map(const Callable &p_callable) const {
new_arr.resize(size());

const Variant *argptrs[1];
Variant *write = new_arr._p->array.ptrw();
for (int i = 0; i < size(); i++) {
argptrs[0] = &get(i);

Variant result;
Callable::CallError ce;
p_callable.callp(argptrs, 1, result, ce);
p_callable.callp(argptrs, 1, write[i], ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_V_MSG(Array(), "Error calling method from 'map': " + Variant::get_callable_error_text(p_callable, argptrs, 1, ce));
}

new_arr[i] = result;
}

return new_arr;
Expand Down

0 comments on commit 0ac2cf2

Please sign in to comment.