1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:27:45 +00:00

LibJS: Add a fast path for getting the TA length of known valid TAs

In IsValidIntegerIndex, we check IsTypedArrayOutOfBounds before invoking
TypedArrayLength. There's no need to check it again.

On https://cyxx.github.io/another_js, this reduces the runtime of
IsValidIntegerIndex from 16% to 12.5%.
This commit is contained in:
Timothy Flynn 2024-02-27 08:47:50 -05:00 committed by Andreas Kling
parent 6656f70387
commit 9c943f36ed
2 changed files with 15 additions and 6 deletions

View file

@ -639,11 +639,8 @@ u32 typed_array_byte_length(TypedArrayWithBufferWitness const& typed_array_recor
}
// 10.4.5.12 TypedArrayLength ( taRecord ), https://tc39.es/ecma262/#sec-typedarraylength
u32 typed_array_length(TypedArrayWithBufferWitness const& typed_array_record)
u32 typed_array_length_with_known_valid_bounds(TypedArrayWithBufferWitness const& typed_array_record)
{
// 1. Assert: IsTypedArrayOutOfBounds(taRecord) is false.
VERIFY(!is_typed_array_out_of_bounds(typed_array_record));
// 2. Let O be taRecord.[[Object]].
auto object = typed_array_record.object;
@ -735,7 +732,7 @@ bool is_valid_integer_index(TypedArrayBase const& typed_array, CanonicalIndex pr
return false;
// 7. Let length be TypedArrayLength(taRecord).
auto length = typed_array_length(typed_array_record);
auto length = typed_array_length_with_known_valid_bounds(typed_array_record);
// 8. If (index) < 0 or (index) ≥ length, return false.
if (property_index.as_index() >= length)

View file

@ -94,10 +94,22 @@ struct TypedArrayWithBufferWitness {
TypedArrayWithBufferWitness make_typed_array_with_buffer_witness_record(TypedArrayBase const&, ArrayBuffer::Order);
u32 typed_array_byte_length(TypedArrayWithBufferWitness const&);
u32 typed_array_length(TypedArrayWithBufferWitness const&);
bool is_typed_array_out_of_bounds(TypedArrayWithBufferWitness const&);
bool is_valid_integer_index(TypedArrayBase const&, CanonicalIndex);
// Fast-path version of TypedArrayLength when you already know the TA is within its bounds,
// i.e. you previously checked IsTypedArrayOutOfBounds.
u32 typed_array_length_with_known_valid_bounds(TypedArrayWithBufferWitness const&);
// 10.4.5.12 TypedArrayLength ( taRecord ), https://tc39.es/ecma262/#sec-typedarraylength
inline u32 typed_array_length(TypedArrayWithBufferWitness const& typed_array_record)
{
// 1. Assert: IsTypedArrayOutOfBounds(taRecord) is false.
VERIFY(!is_typed_array_out_of_bounds(typed_array_record));
return typed_array_length_with_known_valid_bounds(typed_array_record);
}
// 10.4.5.15 TypedArrayGetElement ( O, index ), https://tc39.es/ecma262/#sec-typedarraygetelement
template<typename T>
inline ThrowCompletionOr<Value> typed_array_get_element(TypedArrayBase const& typed_array, CanonicalIndex property_index)