LibJS: Make overflowing arithmetic on 2x Int32 values faster

Instead of converting them to doubles and doing double math, just do the
arithmetic operation in i64 space instead.

This gives us a ~1.25x speed-up on this kind of micro-benchmark:

    (() => {
        let a = -2124299999;
        for (let i = 0; i < 100_000_000; ++i) {
            a + a;
        }
    })()

Same idea for Add, Sub, and Mul.

There's a fair bit of overflowing Int32 arithmetic in some of the
JetStream benchmarks, and this seems like an obvious improvement.
This commit is contained in:
Andreas Kling 2025-10-07 12:08:06 +02:00 committed by Andreas Kling
parent d696f107be
commit 38e224cd9f
Notes: github-actions[bot] 2025-10-07 16:29:40 +00:00
2 changed files with 16 additions and 0 deletions

View file

@ -1949,6 +1949,9 @@ ThrowCompletionOr<void> Add::execute_impl(Bytecode::Interpreter& interpreter) co
interpreter.set(m_dst, Value(lhs.as_i32() + rhs.as_i32()));
return {};
}
auto result = static_cast<i64>(lhs.as_i32()) + static_cast<i64>(rhs.as_i32());
interpreter.set(m_dst, Value(result, Value::CannotFitInInt32::Indeed));
return {};
}
interpreter.set(m_dst, Value(lhs.as_double() + rhs.as_double()));
return {};
@ -1970,6 +1973,9 @@ ThrowCompletionOr<void> Mul::execute_impl(Bytecode::Interpreter& interpreter) co
interpreter.set(m_dst, Value(lhs.as_i32() * rhs.as_i32()));
return {};
}
auto result = static_cast<i64>(lhs.as_i32()) * static_cast<i64>(rhs.as_i32());
interpreter.set(m_dst, Value(result, Value::CannotFitInInt32::Indeed));
return {};
}
interpreter.set(m_dst, Value(lhs.as_double() * rhs.as_double()));
return {};
@ -1991,6 +1997,9 @@ ThrowCompletionOr<void> Sub::execute_impl(Bytecode::Interpreter& interpreter) co
interpreter.set(m_dst, Value(lhs.as_i32() - rhs.as_i32()));
return {};
}
auto result = static_cast<i64>(lhs.as_i32()) - static_cast<i64>(rhs.as_i32());
interpreter.set(m_dst, Value(result, Value::CannotFitInInt32::Indeed));
return {};
}
interpreter.set(m_dst, Value(lhs.as_double() - rhs.as_double()));
return {};