From 111622a1649d61e212563cb327058bd272c1e8b7 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 4 Oct 2023 15:48:29 +0200 Subject: [PATCH] LibJS: Add fast path for multiplying two Int32 values We can avoid a lot of work here, as long as the result doesn't overflow the Int32 range. 5% speed-up on Kraken/imaging-gaussian-blur.js :^) --- Userland/Libraries/LibJS/Runtime/Value.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 4cc787169d..5e3176ab3e 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -1818,6 +1818,14 @@ ThrowCompletionOr sub(VM& vm, Value lhs, Value rhs) // MultiplicativeExpression : MultiplicativeExpression MultiplicativeOperator ExponentiationExpression ThrowCompletionOr mul(VM& vm, Value lhs, Value rhs) { + // OPTIMIZATION: Fast path for multiplication of two Int32 values. + if (lhs.is_int32() && rhs.is_int32()) { + Checked result = lhs.as_i32(); + result *= rhs.as_i32(); + if (!result.has_overflow()) + return result.value(); + } + // 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator // 1-2, 6. N/A.