LibJS: Make overflowing arithmetic on 2x Int32 values faster
Some checks are pending
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / 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

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
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 {};