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

LibJS: Inline fast case for Value::to_{boolean,number,numeric,primitive}

These functions all have a very common case that can be dealt with a
very simple inline check, often avoiding the need to call an out-of-line
function. This patch moves the common case to inline functions in a new
ValueInlines.h header (necessary due to header dependency issues..)

8% speed-up on the entire Kraken benchmark :^)
This commit is contained in:
Andreas Kling 2023-10-06 17:54:21 +02:00
parent 42d80aab06
commit 65717e3b75
53 changed files with 111 additions and 8 deletions

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
inline bool Value::to_boolean() const
{
// OPTIMIZATION: Fast path for when this value is already a boolean.
if (is_boolean())
return as_bool();
return to_boolean_slow_case();
}
inline ThrowCompletionOr<Value> Value::to_number(VM& vm) const
{
// OPTIMIZATION: Fast path for when this value is already a number.
if (is_number())
return *this;
return to_number_slow_case(vm);
}
inline ThrowCompletionOr<Value> Value::to_numeric(VM& vm) const
{
// OPTIMIZATION: Fast path for when this value is already a number.
if (is_number())
return *this;
return to_numeric_slow_case(vm);
}
inline ThrowCompletionOr<Value> Value::to_primitive(VM& vm, PreferredType preferred_type) const
{
if (!is_object())
return *this;
return to_primitive_slow_case(vm, preferred_type);
}
}