1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 13:37:10 +09:00
Commit graph

1536 commits

Author SHA1 Message Date
Aliaksandr Kalenik
db480b1f0c LibJS: Preserve information about local variables declaration kind
This is required for upcoming change where we want to emit ThrowIfTDZ
for assignment expressions only for lexical declarations.
2025-05-06 12:06:23 +02:00
Andreas Kling
183c847c80 LibJS: Cache PutById to setters in the prototype chain
This is *extremely* common on the web, but barely shows up at all in
JavaScript benchmarks.

A typical example is setting Element.innerHTML on a HTMLDivElement.
HTMLDivElement doesn't have innerHTML, so it has to travel up the
prototype chain until it finds it.

Before this change, we didn't cache this at all, so we had to travel
the prototype chain every time a setter like this was used.

We now use the same mechanism we already had for GetBydId and cache
PutById setter accesses in the prototype chain as well.

1.74x speedup on MicroBench/setter-in-prototype-chain.js
2025-05-05 15:21:43 +02:00
Timothy Flynn
01791c5a52 LibJS: Mark sync module evaluation promise as handled
This is a normative change in the ECMA-262 spec. See:
0fb1859

As noted in the PR for this change, this is not actually testable via
either test262 or WPT.
2025-05-05 17:50:18 +12:00
Andreas Kling
bf1b754e91 LibJS: Optimize reading known-to-be-initialized var bindings
`var` bindings are never in the temporal dead zone (TDZ), and so we
know accessing them will not throw.

We now take advantage of this by having a specialized environment
binding value getter that doesn't check for exceptional cases.

1.08x speedup on JetStream.
2025-05-04 02:31:18 +02:00
Andreas Kling
ad7c1e147f LibJS: Add SetGlobal bytecode instruction for cached global writes
Before this change, setting a global would end up as SetLexicalBinding.
That instruction always failed to cache the access if the global was a
property of the global object.

1.14x speedup on Octane/earley-boyer.js
2.04x speedup on MicroBench/for-of.js

Note that MicroBench/for-of.js was more of a "set global" benchmark
before this. After this change, it's actually a for..of benchmark. :^)
2025-05-04 01:58:57 +02:00
Andreas Kling
edb5547e37 LibJS: Don't incrementally delete PropertyNameIterator while iterating
We were spending a lot of time removing each property name from the
iterator's underlying HashMap while iterating over it. This wasn't
actually necessary, so let's stop doing it and instead just iterate
over the property names with a stored HashTable iterator.

1.10x speedup on MicroBench/for-in-indexed-properties.js
2025-05-03 22:00:51 +02:00
Andreas Kling
570d1e8811 LibJS: Use PrimitiveString more instead of Utf16String in String code
This avoids a whole lot of unnecessary roundtrips from UTF8 <=> UTF16.
2025-05-03 20:01:20 +02:00
Andreas Kling
98fef16972 LibJS: Use PrimitiveString more instead of Utf16String in RegExp code
PrimitiveString has an internal UTF-16 string cache anyway, and so this
actually avoids repeatedly converting between UTF-8 and UTF-16.
2025-05-03 20:01:20 +02:00
Andreas Kling
7c26354563 LibJS: Avoid Value->PropertyKey->Value roundtrip in for..in iteration
Before this change, we would call [[OwnPropertyKeys]] on the target
objects, then convert the returned keys from Value into PropertyKey.
Then, when actually iterating, we'd convert them back into Value again.
This was particularly costly for numeric property keys, since we had
to go through string-from-number construction.

Now, we simply keep the original values returned by [[OwnPropertyKeys]]
around and use them for the enumeration.

1.09x speedup on MicroBench/for-in-indexed-properties.js
1.01x speedup on MicroBench/for-in-named-properties.js
2025-05-03 17:33:54 +02:00
Timothy Flynn
8a80ff7b3b AK+LibJS: Use simdutf for all base64 operations
We were previously unable to use simdutf for base64 decoding operations
other than "loose". Upstream has added support for the "strict" and
"stop-before-partial" operations, so let's make use of them!
2025-05-03 11:21:10 -04:00
Shannon Booth
e476d21ed0 LibJS: Add and use PrimitiveString::length_in_utf16_code_units
I was investigating an optimization in this area, and while it
didn't seem to have a noticable improvement, it still seems
useful to apply this change.
2025-05-03 16:18:47 +02:00
Andreas Kling
e537e426c1 LibJS: Teach for..in iterator to use IteratorNextUnpack optimization
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
"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
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