From c02535e9f9ce709dbe77dad085898673e689b315 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Mon, 26 May 2025 13:54:46 +0300 Subject: [PATCH] LibJS: Use CallBuiltin for Math.sin() Improves performance on https://pierre.co/ --- Libraries/LibJS/Bytecode/Builtins.h | 3 ++- Libraries/LibJS/Bytecode/Interpreter.cpp | 2 ++ Libraries/LibJS/Runtime/MathObject.cpp | 11 ++++++++--- Libraries/LibJS/Runtime/MathObject.h | 2 ++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Libraries/LibJS/Bytecode/Builtins.h b/Libraries/LibJS/Bytecode/Builtins.h index 270b68e7c50..b7ea0a63451 100644 --- a/Libraries/LibJS/Bytecode/Builtins.h +++ b/Libraries/LibJS/Bytecode/Builtins.h @@ -22,7 +22,8 @@ namespace JS::Bytecode { O(MathImul, math_imul, Math, imul, 2) \ O(MathRandom, math_random, Math, random, 0) \ O(MathRound, math_round, Math, round, 1) \ - O(MathSqrt, math_sqrt, Math, sqrt, 1) + O(MathSqrt, math_sqrt, Math, sqrt, 1) \ + O(MathSin, math_sin, Math, sin, 1) enum class Builtin : u8 { #define DEFINE_BUILTIN_ENUM(name, ...) name, diff --git a/Libraries/LibJS/Bytecode/Interpreter.cpp b/Libraries/LibJS/Bytecode/Interpreter.cpp index 2815ac10270..542a8fef729 100644 --- a/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -2819,6 +2819,8 @@ static ThrowCompletionOr dispatch_builtin_call(Bytecode::Interpreter& int return TRY(MathObject::round_impl(interpreter.vm(), interpreter.get(arguments[0]))); case Builtin::MathSqrt: return TRY(MathObject::sqrt_impl(interpreter.vm(), interpreter.get(arguments[0]))); + case Builtin::MathSin: + return TRY(MathObject::sin_impl(interpreter.vm(), interpreter.get(arguments[0]))); case Bytecode::Builtin::__Count: VERIFY_NOT_REACHED(); } diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 4e0452dbad3..9a0e5214ea3 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -42,7 +42,7 @@ void MathObject::initialize(Realm& realm) define_native_function(realm, vm.names.max, max, 2, attr); define_native_function(realm, vm.names.min, min, 2, attr); define_native_function(realm, vm.names.trunc, trunc, 1, attr); - define_native_function(realm, vm.names.sin, sin, 1, attr); + define_native_function(realm, vm.names.sin, sin, 1, attr, Bytecode::Builtin::MathSin); define_native_function(realm, vm.names.cos, cos, 1, attr); define_native_function(realm, vm.names.tan, tan, 1, attr); define_native_function(realm, vm.names.pow, pow, 2, attr, Bytecode::Builtin::MathPow); @@ -905,10 +905,10 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sign) } // 21.3.2.31 Math.sin ( x ), https://tc39.es/ecma262/#sec-math.sin -JS_DEFINE_NATIVE_FUNCTION(MathObject::sin) +ThrowCompletionOr MathObject::sin_impl(VM& vm, Value value) { // 1. Let n be ? ToNumber(x). - auto number = TRY(vm.argument(0).to_number(vm)); + auto number = TRY(value.to_number(vm)); // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero()) @@ -922,6 +922,11 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sin) return Value(::sin(number.as_double())); } +JS_DEFINE_NATIVE_FUNCTION(MathObject::sin) +{ + return sin_impl(vm, vm.argument(0)); +} + // 21.3.2.32 Math.sinh ( x ), https://tc39.es/ecma262/#sec-math.sinh JS_DEFINE_NATIVE_FUNCTION(MathObject::sinh) { diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h index 19d5efd84ad..73f371f7c3d 100644 --- a/Libraries/LibJS/Runtime/MathObject.h +++ b/Libraries/LibJS/Runtime/MathObject.h @@ -28,6 +28,8 @@ public: static ThrowCompletionOr abs_impl(VM&, Value); static ThrowCompletionOr sum_precise_impl(VM&, Value); static ThrowCompletionOr imul_impl(VM&, Value, Value); + static ThrowCompletionOr sin_impl(VM&, Value); + static Value random_impl(); private: