Commit graph

1525 commits

Author SHA1 Message Date
Andreas Kling
e537e426c1 LibJS: Teach for..in iterator to use IteratorNextUnpack optimization
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
Even though this code was already optimized to re-use a single result
object, returning { value, done } directly in output parameters still
provides a substantial speedup.

1.21x speedup on MicroBench/for-in-indexed-properties.js
2025-05-03 11:43:57 +02:00
Andreas Kling
0ef6444824 LibJS: Replace some use of ByteString with String
1.19x speedup on MicroBench/for-in-indexed-properties.js
2025-05-03 08:08:04 +02:00
Andreas Kling
2ef2e75cdc LibJS: Reduce HashTable rehashing in get_object_property_iterator()
Apply a little ensure_capacity() to avoid excessive rehashing of the
property key table when enumerating a large number of properties.

1.23x speedup on MicroBench/for-in-indexed-properties.js
2025-05-03 08:08:04 +02:00
Aliaksandr Kalenik
6426586052 LibJS: Skip "return" method call while closing built-in iterators
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
"return" method is not defined on any of builtin iterators, so we could
skip it, avoiding method lookup.

I measured 10% improvement in array-destructuring-assignment.js micro
benchmark with this change.
2025-05-01 18:17:06 +02:00
Aliaksandr Kalenik
60bd5012fe LibJS: Optimize array destructuring assignment for builtin iterators
...by avoiding `{ value, done }` iterator result value allocation. This
change applies the same otimization 81b6a11 added for `for..in` and
`for..of`.

Makes following micro benchmark go 22% faster on my computer:
```js
function f() {
    const arr = [];
    for (let i = 0; i < 10_000_000; i++) {
        arr.push([i]);
    }
    let sum = 0;
    for (let [i] of arr) {
        sum += i;
    }
}

f();
```
2025-05-01 16:57:56 +03:00
Aliaksandr Kalenik
81b6a1100e LibJS: Skip iterator result object allocation in for..of and for..in
Introduce special instruction for `for..of` and `for..in` loop that
skips `{ value, done }` result object allocation if iterator is builtin
(array, map, set, string). This reduces GC pressure significantly and
avoids extracting the `value` and `done` properties.

This change makes this micro benchmark 48% faster on my computer:
```js
const arr = new Array(10_000_000);
let counter = 0;
for (let _ of arr) {
    counter++;
}
```
2025-04-30 20:51:39 +02:00
Aliaksandr Kalenik
ab52d86a69 LibJS: Allow advancing built-in iterators without result object creation
Expose a method on built-in iterators that allows retrieving the next
iteration result without allocating a JS::Object. This change is a
preparation for optimizing for..of and for..in loops.
2025-04-30 20:51:39 +02:00
Timothy Flynn
6b4b7a54de LibJS: Revert ArrayIterator and RegExpStringIterator to manual iterators
This is a normative change in the ECMA-262 spec. See:
de62e8d

This did not actually seem to affect our implementation as we were not
using generators here to begin with. So this patch is basically just
adding spec comments.
2025-04-30 07:29:34 -04:00
Andreas Kling
7a600e60bc LibJS: Mark catch parameter as initialized local when applicable
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
Otherwise we'll emit a TDZ check for every `catch` parameter immediately
after initializing it. This check would never fail, so it's redundant.
2025-04-30 09:38:40 +02:00
Andreas Kling
00f7a6f9e0 LibJS: Remove unused bytecode VM register reservation
We were not actually using the "saved exception" register for anything,
but we were clearing it on every function entry.
2025-04-30 09:38:40 +02:00
Andreas Kling
1f0c67cc12 Revert "LibJS: Add StackFrame to avoid indirection in VM register access"
This reverts commit 36bb2824a6.

Although this was faster on my M3 MacBook Pro, other Apple machines
disagree, including our benchmark runner. So let's revert it.
2025-04-29 16:08:42 +02:00
Andreas Kling
36bb2824a6 LibJS: Add StackFrame to avoid indirection in VM register access
This is a simple trick to generate better native code for access to
registers, locals, and constants. Before this change, each access had
to first dereference the member pointer in Interpreter, and then get to
the values. Now we always have a pointer directly to the values on hand.

Here's how it looks:

    class StackFrame {
    public:
        Value get(Operand) const;
        void set(Operand, Value);

    private:
        Value m_values[];
    };

And we just place one of these as a window on top of the execution
context's array of values (registers, locals, and constants).
2025-04-29 14:23:03 +02:00
Timothy Flynn
4d70f6ce1c LibJS: Ensure iterator parameter validation closes underlying iterator
This is a normative change in the ECMA-262 spec. See:
9552f29
f2bad00
2025-04-29 07:33:08 -04:00
Timothy Flynn
908349f8fe LibJS: Add a TRY_OR_CLOSE_ITERATOR macro for IfAbruptCloseIterator
Behaves just like TRY_OR_REJECT for promises.
2025-04-29 07:33:08 -04:00
Timothy Flynn
568524f8ba LibJS: Close sync iterator when async wrapper yields rejection
This is a normative change in the ECMA-262 spec. See:
ff129b1
2025-04-29 07:33:08 -04:00
Timothy Flynn
15faaeb2bb LibJS: Remove [[VarNames]] from GlobalEnvironment
This is a normative change in the ECMA-262 spec. See:
ed75310
2025-04-29 07:33:08 -04:00
Timothy Flynn
9c85a16aeb LibJS: Standardize spec prose for Math.sumPrecise 2025-04-29 07:33:08 -04:00
Timothy Flynn
9674210ef8 LibJS: Update spec steps / links for the Float16Array proposal
This proposal has reached stage 4 and been merged into the main ECMA-262
spec. See:

d430ace
2025-04-29 07:33:08 -04:00
Timothy Flynn
adf6024805 LibJS: Update spec steps / links for the RegExp.escape proposal
This proposal has reached stage 4 and been merged into the main ECMA-262
spec. See:

e2da759
2025-04-29 07:33:08 -04:00
Timothy Flynn
2401764697 LibJS: Update spec steps / links for the JSON modules proposal
This proposal has reached stage 4 and been merged into the main ECMA-262
spec. See:

3feb1ba
2025-04-29 07:33:08 -04:00
Timothy Flynn
3867a192a1 LibJS: Update spec steps / links for the import-assertions proposal
This proposal has reached stage 4 and been merged into the main ECMA-262
spec. See:

4e3450e
2025-04-29 07:33:08 -04:00
Andreas Kling
6061da3382 LibJS: Overload Instruction::length() on variable-length instructions
This allows the DISPATCH_NEXT() macro to do the math to find the next
instruction boundary point without performing a function call.
2025-04-29 02:09:35 +02:00
Andreas Kling
a2b7e04da3 LibJS: Defer looking up the realm in ordinary_call_evaluate_body()
We don't actually need the realm for normal function calls, so we
can avoid looking it up on the EC stack in that case.
2025-04-29 02:09:35 +02:00
Andreas Kling
0f1be720bb LibJS: Mark exception path [[unlikely]] in ordinary_call_evaluate_body() 2025-04-29 02:09:35 +02:00
Andreas Kling
58925887ce LibJS: Inline VM::bytecode_interpreter() 2025-04-29 02:09:35 +02:00
Andreas Kling
35275651e3 LibJS: Skip stack overflow check in ESFO::prepare_for_ordinary_call()
We already do a stack overflow check when entering run_bytecode(),
which is the first thing that happens when we actually invoke the ESFO
executable.
2025-04-29 02:09:35 +02:00
Andreas Kling
95ba74d934 LibJS: Remove redundant VERIFY in run_executable()
Getting the running_execution_context() already verifies that the
execution context stack is non-empty, we don't need to do it separately
here as well.
2025-04-29 02:09:35 +02:00
Andreas Kling
6de1a0aeaf LibJS: Don't cache a pointer to accumulator register in run_bytecode()
The old accumulator register is really only used to pass the end
completion to the caller of run_bytecode() nowadays. As such, we don't
need to cache a pointer to it for fast access. One less thing to do
on run_bytecode() entry.
2025-04-29 02:09:35 +02:00
Andreas Kling
942ce2162d LibJS: Mark stack overflow path in run_bytecode() [[unlikely]] 2025-04-29 02:09:35 +02:00
Andreas Kling
4d17707b26 LibJS: Store bytecode VM program counter in ExecutionContext
This way it's always automatically correct, and we don't have to
manually flush it in push_execution_context().

~7% speedup on the MicroBench/call* tests :^)
2025-04-28 21:12:48 +02:00
Andreas Kling
233097c250 LibJS: Inline part of VM::run_queued_promise_jobs()
Most of the time there are no queued promise jobs to run after exiting
a stack frame. By moving the check inline, leaving a function call gets
a measurable speedup in the common case.
2025-04-28 10:39:42 -04:00
Andreas Kling
b4554c01db LibJS: Mark ESFO path for [[Call]] on a class constructor [[unlikely]]
This is an exception path that's not supposed to be called normally,
so let's mark it unlikely.
2025-04-28 10:39:42 -04:00
Andreas Kling
6ec4d0f5ba LibJS: Mark stack overflow exception code path as [[unlikely]]
This is supposed to be exceedingly rare, so a great candidate for
[[unlikely]] annotation.
2025-04-28 10:39:42 -04:00
Andreas Kling
074ca5d5b4 LibJS: Make ESFO::ordinary_call_evaluate_body() return TCO<Value>
This matches what the caller wants to return and allows us to simplify
a bunch of logic around returning a value or throwing.
2025-04-28 12:44:49 +02:00
Andreas Kling
670e439e1e LibJS: Put FLATTEN on ECMAScriptFunctionObject.[[Call]]
This makes function calls ~5% faster in micro-benchmarks on my MBP.
Basically free money on the table. Let's take it!
2025-04-28 12:44:49 +02:00
Andreas Kling
d0d87d3aed LibJS: Demote some overly paranoid VERIFY()s in ESFO [[Call]] flow 2025-04-28 12:44:49 +02:00
Andreas Kling
403ae86fd9 LibJS: Pass VM& to ECMAScriptFunctionObject [[Call]] helpers
This avoids fetching the VM from the Cell::private_data() repeatedly.
2025-04-28 12:44:49 +02:00
devgianlu
5f1a30197c LibCrypto: Remove the concept of invalid big integers
This concept is rarely used in codebase and very much error-prone
if you forget to check it.

Instead, make it so that operations that would produce invalid integers
return an error instead.
2025-04-28 12:05:26 +02:00
devgianlu
dd0cced92f LibJS: Prevent huge memory allocations for bigint left shift 2025-04-28 12:05:26 +02:00
devgianlu
a019efb24b LibCrypto+LibJS: Remove {Signed,Unsigned}BigInteger to_base_deprecated
Use `to_base` instead.
2025-04-28 12:05:26 +02:00
Andreas Kling
a05be67e4a LibJS: Let invokers (callers) of [[Call]] allocate ExecutionContext
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
Instead of letting every [[Call]] implementation allocate an
ExecutionContext, we now make that a responsibility of the caller.

The main point of this exercise is to allow the Call instruction
to write function arguments directly into the callee ExecutionContext
instead of copying them later.

This makes function calls significantly faster:
- 10-20% faster on micro-benchmarks (depending on argument count)
- 4% speedup on Kraken
- 2% speedup on Octane
- 5% speedup on JetStream
2025-04-28 01:23:56 +02:00
Andreas Kling
93788f8057 LibJS: Add parentheses to ALLOCATE_EXECUTION_CONTEXT_ON_NATIVE_STACK()
Just to stop clang-tidy from complaining about it every time.
2025-04-28 01:23:56 +02:00
Andreas Kling
373307db5b LibJS: Mark arguments local as initialized after creating it
This avoids emitting an unnecessary TDZ check for the first time we
access the arguments object.
2025-04-28 01:23:56 +02:00
Shannon Booth
9e44d86915 LibJS: Remove value_or from JS::Value
Some checks are pending
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
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
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
This is no longer used after 3cf5053.
2025-04-27 11:14:54 -04:00
Timothy Flynn
317cf65eb0 LibJS: Avoid naming conflict between Object's and Error's is_error
Object defines an is_error virtual method to be overridden by Error for
fast-is. This is the same name as the Error.isError constructor method.
Rename the former to avoid conflicts, as GCC 15 just started warning on
this.
2025-04-26 09:04:45 -06:00
Aliaksandr Kalenik
2d732b2251 LibJS: Skip allocating locals for arguments that allowed to be local
This allows us to get rid of instructions that move arguments to locals
and allocate smaller JS::Value vector in ExecutionContext by reusing
slots that were already allocated for arguments.

With this change for following function:
```js
function f(x, y) {
    return x + y;
}
```

we now produce following bytecode:
```
[   0]    0: Add dst:reg6, lhs:arg0, rhs:arg1
[  10]       Return value:reg6
```

instead of:
```
[   0]    0: GetArgument 0, dst:x~1
[  10]       GetArgument 1, dst:y~0
[  20]       Add dst:reg6, lhs:x~1, rhs:y~0
[  30]       Return value:reg6
```
2025-04-26 11:02:29 +02:00
Aliaksandr Kalenik
3f04d18ef7 LibJS: Add new operand type for function arguments
This allows us to directly access passed arguments instead of copying
them to register/local first using GetArgument instruction.
2025-04-26 11:02:29 +02:00
Aliaksandr Kalenik
81a3bfd492 LibJS: Allow using locals if arguments is used in strict mode
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
Previously we blocked using locals for function arguments whenever
`arguments` was mentioned in function body, however, this is not
necessary in strict mode, where mutations to the arguments object are
not reflected in the function arguments and vice versa.
2025-04-25 21:08:24 +02:00
Shannon Booth
5290ebfe19 LibJS: Switch Agent [[CanBlock]] slot to a enum member
It turns out it was a mistake to make this a virtual since
ServiceWorkerAgents are effectively the exact same as
DedicatedWorkerAgents and SharedWorkerAgents just with [[CanBlock]]
set to false.
2025-04-25 14:07:51 +02:00
Shannon Booth
7dd7e5b438 LibJS+LibWeb: Defer initialization of the Agent after VM constructor
This helps unwind a niggly depedency where the VM owns and constructs
the Heap and the Agent. But the agent wants to have customized
construction that depends on the heap. Solve this by defering
the initialization of the Agent to after we have constructed the
VM and the heap.
2025-04-25 14:07:51 +02:00