From 9c943f36ed11f5b490163a93e1f637153919545b Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 27 Feb 2024 08:47:50 -0500 Subject: [PATCH] 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%. --- Userland/Libraries/LibJS/Runtime/TypedArray.cpp | 7 ++----- Userland/Libraries/LibJS/Runtime/TypedArray.h | 14 +++++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp index c23996961c..dc101dc46e 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp @@ -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) diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index d36053a9f7..bc7f3941f8 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -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 inline ThrowCompletionOr typed_array_get_element(TypedArrayBase const& typed_array, CanonicalIndex property_index)