LibJS: Avoid roundtrip through Value for comparison bytecode evaluation
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run

1.1x speedup on strictly-equals-object.js
This commit is contained in:
Shannon Booth 2025-05-08 15:19:35 +12:00 committed by Andreas Kling
commit 19bf897116
Notes: github-actions[bot] 2025-05-08 18:40:23 +00:00
4 changed files with 42 additions and 42 deletions

View file

@ -113,13 +113,13 @@ static ThrowCompletionOr<ScopedOperand> constant_fold_binary_expression(Generato
case BinaryOp::Exponentiation: case BinaryOp::Exponentiation:
return generator.add_constant(TRY(exp(generator.vm(), lhs, rhs))); return generator.add_constant(TRY(exp(generator.vm(), lhs, rhs)));
case BinaryOp::GreaterThan: case BinaryOp::GreaterThan:
return generator.add_constant(TRY(greater_than(generator.vm(), lhs, rhs))); return generator.add_constant(Value { TRY(greater_than(generator.vm(), lhs, rhs)) });
case BinaryOp::GreaterThanEquals: case BinaryOp::GreaterThanEquals:
return generator.add_constant(TRY(greater_than_equals(generator.vm(), lhs, rhs))); return generator.add_constant(Value { TRY(greater_than_equals(generator.vm(), lhs, rhs)) });
case BinaryOp::LessThan: case BinaryOp::LessThan:
return generator.add_constant(TRY(less_than(generator.vm(), lhs, rhs))); return generator.add_constant(Value { TRY(less_than(generator.vm(), lhs, rhs)) });
case BinaryOp::LessThanEquals: case BinaryOp::LessThanEquals:
return generator.add_constant(TRY(less_than_equals(generator.vm(), lhs, rhs))); return generator.add_constant(Value { TRY(less_than_equals(generator.vm(), lhs, rhs)) });
case BinaryOp::LooselyInequals: case BinaryOp::LooselyInequals:
return generator.add_constant(Value(!TRY(is_loosely_equal(generator.vm(), lhs, rhs)))); return generator.add_constant(Value(!TRY(is_loosely_equal(generator.vm(), lhs, rhs))));
case BinaryOp::LooselyEquals: case BinaryOp::LooselyEquals:

View file

@ -142,40 +142,40 @@ static ByteString format_value_list(StringView name, ReadonlySpan<Value> values)
return builder.to_byte_string(); return builder.to_byte_string();
} }
ALWAYS_INLINE static ThrowCompletionOr<Value> loosely_inequals(VM& vm, Value src1, Value src2) ALWAYS_INLINE static ThrowCompletionOr<bool> loosely_inequals(VM& vm, Value src1, Value src2)
{ {
if (src1.tag() == src2.tag()) { if (src1.tag() == src2.tag()) {
if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish()) if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
return Value(src1.encoded() != src2.encoded()); return src1.encoded() != src2.encoded();
} }
return Value(!TRY(is_loosely_equal(vm, src1, src2))); return !TRY(is_loosely_equal(vm, src1, src2));
} }
ALWAYS_INLINE static ThrowCompletionOr<Value> loosely_equals(VM& vm, Value src1, Value src2) ALWAYS_INLINE static ThrowCompletionOr<bool> loosely_equals(VM& vm, Value src1, Value src2)
{ {
if (src1.tag() == src2.tag()) { if (src1.tag() == src2.tag()) {
if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish()) if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
return Value(src1.encoded() == src2.encoded()); return src1.encoded() == src2.encoded();
} }
return Value(TRY(is_loosely_equal(vm, src1, src2))); return TRY(is_loosely_equal(vm, src1, src2));
} }
ALWAYS_INLINE static ThrowCompletionOr<Value> strict_inequals(VM&, Value src1, Value src2) ALWAYS_INLINE static ThrowCompletionOr<bool> strict_inequals(VM&, Value src1, Value src2)
{ {
if (src1.tag() == src2.tag()) { if (src1.tag() == src2.tag()) {
if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish()) if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
return Value(src1.encoded() != src2.encoded()); return src1.encoded() != src2.encoded();
} }
return Value(!is_strictly_equal(src1, src2)); return !is_strictly_equal(src1, src2);
} }
ALWAYS_INLINE static ThrowCompletionOr<Value> strict_equals(VM&, Value src1, Value src2) ALWAYS_INLINE static ThrowCompletionOr<bool> strict_equals(VM&, Value src1, Value src2)
{ {
if (src1.tag() == src2.tag()) { if (src1.tag() == src2.tag()) {
if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish()) if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
return Value(src1.encoded() == src2.encoded()); return src1.encoded() == src2.encoded();
} }
return Value(is_strictly_equal(src1, src2)); return is_strictly_equal(src1, src2);
} }
Interpreter::Interpreter(VM& vm) Interpreter::Interpreter(VM& vm)
@ -486,7 +486,7 @@ FLATTEN_ON_CLANG void Interpreter::run_bytecode(size_t entry_point)
return; \ return; \
goto start; \ goto start; \
} \ } \
if (result.value().to_boolean()) \ if (result.value()) \
program_counter = instruction.true_target().address(); \ program_counter = instruction.true_target().address(); \
else \ else \
program_counter = instruction.false_target().address(); \ program_counter = instruction.false_target().address(); \
@ -1932,7 +1932,7 @@ void Dump::execute_impl(Bytecode::Interpreter& interpreter) const
auto& vm = interpreter.vm(); \ auto& vm = interpreter.vm(); \
auto lhs = interpreter.get(m_lhs); \ auto lhs = interpreter.get(m_lhs); \
auto rhs = interpreter.get(m_rhs); \ auto rhs = interpreter.get(m_rhs); \
interpreter.set(m_dst, TRY(op_snake_case(vm, lhs, rhs))); \ interpreter.set(m_dst, Value { TRY(op_snake_case(vm, lhs, rhs)) }); \
return {}; \ return {}; \
} }
@ -2106,7 +2106,7 @@ ThrowCompletionOr<void> LessThan::execute_impl(Bytecode::Interpreter& interprete
interpreter.set(m_dst, Value(lhs.as_double() < rhs.as_double())); interpreter.set(m_dst, Value(lhs.as_double() < rhs.as_double()));
return {}; return {};
} }
interpreter.set(m_dst, TRY(less_than(vm, lhs, rhs))); interpreter.set(m_dst, Value { TRY(less_than(vm, lhs, rhs)) });
return {}; return {};
} }
@ -2123,7 +2123,7 @@ ThrowCompletionOr<void> LessThanEquals::execute_impl(Bytecode::Interpreter& inte
interpreter.set(m_dst, Value(lhs.as_double() <= rhs.as_double())); interpreter.set(m_dst, Value(lhs.as_double() <= rhs.as_double()));
return {}; return {};
} }
interpreter.set(m_dst, TRY(less_than_equals(vm, lhs, rhs))); interpreter.set(m_dst, Value { TRY(less_than_equals(vm, lhs, rhs)) });
return {}; return {};
} }
@ -2140,7 +2140,7 @@ ThrowCompletionOr<void> GreaterThan::execute_impl(Bytecode::Interpreter& interpr
interpreter.set(m_dst, Value(lhs.as_double() > rhs.as_double())); interpreter.set(m_dst, Value(lhs.as_double() > rhs.as_double()));
return {}; return {};
} }
interpreter.set(m_dst, TRY(greater_than(vm, lhs, rhs))); interpreter.set(m_dst, Value { TRY(greater_than(vm, lhs, rhs)) });
return {}; return {};
} }
@ -2157,7 +2157,7 @@ ThrowCompletionOr<void> GreaterThanEquals::execute_impl(Bytecode::Interpreter& i
interpreter.set(m_dst, Value(lhs.as_double() >= rhs.as_double())); interpreter.set(m_dst, Value(lhs.as_double() >= rhs.as_double()));
return {}; return {};
} }
interpreter.set(m_dst, TRY(greater_than_equals(vm, lhs, rhs))); interpreter.set(m_dst, Value { TRY(greater_than_equals(vm, lhs, rhs)) });
return {}; return {};
} }

View file

@ -1261,7 +1261,7 @@ ThrowCompletionOr<GC::Ptr<FunctionObject>> Value::get_method(VM& vm, PropertyKey
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
// RelationalExpression : RelationalExpression > ShiftExpression // RelationalExpression : RelationalExpression > ShiftExpression
ThrowCompletionOr<Value> greater_than(VM& vm, Value lhs, Value rhs) ThrowCompletionOr<bool> greater_than(VM& vm, Value lhs, Value rhs)
{ {
// 1. Let lref be ? Evaluation of RelationalExpression. // 1. Let lref be ? Evaluation of RelationalExpression.
// 2. Let lval be ? GetValue(lref). // 2. Let lval be ? GetValue(lref).
@ -1278,13 +1278,13 @@ ThrowCompletionOr<Value> greater_than(VM& vm, Value lhs, Value rhs)
// 6. If r is undefined, return false. Otherwise, return r. // 6. If r is undefined, return false. Otherwise, return r.
if (relation == TriState::Unknown) if (relation == TriState::Unknown)
return Value(false); return false;
return Value(relation == TriState::True); return relation == TriState::True;
} }
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
// RelationalExpression : RelationalExpression >= ShiftExpression // RelationalExpression : RelationalExpression >= ShiftExpression
ThrowCompletionOr<Value> greater_than_equals(VM& vm, Value lhs, Value rhs) ThrowCompletionOr<bool> greater_than_equals(VM& vm, Value lhs, Value rhs)
{ {
// 1. Let lref be ? Evaluation of RelationalExpression. // 1. Let lref be ? Evaluation of RelationalExpression.
// 2. Let lval be ? GetValue(lref). // 2. Let lval be ? GetValue(lref).
@ -1301,13 +1301,13 @@ ThrowCompletionOr<Value> greater_than_equals(VM& vm, Value lhs, Value rhs)
// 6. If r is true or undefined, return false. Otherwise, return true. // 6. If r is true or undefined, return false. Otherwise, return true.
if (relation == TriState::Unknown || relation == TriState::True) if (relation == TriState::Unknown || relation == TriState::True)
return Value(false); return false;
return Value(true); return true;
} }
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
// RelationalExpression : RelationalExpression < ShiftExpression // RelationalExpression : RelationalExpression < ShiftExpression
ThrowCompletionOr<Value> less_than(VM& vm, Value lhs, Value rhs) ThrowCompletionOr<bool> less_than(VM& vm, Value lhs, Value rhs)
{ {
// 1. Let lref be ? Evaluation of RelationalExpression. // 1. Let lref be ? Evaluation of RelationalExpression.
// 2. Let lval be ? GetValue(lref). // 2. Let lval be ? GetValue(lref).
@ -1324,13 +1324,13 @@ ThrowCompletionOr<Value> less_than(VM& vm, Value lhs, Value rhs)
// 6. If r is undefined, return false. Otherwise, return r. // 6. If r is undefined, return false. Otherwise, return r.
if (relation == TriState::Unknown) if (relation == TriState::Unknown)
return Value(false); return false;
return Value(relation == TriState::True); return relation == TriState::True;
} }
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
// RelationalExpression : RelationalExpression <= ShiftExpression // RelationalExpression : RelationalExpression <= ShiftExpression
ThrowCompletionOr<Value> less_than_equals(VM& vm, Value lhs, Value rhs) ThrowCompletionOr<bool> less_than_equals(VM& vm, Value lhs, Value rhs)
{ {
// 1. Let lref be ? Evaluation of RelationalExpression. // 1. Let lref be ? Evaluation of RelationalExpression.
// 2. Let lval be ? GetValue(lref). // 2. Let lval be ? GetValue(lref).
@ -1347,8 +1347,8 @@ ThrowCompletionOr<Value> less_than_equals(VM& vm, Value lhs, Value rhs)
// 6. If r is true or undefined, return false. Otherwise, return true. // 6. If r is true or undefined, return false. Otherwise, return true.
if (relation == TriState::True || relation == TriState::Unknown) if (relation == TriState::True || relation == TriState::Unknown)
return Value(false); return false;
return Value(true); return true;
} }
// 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators

View file

@ -461,10 +461,10 @@ private:
friend constexpr Value js_undefined(); friend constexpr Value js_undefined();
friend constexpr Value js_null(); friend constexpr Value js_null();
friend constexpr Value js_special_empty_value(); friend constexpr Value js_special_empty_value();
friend ThrowCompletionOr<Value> greater_than(VM&, Value lhs, Value rhs); friend ThrowCompletionOr<bool> greater_than(VM&, Value lhs, Value rhs);
friend ThrowCompletionOr<Value> greater_than_equals(VM&, Value lhs, Value rhs); friend ThrowCompletionOr<bool> greater_than_equals(VM&, Value lhs, Value rhs);
friend ThrowCompletionOr<Value> less_than(VM&, Value lhs, Value rhs); friend ThrowCompletionOr<bool> less_than(VM&, Value lhs, Value rhs);
friend ThrowCompletionOr<Value> less_than_equals(VM&, Value lhs, Value rhs); friend ThrowCompletionOr<bool> less_than_equals(VM&, Value lhs, Value rhs);
friend ThrowCompletionOr<Value> add(VM&, Value lhs, Value rhs); friend ThrowCompletionOr<Value> add(VM&, Value lhs, Value rhs);
friend bool same_value_non_number(Value lhs, Value rhs); friend bool same_value_non_number(Value lhs, Value rhs);
}; };
@ -499,10 +499,10 @@ inline Value js_negative_infinity()
return Value(-INFINITY); return Value(-INFINITY);
} }
ThrowCompletionOr<Value> greater_than(VM&, Value lhs, Value rhs); ThrowCompletionOr<bool> greater_than(VM&, Value lhs, Value rhs);
ThrowCompletionOr<Value> greater_than_equals(VM&, Value lhs, Value rhs); ThrowCompletionOr<bool> greater_than_equals(VM&, Value lhs, Value rhs);
ThrowCompletionOr<Value> less_than(VM&, Value lhs, Value rhs); ThrowCompletionOr<bool> less_than(VM&, Value lhs, Value rhs);
ThrowCompletionOr<Value> less_than_equals(VM&, Value lhs, Value rhs); ThrowCompletionOr<bool> less_than_equals(VM&, Value lhs, Value rhs);
ThrowCompletionOr<Value> bitwise_and(VM&, Value lhs, Value rhs); ThrowCompletionOr<Value> bitwise_and(VM&, Value lhs, Value rhs);
ThrowCompletionOr<Value> bitwise_or(VM&, Value lhs, Value rhs); ThrowCompletionOr<Value> bitwise_or(VM&, Value lhs, Value rhs);
ThrowCompletionOr<Value> bitwise_xor(VM&, Value lhs, Value rhs); ThrowCompletionOr<Value> bitwise_xor(VM&, Value lhs, Value rhs);