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

127 commits

Author SHA1 Message Date
Aliaksandr Kalenik
285bc005cb LibJS: Do more comprehensive check if next() fast path is possible
Before this change each built-in iterator object has a boolean
`m_next_method_was_redefined`. If user code later changed the iterator’s
prototype (e.g. `Object.setPrototypeOf()`), we still believed the
built-in fast-path was safe and skipped the user supplied override,
producing wrong results.

With this change
`BuiltinIterator::as_builtin_iterator_if_next_is_not_redefined()` looks
up the current `next` property and verifies that it is still the
built-in native function.
2025-06-02 00:15:36 +02:00
Julien Le Bras
3ba6d129df LibJS: Cache string constants in Generator::add_constant
This mirrors the existing caching logic for int32 constants.
Avoids duplication of string constants in m_constants which could
result in stack overflows for large scripts with a lot of similar
strings.
2025-06-01 18:25:59 +02:00
R-Goc
96c197faf1 LibJS: Add minimum changes to build on Windows and run js.exe
This commit adds the minimal export macros needed to run js.exe on
windows. A followup commit is planned to move to explicit export
entirely.

A static_assert for the size of a struct is also ifdef'ed out as the
semantics around object layout and inheritance are different on MSVC abi
and the struct IteratorRecord ends up being 40 bytes not 32.
2025-05-29 03:26:23 -06:00
Aliaksandr Kalenik
1647d7b34c LibJS: Use CallBuiltin for Math.tan() 2025-05-26 21:52:43 +02:00
Aliaksandr Kalenik
878cc16d7a LibJS: Use CallBuiltin for Math.cos() 2025-05-26 21:52:43 +02:00
Aliaksandr Kalenik
c02535e9f9 LibJS: Use CallBuiltin for Math.sin()
Improves performance on https://pierre.co/
2025-05-26 21:52:43 +02:00
Shannon Booth
f2fb86abea LibJS: Always emit value in emit_named_evaluation_if_anonymous_function
There does not appear to be any case that we need to return an
OptionalNone{}.
2025-05-23 03:25:55 +02:00
Shannon Booth
20dba8f1ab LibJS: Make Op::Return value required
It turns out we do not have any scenario where this is not provided.
2025-05-23 03:25:55 +02:00
Aliaksandr Kalenik
95e1ec4abc LibJS: Skip caching get_by_id() if object's shape is changed by a getter
Fixes a bug that reproduces with the following steps:
1. Create an object with a getter for property "a" in its prototype,
   where the getter adds an "a" property to the object itself.
2. Call the "a" getter in a loop for the first time. This triggers
   caching of metadata indicating that the "a" property is located in
   the prototype chain.
3. Call the "a" getter in a loop for the second time. Oops, the cache
   says the getter is in the prototype chain, but the object now
   also has its own "a" property that was added by the first getter
   call.
2025-05-20 19:10:56 -04:00
Aliaksandr Kalenik
b559965448 LibJS+LibWeb: Replace StringOrSymbol usage with PropertyKey
- Avoids unnecessary conversions between StringOrSymbol and PropertyKey
  on the hot path of property access.
- Simplifies the code by removing StringOrSymbol and using PropertyKey
  directly. There was no reason to have a separate StringOrSymbol type
  representing the same data as PropertyKey, just with the index key
  stored as a string.

PropertyKey has been updated to use a tagged pointer instead of a
Variant, so it still occupies 8 bytes, same as StringOrSymbol.

12% improvement on JetStream/gcc-loops.cpp.js
12% improvement on MicroBench/object-assign.js
7% improvement on MicroBench/object-keys.js
2025-05-17 10:08:37 -04:00
Tim Ledbetter
2903defcfc Revert "LibJS+LibWeb: Return Vector<PropertyKey> from…
internal_own_property_keys"

This reverts commit 5ee810f772.
2025-05-16 06:33:09 +01:00
Aliaksandr Kalenik
5ee810f772 LibJS+LibWeb: Return Vector<PropertyKey> from internal_own_property_keys
By doing that we avoid lots of `PropertyKey` -> `Value` -> `PropertyKey`
transforms, which are quite expensive because of underlying
`FlyString` -> `PrimitiveString` -> `FlyString` conversions.

10% improvement on MicroBench/object-keys.js
2025-05-15 14:12:18 -04:00
R-Goc
34c14d4e6d LibJS: Add fast path for float to int put
This commit adds a fast path for putting values into a TypedArray of an
integer type, when the value being put in is a double. This leads to a
6% speedup on JetStream/gcc-loops.js.
2025-05-14 12:33:35 +02:00
Timothy Flynn
7280ed6312 Meta: Enforce newlines around namespaces
This has come up several times during code review, so let's just enforce
it using a new clang-format 20 option.
2025-05-14 02:01:59 -06:00
Aliaksandr Kalenik
155e60a5ff LibJS: Use iteration_step() in IteratorNextUnpack instruction
Allows us to avoid code duplication for builtin iterators fast path.
2025-05-13 15:14:25 +03:00
Daniel Bertalan
456d750539 LibJS: Make generate_labelled_evaluation non-virtual if possible
We don't override anything with definitions of this function in
`SwitchStatement` and `LabelledStatement`. Also, we can make the
`IterationStatement` abstract, there is no need to add a fallback
error-generating stub implementation of this method.
2025-05-12 11:40:45 -06:00
Aliaksandr Kalenik
f405d71657 LibJS: Disable optimization in IteratorNextUnpack if next() is redefined
81b6a11 regressed correctness by always bypassing the `next()` method
resolution for built-in iterators, causing incorrect behavior when
`next()` was redefined on built-in prototypes. This change fixes the
issue by storing a flag on built-in prototypes indicating whether
`next()` has ever been redefined.
2025-05-12 07:41:29 -04:00
Shannon Booth
286a9d8101 LibJS: Don't run promise and cleanup jobs in Interpreter::run
https://tc39.es/ecma262/#sec-jobs specifies that we should only be
running queued promise jobs and host-defined cleanup when the
execution context stack is empty. It is asserted to _not_ be empty
the line above, so remove it.

No impact on test262 or our test suites, Interpreter::run_executable
is already (incorrectly) performing this unconditionally.
run_promise_jobs also happens to do nothing when LibJS is embedded
into LibWeb.
2025-05-09 09:35:49 +02:00
Shannon Booth
19bf897116 LibJS: Avoid roundtrip through Value for comparison bytecode evaluation
1.1x speedup on strictly-equals-object.js
2025-05-08 20:39:29 +02:00
Andreas Kling
d462731a4d LibJS: Make PutById inline cache polymorphic (4 shapes)
2.44x speedup on MicroBench/pic-put-own.js
1.48x speedup on MicroBench/pic-put-pchain.js
2025-05-07 00:27:11 +02:00
Andreas Kling
a677d96b8f LibJS: Make GetById cache polymorphic
Instead of monomorphic (1 shape), GetById inline caches are now
polymorphic (4 shapes).

This improves inline cache hit rates greatly on most web JavaScript.
For example, Speedometer 2.1 sees 88% -> 97% cache hit rate improvement.

1.71x speedup on MicroBench/pic-get-own.js
1.82x speedup on MicroBench/pic-get-pchain.js
2025-05-07 00:27:11 +02:00
Aliaksandr Kalenik
b3713db4ab LibJS: Emit ThrowIfTDZ for identifiers used in assignment expressions
Error about not-initialized `x` variable access should be thrown for:
```js
function foo() {
    x = 69;
    let x;
}

foo();
```

Progress on test262:
```
Diff Tests:
test/staging/sm/expressions/optional-chain-tdz.js  -> 
test/staging/sm/expressions/short-circuit-compound-assignment-tdz.js
 -> 
```

Fixes https://github.com/LadybirdBrowser/ladybird/issues/4587
2025-05-06 12:06:23 +02:00
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
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
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
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
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
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
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
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
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
a05be67e4a LibJS: Let invokers (callers) of [[Call]] allocate ExecutionContext
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
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
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
e48645c83f LibJS: Cache arguments span in ExecutionContext
Allows us to avoid doing math in ExecutionContext::argument()
2025-04-24 10:30:52 +02:00
Aliaksandr Kalenik
ff751173ac LibJS: Delete unused m_arguments member in Interpreter 2025-04-24 10:30:52 +02:00
Aliaksandr Kalenik
a329868c1b LibJS: Allocate ExecutionContext memory using alloca() when possible
This should be faster than heap allocation. However, heap allocation is
still necessary in some cases, such as with generators and async
functions.
2025-04-24 10:30:52 +02:00
Aliaksandr Kalenik
5a92929282 LibJS: Put vector of regs+consts+locals+args in tail of ExecutionContext
By doing that we avoid doing separate allocation for each such vector,
which was really expensive on js heavy websites. For example this change
helps to get EC allocation down from ~17% to ~2% on Google Maps. This
comes at cost of adding extra complexity to custom execution context
allocator, because EC no longer has fixed size and we need to maintain
a list of buckets.
2025-04-24 10:30:52 +02:00