Skip to content

Port bellard/quickjs@10c81b1 to fix GCC 14+ performance regression - #1744

Merged
saghul merged 1 commit into
quickjs-ng:masterfrom
gengjiawen:jsvalue-uint64
Sep 28, 2026
Merged

saghul merged 1 commit into
quickjs-ng:masterfrom
gengjiawen:jsvalue-uint64

Conversation

@gengjiawen

@gengjiawen gengjiawen commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Port of bellard/quickjs@10c81b1 ("fixed large performance regression with recent GCC versions").

JS_MKVAL() only writes the 32-bit int32 member of JSValueUnion, so the upper half of the union is left uninitialized. Since GCC 14, x86-64 code generation for such partially initialized 16-byte structs is much worse. Every js_int32() or js_bool() inlined into JS_CallInternal() becomes movabs $0xffffffff00000000 + and + or to merge the new value into the old register contents, and the value then makes a round trip through the stack with a 16-byte reload. This is GCC PR 114134, a regression from r14-2386 that is still open. Writing the whole 64-bit member avoids it.

The change is limited to the non-NaN-boxing branch of quickjs.h. The size and layout of JSValue do not change.

Differences from upstream:

  • JS_VALUE_GET_SHORT_BIG_INT() also reads the 64-bit member. In ng, __JS_NewShortBigInt() builds its value with JS_MKVAL(), so reading the 32-bit short_big_int member returns the wrong half on big-endian hosts. On s390x with only upstream's hunk, 7n / 2n throws "BigInt division by zero" and test_bigint.js and test_std.js fail.
  • The C++ JS_MKVAL() inline function gets the same change.

The int32 member of JSValueUnion is removed, as upstream does. Embedders that read v.u.int32 directly should use JS_VALUE_GET_INT().

Code size

JS_CallInternal(), x86-64, Release build. "Masks" counts movabs $0xffffffff00000000, "16-byte stores" counts movups %xmm, mem.

compiler instructions masks 16-byte stores
GCC 13.4 9471 → 8845 3 → 0 79 → 55
GCC 14.3 10712 → 9073 55 → 0 142 → 52
GCC 15.2 10428 → 8827 52 → 0 142 → 52
GCC 16 snapshot (r16-8246) 9851 → 8886 52 → 0 137 → 48
clang 21.1 14821 → 14340 19 → 0 27 → 27

Performance

V8 benchmark suite v7. Each benchmark runs a fixed number of iterations and the table shows wall time, master → this PR, best of 7 interleaved runs. The machine is a shared 8-vCPU Skylake VM, so differences of a few percent are noise.

benchmark GCC 15.2 GCC 14.3 clang 21
Richards 0.334 → 0.297 s (1.12x) 0.353 → 0.292 s (1.21x) 0.267 → 0.256 s (1.04x)
DeltaBlue 0.430 → 0.401 s (1.07x) 0.498 → 0.432 s (1.15x) 0.440 → 0.379 s (1.16x)
Crypto 0.878 → 0.444 s (1.98x) 0.918 → 0.482 s (1.90x) 0.484 → 0.452 s (1.07x)
RayTrace 0.660 → 0.670 s (0.99x) 0.695 → 0.642 s (1.08x) 0.553 → 0.545 s (1.01x)
EarleyBoyer 0.564 → 0.535 s (1.05x) 0.570 → 0.557 s (1.02x) 0.523 → 0.526 s (0.99x)
RegExp 0.937 → 0.903 s (1.04x) 0.972 → 0.918 s (1.06x) 0.949 → 0.870 s (1.09x)
Splay 1.190 → 1.103 s (1.08x) 1.212 → 1.149 s (1.05x) 1.154 → 1.118 s (1.03x)
NavierStokes 0.562 → 0.392 s (1.43x) 0.586 → 0.456 s (1.29x) 0.455 → 0.469 s (0.97x)
geometric mean 1.19x 1.20x 1.05x

Testing

  • GCC 15.2: make test, api-test, standalone test, ctest, full test262 with the 49 known errors and nothing new
  • clang 21: make test, api-test
  • cxxtest with g++ and clang++
  • ASAN + UBSAN: make test, api-test
  • 32-bit x86 with -DJS_NAN_BOXING=0 so the struct representation is used: make test, api-test
  • s390x big-endian under qemu, bytecode regenerated with make codegen as docs/docs/building.md describes: make test, api-test

Port of bellard/quickjs@10c81b1
("fixed large performance regression with recent GCC versions").

JS_MKVAL() only wrote the 32-bit int32 member of JSValueUnion and left
the upper half of the union uninitialized. Since GCC 14, x86-64 code
generation for such partially initialized 16-byte structs is much
worse (GCC PR 114134, a regression from r14-2386): every js_int32() or
js_bool() inlined into JS_CallInternal() becomes a movabs/and/or
read-modify-write and the value makes a round trip through the stack.
clang 21 emits some of the same masking sequences. Writing the whole
64-bit member avoids that.

Unlike upstream, JS_VALUE_GET_SHORT_BIG_INT() also reads the 64-bit
member because __JS_NewShortBigInt() builds its value with JS_MKVAL().
Reading the 32-bit member would return the wrong half on big-endian
hosts.

Co-authored-by: Fabrice Bellard <[email protected]>
@gengjiawen gengjiawen changed the title Initialize all 64 bits of JSValue in JS_MKVAL Port bellard/quickjs@10c81b1 to fix GCC 14+ performance regression Sep 23, 2026
@saghul
saghul merged commit 2f0aa72 into quickjs-ng:master Sep 28, 2026
128 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants