From 27a83f7e5e28e6c1b441f12571db98b403e9f847 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 5 Oct 2023 09:00:54 +0200 Subject: [PATCH] LibJS: Make array-like Get access on ordinary objects much faster This patch adds a fast path to the GetByValue bytecode op that bypasses a ton of things *if* a set of assumptions hold: - The property key must be a non-negative Int32 - The base object must not interfere with indexed property access - The property key must already be present as an own property - The existing value must not have any accessors defined If this holds (which it should in the common case), we can poke directly at the indexed property storage and save a boatload of time. 10% speed-up on the entire Kraken benchmark :^) (including: 31% speed-up on Kraken/audio-dft.js) (including: 23% speed-up on Kraken/stanford-crypto-aes.js) --- Userland/Libraries/LibJS/Bytecode/Interpreter.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 28e8ad572b..4c875fe5fc 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -1530,6 +1530,19 @@ ThrowCompletionOr GetByValue::execute_impl(Bytecode::Interpreter& interpre auto base_value = interpreter.reg(m_base); auto object = TRY(base_object_for_get(interpreter, base_value)); + + // OPTIMIZATION: Fast path for simple Int32 indexes in array-like objects. + if (property_key_value.is_int32() + && property_key_value.as_i32() >= 0 + && !object->may_interfere_with_indexed_property_access() + && object->indexed_properties().has_index(property_key_value.as_i32())) { + auto value = object->indexed_properties().get(property_key_value.as_i32())->value; + if (!value.is_accessor()) { + interpreter.accumulator() = value; + return {}; + } + } + auto property_key = TRY(property_key_value.to_property_key(vm)); if (base_value.is_string()) {