perf: Array.push: move elements directly when source is unique

This commit is contained in:
Sebastian Ullrich 2021-12-16 15:06:51 +01:00 committed by Leonardo de Moura
parent 72cfe18e9f
commit 87e860f871

View file

@ -2064,11 +2064,17 @@ extern "C" LEAN_EXPORT obj_res lean_copy_expand_array(obj_arg a, bool expand) {
object ** it = lean_array_cptr(a);
object ** end = it + sz;
object ** dest = lean_array_cptr(r);
for (; it != end; ++it, ++dest) {
*dest = *it;
lean_inc(*it);
if (lean_is_exclusive(a)) {
// transfer ownership of elements directly instead of inc+dec
memcpy(dest, it, sz * sizeof(object *));
lean_dealloc(a, lean_array_byte_size(a));
} else {
for (; it != end; ++it, ++dest) {
*dest = *it;
lean_inc(*it);
}
lean_dec(a);
}
lean_dec(a);
return r;
}