LibJS: Cache typed array data pointers for indexed access

Cache raw data pointers on fixed-length typed array views so asm
GetByValue and PutByValue can use them directly for indexed
element access.

Replace the asm typed-array hot-path
ArrayBuffer/DataBlock/ByteBuffer walk with one cached_data_ptr load.
Remove six unconditional loads, four branches, and the byte_offset
add before the element access, trading them for one
cached_data_ptr null check.

Keep direct C++ typed-array access on IsValidIntegerIndex-based
checks, invalidate cached pointers eagerly when a backing
ArrayBuffer is detached, and add regression coverage for shrink,
regrow, and detach on number and BigInt typed arrays.
This commit is contained in:
Andreas Kling
2026-03-18 09:27:37 -05:00
committed by Andreas Kling
parent cf050288fa
commit 9299d430c8
Notes: github-actions[bot] 2026-03-18 19:02:40 +00:00
7 changed files with 202 additions and 114 deletions

View File

@@ -8,6 +8,7 @@
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/ArrayBufferConstructor.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/TypedArray.h>
namespace JS {
@@ -250,6 +251,21 @@ ThrowCompletionOr<ArrayBuffer*> array_buffer_copy_and_detach(VM& vm, ArrayBuffer
return new_buffer;
}
void ArrayBuffer::detach_buffer()
{
for (auto& view : m_cached_views) {
if (view.viewed_array_buffer() == this)
view.set_cached_data_ptr(nullptr);
}
m_cached_views.clear();
m_data_block.byte_buffer = Empty {};
}
void ArrayBuffer::register_cached_typed_array_view(TypedArrayBase& view)
{
m_cached_views.set(view);
}
// 25.1.3.5 DetachArrayBuffer ( arrayBuffer [ , key ] ), https://tc39.es/ecma262/#sec-detacharraybuffer
ThrowCompletionOr<void> detach_array_buffer(VM& vm, ArrayBuffer& array_buffer, Optional<Value> key)
{