LibJS: Fast path for TypedArray.slice()

We now try to do a bulk memcpy() when possible. This is significantly
faster than going byte-at-a-time.
This commit is contained in:
Andreas Kling 2025-12-01 12:08:11 +01:00 committed by Jelle Raaijmakers
parent 94fc8c47c0
commit 99bef81d09
Notes: github-actions[bot] 2025-12-01 14:14:00 +00:00

View file

@ -1740,6 +1740,12 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::slice)
return array;
}
// OPTIMIZATION: If the buffers are not detached and not shared, we can do a single bulk copy.
if (!target_buffer.is_detached() && !target_buffer.is_shared_array_buffer()
&& !source_buffer.is_detached() && !source_buffer.is_shared_array_buffer()
&& &target_buffer.buffer() != &source_buffer.buffer()) {
target_buffer.buffer().overwrite(target_byte_index, source_buffer.buffer().data() + source_byte_index.value(), limit.value() - target_byte_index);
} else {
// ix. Repeat, while targetByteIndex < limit,
while (target_byte_index < limit) {
// 1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, uint8, true, unordered).
@ -1755,6 +1761,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::slice)
++target_byte_index;
}
}
}
// i. Else,
else {
// i. Let n be 0.