LibJS: Add fast path for TypedArrayPrototype.copyWithin()

This can be a simple memmove() in the most common cases.

Shaves 500ms of load time off of https://terminal.shop/api
This commit is contained in:
Andreas Kling 2025-07-17 00:03:18 +02:00 committed by Jelle Raaijmakers
commit ada198bee0
Notes: github-actions[bot] 2025-07-17 06:51:12 +00:00

View file

@ -383,6 +383,25 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::copy_within)
direction = 1;
}
// OPTIMIZATION: Fast path for non-shared ArrayBuffers that are not detached and have enough space to perform the copy with memmove.
if (!buffer->is_shared_array_buffer()) {
Checked<size_t> from_end = from_byte_index;
from_end += count_bytes;
Checked<size_t> to_end = to_byte_index;
to_end += count_bytes;
if (!from_end.has_overflow()
&& !to_end.has_overflow()
&& from_end.value() <= buffer_byte_limit
&& to_end.value() <= buffer_byte_limit) {
auto* base = buffer->buffer().data();
void const* src = base + from_byte_index;
void* dst = base + to_byte_index;
memmove(dst, src, count_bytes);
return typed_array;
}
}
// n. Repeat, while countBytes > 0,
while (count_bytes > 0) {
// i. If fromByteIndex < bufferByteLimit and toByteIndex < bufferByteLimit, then