Commit graph

1456 commits

Author SHA1 Message Date
Andreas Kling
c7bba505ea LibJS: Only use bitfields in Bytecode::Operand on aarch64
It seems both aarch64 and x86_64 are extremely sensitive to the use of
bitfields here. Unfortunately, aarch64 gains a huge speedup from them
while x86_64 sees a very noticeable slowdown.

Since we're talking about 5%+ swings in both directions here, let's go
for the best of both worlds and use ifdefs in the Operand memory layout.
2025-04-06 14:14:34 +02:00
Andreas Kling
d138474e0d LibJS: Avoid unnecessary shifts in Value empty/null/undefined checks
Some checks are pending
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (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
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
We know that the payload is always 0 for these three Value types, and so
we can implement checking for them as full 64-bit compares against
constant values instead of checking just the tag.

This avoids shifting the tag 48 bits to the right before comparing it.
Since these are used all over the place, it actually leads to a nice
code size reduction.
2025-04-06 04:47:01 +02:00
Andreas Kling
c8865458da LibJS: Mark exception-handling paths with [[unlikely]] in interpreter
This appears actually helpful and consistently makes all benchmarks
slightly faster on my machine.
2025-04-06 04:47:01 +02:00
Andreas Kling
aec7dd5778 LibJS: Mark Interpreter::handle_exception() as NEVER_INLINE
Before this change, we were inlining this function after every
handler for instructions that could throw.

Forcing it out-of-line shrinks the main bytecode interpreter by 15%
and yields a decent 2.5% speedup on JetStream/gcc-loops.cpp.js
2025-04-06 04:47:01 +02:00
Andreas Kling
3c2a2bb39f LibJS: Shrink JS::Bytecode::Operand from 8 bytes to 4 bytes
This packs the bytecode much better and gives us a decent performance
boost on throughput-focused benchmarks.

Measured on my M3 MacBook Pro:
- 4.7% speedup on Kraken
- 2.3% speedup on Octane
- 2.7% speedup on JetStream1
2025-04-06 02:05:27 +02:00
Andreas Kling
70411a117b LibJS: Use u32 instead of size_t for bytecode instruction array sizes
Instructions that have an embedded tail array are not going to have
more than 2^32 elements in any remotely sane situation.
2025-04-06 02:05:27 +02:00
Andreas Kling
42cc481091 LibJS: Make Optional<StringTableIndex> use less space
We can use the index's invalid state to signal an empty optional.
This makes Optional<StringTableIndex> 4 bytes instead of 8,
shrinking every bytecode instruction that uses these.
2025-04-06 02:05:27 +02:00
Andreas Kling
f1a54ef281 LibJS: Use u32 for RegexTableIndex
Same as we already do for StringTableIndex and IdentifierTableIndex.
2025-04-06 02:05:27 +02:00
Andreas Kling
15e2c78e9a LibJS: Shrink ThrowCompletionOr<void>
Some checks failed
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
Build Dev Container Image / build (push) Has been cancelled
By specializing this template and using the special empty JS::Value as a
marker for the `void` state, we shrink this very common class from 16
bytes to 8 bytes.

This allows bytecode instruction handlers to return their result in a
single 64-bit register, allowing tighter code generation.
2025-04-05 21:34:13 +02:00
Andreas Kling
c5a239b8c4 LibJS: Add missing visit for CyclicModule::m_evaluation_error 2025-04-05 21:34:13 +02:00
devgianlu
6fc9de7aab LibJS: Remove invalid call to realm() on bytecode generator error
There is no realm when that call happens, use the same logic as the
lines above to create an error from the VM alone.
2025-04-05 20:55:21 +02:00
devgianlu
08cfd5ff1b LibJS: Set empty function parameters on ClassStaticInit scope
This prevents the variables declared inside a class static initializer
to escape to the nearest containing function causing all sorts of memory
corruptions.
2025-04-05 18:20:36 +01:00
devgianlu
6aea459e00 LibJS: Wrap static_init_block_scope call in its own scope 2025-04-05 18:20:36 +01:00
Andreas Kling
fe1962d7fa LibJS: Make SetCompletionType bytecode instruction actually set type
This recovers 38 tests in test262 that regressed in a0bb31f7a0.
2025-04-05 15:00:05 +02:00
Andreas Kling
b05b9378ed LibJS: Demote VERIFYs in Completion() to ASSERT
These were *extremely* hot in profiles (noticed when looking at
disassembly).

Now that we've made the special empty JS::Value much harder to create
accidentally, we can feel better about turning these into ASSERT and
catching them in debug builds.
2025-04-05 11:20:26 +02:00
Andreas Kling
3cf50539ec LibJS: Make Value() default-construct the undefined value
The special empty value (that we use for array holes, Optional<Value>
when empty and a few other other placeholder/sentinel tasks) still
exists, but you now create one via JS::js_special_empty_value() and
check for it with Value::is_special_empty_value().

The main idea here is to make it very unlikely to accidentally create an
unexpected special empty value.
2025-04-05 11:20:26 +02:00
Andreas Kling
0d91363742 LibJS: Remove weird fallback behavior in VM::argument/argument_count
If there's no running execution context, let's just error out in there
instead of returning a weird empty value.
2025-04-05 11:20:26 +02:00
Andreas Kling
ceddc8d660 LibJS: Make a bunch of functions in Environment pure virtual 2025-04-05 11:20:26 +02:00
Andreas Kling
de424d6879 LibJS: Make Completion.[[Value]] non-optional
Instead, just use js_undefined() whenever the [[Value]] field is unused.
This avoids a whole bunch of presence checks.
2025-04-05 11:20:26 +02:00
Andreas Kling
c0600c4353 LibJS: Remove unused ThrowCompletionOr<T>::release... helper 2025-04-05 11:20:26 +02:00
Andreas Kling
6d8bc2b1d6 LibJS: Remove unused Completion::update_empty() 2025-04-05 11:20:26 +02:00
Andreas Kling
41314d0460 LibJS: Remove unnecessary exception checks in bytecode dispatch
No need to check for exceptions after instructions that cannot throw.
2025-04-05 11:20:26 +02:00
Andrew Kaster
8fd81c3338 LibGC+LibWeb+LibJS: Remove workaround for Swift boolean bitfield issue
We're using a main snapshot everywhere, so we can yeet the workaround.
2025-04-04 13:06:53 -06:00
Jess
83e46b3728 LibRegex: Fix crash when parse result exceeds max cache size
Before, If the cache was empty we would try and evict non-existant
entries and crash. So the fix is to make sure that we don't saturate
the cache with a single parse result.
2025-04-04 16:10:25 +02:00
R-Goc
28d5d982ce Everywhere: Remove unused private fields
Some checks are pending
CI / Lagom (x86_64, Fuzzers_CI, false, 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, 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
This commit removes the -Wno-unusued-private-field flag, thus
reenabling the warning. Unused field were either removed or marked
[[maybe_unused]] when unsure.
2025-04-04 12:40:07 +02:00
Andrew Kaster
e4c88915ab LibGC+LibJS+LibWeb: Add workaround for Swift boolean bitfield issue
This patch adds a workaround for a Swift issue where boolean bitfields
with getters and setters in SWIFT_UNSAFE_REFERENCE types are improperly
imported, causing an ICE.
2025-04-03 16:47:48 -06:00
Andreas Kling
3169747989 LibJS: Emit PutById instead of PutByValue when key is string literal
Some checks failed
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
Build Dev Container Image / build (push) Has been cancelled
Basically convert o["foo"]=x into o.foo=x when emitting bytecode.
These are effectively the same thing, and the latter format opts
into using an inline cache for the property lookups.
2025-04-03 18:47:38 +02:00
Andreas Kling
4426c50a18 LibJS: Emit GetById instead of GetByValue when key is string literal
Basically convert o["foo"] into o.foo when emitting bytecode. These are
effectively the same thing, and the latter format opts into using an
inline cache for the property lookups.
2025-04-03 18:47:38 +02:00
Andreas Kling
976ccb9224 LibJS: Add fast path for Int32 values in ToBoolean
It's not uncommon to branch on the boolean value of integers,
so let's do that on the ToBoolean fast path.
2025-04-03 18:47:38 +02:00
Andreas Kling
8de03e8cfd LibJS: Add fast paths in ToNumeric for booleans, null and undefined
These are still in the out-of-line "slow path" for ToNumeric, but
skipping all the coercion machinery can save us a lot of time.
2025-04-03 18:47:38 +02:00
Andreas Kling
cba80580e2 Revert "LibJS: Avoid calling generic Instruction::length() during dispatch"
This reverts commit 8f2ee01e6f.

Speculative revert, as this appears to severely regress performance on
our JS benchmark runner.
2025-04-03 15:09:49 +02:00
Andreas Kling
ec590ef3e6 LibJS: Add builtin for Math.random() 2025-04-03 13:56:39 +02:00
Andreas Kling
714e8aec8a LibJS: Add builtin for Math.imul() 2025-04-03 13:56:39 +02:00
Andreas Kling
8f2ee01e6f LibJS: Avoid calling generic Instruction::length() during dispatch
Since we know the exact type, we can avoid calling the generic lookup
function that branches on instruction type.
2025-04-03 13:56:39 +02:00
Kenneth Myhra
82a2ae99c8 Everywhere: Remove DeprecatedFlyString + any remaining references to it
This reverts commit 7c32d1e8a5.
2025-04-02 11:43:13 +02:00
Andreas Kling
6bb0d585e3 LibJS: Elide function wrapper for class field literal initializers
Some checks are pending
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
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, 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
If a class field initializer is just a simple literal, we can skip
creating (and calling) a wrapper function for it entirely.

1.44x speedup on JetStream3/raytrace-private-class-fields.js
1.53x speedup on JetStream3/raytrace-public-class-fields.js
2025-04-01 23:55:20 +02:00
Andreas Kling
fd0a0e963a LibJS: Make IteratorRecord slightly smaller
By reordering the members, we take this from 40 bytes to 32 bytes.
2025-04-01 20:16:39 +02:00
Andreas Kling
7c32d1e8a5 Revert "Everywhere: Remove DeprecatedFlyString + any remaining references to it"
This reverts commit 3131e6369f.

Greatly regressed JavaScript benchmark performance.
2025-04-01 15:40:27 +02:00
Andreas Kling
b9c57d3122 LibJS: Use xorshift128++ PRNG for Math.random()
This is the same PRNG used by major browser engines, and although it's
a step down in randomness, it massively improves performance of
Math.random().

1.20x speedup on JetStream3/sync-file-system.js :^)
2025-04-01 15:16:54 +02:00
Kenneth Myhra
3131e6369f Everywhere: Remove DeprecatedFlyString + any remaining references to it 2025-04-01 12:50:00 +02:00
Andreas Kling
a0bb31f7a0 LibJS: Make async functions & generators faster with helper types
Instead of returning internal generator results as ordinary JS::Objects
with properties, we now use GeneratorResult and CompletionCell which
both inherit from Cell directly and allow efficient access to state.

1.59x speedup on JetStream3/lazy-collections.js :^)
2025-04-01 02:30:42 +02:00
Andreas Kling
4dc63ddf49 LibJS: Make Optional<Utf16String> use less space
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 (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (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
By specializing the template, we can shrink it from 16 to 8 bytes.
This makes PrimitiveString a measly 32 bytes. :^)
2025-03-30 07:16:40 +01:00
Andreas Kling
152691f9eb LibWeb: Make RopeString subclass so PrimitiveString can be smaller
By moving the LHS and RHS pointers used by rope strings into a
RopeString subclass, we shrink PrimitiveString by 16 bytes. Most strings
are not rope strings, so this ends up saving quite a bit of memory.
2025-03-30 07:16:40 +01:00
Tim Ledbetter
568531f06a Everywhere: Mark GC::Cell derived classes as Weakable explicitly
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 (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (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, all `GC::Cell` derived classes were Weakable. Marking only
those classes that require this functionality as Weakable allows us to
reduce the memory footprint of some frequently used classes.
2025-03-29 17:36:33 -05:00
Andreas Kling
2462a6b0fa LibJS: Use Value::to_byte_string() in fewer places 2025-03-28 12:31:40 -04:00
Andreas Kling
c71772126f LibJS: Remove ByteString internals from PrimitiveString
PrimitiveString is now internally either UTF-8, UTF-16, or both.
We no longer convert them to/from ByteString anywhere, nor does VM have
a ByteString cache.
2025-03-28 12:31:40 -04:00
Lucien Fiorini
f72d87931f LibJS: Reduce Mov instructions by using preferred destinations 2025-03-28 11:21:12 +00:00
Lucien Fiorini
5707076b9e LibJS: Optimize away Mov instructions when the source is the destination 2025-03-28 11:21:12 +00:00
Andreas Kling
f5dd776b11 LibJS: Remove some irrelevant hash lookups in ESFO constructor
If we don't have parameter expressions, we don't need to collect
metadata about whether instantiated var names collide with parameter
names or function names, as these flags are only used in the parameter
code path.
2025-03-27 23:12:04 +00:00
Andreas Kling
c037bda455 LibJS: Use a premade shape for normal function objects
This avoids going through all the shape transitions when setting up the
most common form of ESFO.

This is extremely hot on Uber Eats, and this provides some relief.
2025-03-27 23:12:04 +00:00