Skip to content

Run the SSE4.2 span scanners on default x86-64 builds via runtime dispatch - #896

Open
jenswl wants to merge 2 commits into
nodejs:mainfrom
jenswl:perf/sse42-dispatch
Open

jenswl wants to merge 2 commits into
nodejs:mainfrom
jenswl:perf/sse42-dispatch

Conversation

@jenswl

@jenswl jenswl commented Nov 8, 2026

Copy link
Copy Markdown

Problem

llparse has emitted SSE4.2 span scanners (_mm_cmpestri) for the header_value, header_value_relaxed and header_field_general states since #24 (2019), but they are guarded by #ifdef __SSE4_2__. Node.js builds with -O3 and no x64 -march, and so do the Debian/Ubuntu/Fedora/Alpine packages — so every shipped x86-64 binary contains zero pcmpestri and the scanners are dead code. They are live only where the build baseline is already ≥ x86-64-v2 (e.g. RHEL rebuilds) and on arm64 (NEON).

Passing -msse4.2 globally was rejected for good reasons (#58), and the 2019 runtime-dispatch attempt (#26) checked CPU features inside the state machine on every scan and measured slower.

Change

Compile the generated parser a second time with -msse4.2 under renamed internal symbols, and let llhttp_execute() pick it once per process with __builtin_cpu_supports("sse4.2"):

  • bin/generate.ts additionally emits build/c/llhttp_sse42_rename.h — #defines that rename the defined llhttp__internal* symbols to *_sse42, derived from the generated source so it stays correct across grammar changes — and the 7-line build/c/llhttp_sse42.c wrapper that includes the parser under those renames. The imported callbacks (llhttp__on_*, llhttp__before_*, llhttp__after_*) keep their names and stay shared.
  • src/native/api.c dispatches in llhttp_execute on x86-64 GCC/clang builds compiled without -msse4.2. Both instantiations come from the same generated source, so the llhttp_t layout is identical and a parser may be driven by either.
  • Makefile and CMakeLists.txt build the second object with -msse4.2, on x86-64 only, skipped on MSVC (no __builtin_cpu_supports). -march ≥ x86-64-v2 builds are unchanged (the dispatch compiles out; the main path already has the scanners).

It is one well-predicted branch per llhttp_execute call (per TCP chunk), not per byte or per span — the objection raised against #26 and in #58.

Measurements (AMD Zen 5)

Standalone harness against this parser (8 interleaved runs, 95% CI of the time delta):

request ns/req base → dispatch delta
4-header GET 69.7 → 66.4 −4.8% [−6.5, −3.0]
32-header GET 561.5 → 496.6 −11.6% [−12.6, −10.5]
browser GET (long Cookie) 405.9 → 332.8 −18.0% [−19.0, −17.1]
10 KiB POST 136.4 → 125.2 −8.2% [−10.5, −5.9]

perf stat (browser): instructions −43%, branches −47%, branch-misses −62%. Requests with short header values (the 4-header case) benefit least, as expected (the 16-byte block rarely fires).

Node benchmark/http/bench-parser.js via benchmark/compare.js --runs 30 (Welch t-test), a node built from this parser vs. the stock one:

improvement p
32 headers +4.81% 2.8e-8
16 headers +3.85% 2.8e-6
8 headers +2.34% 1.6e-3
4 headers +2.19% 4.1e-2

Binary size: the second parser copy adds ≈ 30 KB of .text; only one copy is hot in any process.

Correctness

A libFuzzer differential harness (ASan + UBSan) drove the baseline and the SSE4.2 instantiation with identical random chunking and lenient-flag combinations, comparing the error code and the full callback sequence (kind, offset, length, state). ≈ 5.5e8 executions, 0 divergences, no sanitizer report. The 16-byte SSE loads stay in bounds — the generated code already guards endp - p >= 16. Built and verified standalone (generator + make): base object 0 pcmpestri, SSE object 3; libllhttp.a exports both llhttp__internal_execute and llhttp__internal_execute_sse42; a program linked against it parses correctly with the SSE path selected.

Fuzzing also surfaced a pre-existing infinite loop (unrelated to this change): with lenient_header_value_relaxed set, malformed header values can make llhttp__internal__run spin — identically in the scalar and SSE builds. It looks fixed on main (#882, unreleased in 9.4.3); happy to file a separate issue with the reproducer.

Notes

Companion one-liner for Node: deps/llhttp/llhttp.gyp needs the same second target with -msse4.2 once a release includes this — I can open that PR against nodejs/node after a release. If you'd prefer llparse to emit the llhttp__internal__c_* helpers as static, the rename header could shrink to just the two entry points; glad to take whichever direction you prefer.

🤖 Generated with Claude Code

https://claude.ai/code/session_0139J1kq4EkxBPrZELPKPfF3

…patch

llparse has emitted SSE4.2 span scanners (`_mm_cmpestri`) for the
header-value and header-field states since 2019, but they are guarded by
`#ifdef __SSE4_2__`. Node.js and the common Linux distributions build with
no `-march`, so every shipped x86-64 binary contains zero `pcmpestri` and
the fast path is dead; it is live only where the build baseline is
>= x86-64-v2 (e.g. RHEL). Passing `-msse4.2` globally was rejected before,
and an earlier per-call dispatch attempt measured slower.

This compiles the generated parser a second time with `-msse4.2` under
renamed internal symbols and lets `llhttp_execute()` select it once per
process with `__builtin_cpu_supports("sse4.2")`:

- bin/generate.ts additionally emits `llhttp_sse42_rename.h` (renames the
  defined `llhttp__internal*` symbols, derived from the generated source so
  it stays correct across grammar changes) and the small `llhttp_sse42.c`
  wrapper that includes the generated parser under those renames. The
  imported callbacks (`llhttp__on_*`) keep their names and stay shared.
- src/native/api.c dispatches in `llhttp_execute` on x86-64 GCC/clang builds
  that were compiled without `-msse4.2`. Both instantiations come from the
  same source, so the parser state layout is identical.
- Makefile and CMakeLists build the second object with `-msse4.2`, on
  x86-64 only, skipped on MSVC.

One well-predicted branch per `llhttp_execute` call (per TCP chunk), not
per byte or per span.

Measurements (Zen 5, this parser; standalone harness, 8 interleaved runs, 95% CI):
  4-header GET    -4.8% [-6.5, -3.0]
  32-header GET  -11.6% [-12.6, -10.5]
  browser GET    -18.0% [-19.0, -17.1]   (instructions -43%)
  10 KiB POST     -8.2% [-10.5, -5.9]
Node benchmark/http/bench-parser.js (compare.js, 30 runs, Welch): +4.8% (32
headers, p=3e-8), +3.9% (16), +2.3% (8), +2.2% (4); all significant.

Correctness: a libFuzzer differential harness (ASan+UBSan) drove the base
and SSE4.2 instantiations with identical random chunking and lenient-flag
combinations, comparing error code and the full callback sequence; ~5.5e8
executions, 0 divergences. The 16-byte SSE loads stay in bounds (the
generated code guards `endp - p >= 16`).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139J1kq4EkxBPrZELPKPfF3
@ShogunPanda

Copy link
Copy Markdown
Contributor

Thanks, nice work! The runtime dispatch approach is solid and both the Makefile and CMake builds look right.

One blocker: src/llhttp.gyp isn't updated. llhttp_sources still lists only llhttp.c, api.c and http.c, and that's what ships in the release tarball and in Node's deps/llhttp. With this api.c and no SSE4.2 object, the Node build would fail to link (undefined reference to llhttp__internal_execute_sse42). Please add llhttp_sse42.c (with the proper -msse4.2 flags on x86-64) to the gyp file.

It would also be good to have a test that actually exercises the dispatch path, since the current test suite doesn't compile api.c.

Review follow-up for the runtime SSE4.2 dispatch. src/llhttp.gyp -- which
ships in the release tarball and is what Node's deps/llhttp builds -- still
listed only llhttp.c, api.c and http.c. With the dispatch added to api.c but
no SSE4.2 object, that build fails to link with
`undefined reference to llhttp__internal_execute_sse42`.

- src/llhttp.gyp: on x86-64 (non-MSVC), build src/llhttp_sse42.c as a separate
  static_library target with -msse4.2 and make the main llhttp target depend on
  it. The flag is scoped to that one translation unit; the target and the
  dependency are both guarded by the same arch condition, so other targets and
  non-x86-64 builds are unchanged. (The release Makefile already copies
  llhttp_sse42.c and llhttp_sse42_rename.h into release/src.)
- test/dispatch/dispatch_test.c: links against the built library (which
  contains api.c and, on x86-64, both parser instantiations) and drives the
  runtime dispatch that the markdown suite never compiles. It parses a request
  with header values long enough to take the SSE4.2 span scanners, once in one
  call and once one byte at a time, and asserts the two agree and are correct.
- Makefile: `make test-c` builds and runs it against build/libllhttp.a.
- ci.yaml: run `make test-c` after building the library.

Verified by building the release-layout gyp with a consumer executable: it
links and runs, -msse4.2 is applied only to llhttp_sse42.c, and reverting the
gyp change reproduces the undefined-reference link error.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139J1kq4EkxBPrZELPKPfF3
@jenswl

jenswl commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks for the review! Both points addressed in 7807968.

gyp link (the blocker): src/llhttp.gyp now builds src/llhttp_sse42.c on x86-64 (non-MSVC) as a separate static_library target with -msse4.2, and the main llhttp target depends on it. A separate target keeps -msse4.2 scoped to that one translation unit; the target and the dependency share the same arch condition, so non-x86-64 and MSVC builds are unchanged. The release Makefile already copies llhttp_sse42.c and llhttp_sse42_rename.h into release/src/, so the tarball and Node's deps/llhttp now have the object they link against.

I verified this against the release layout with gyp-next: a consumer executable depending on the llhttp target links and runs, -msse4.2 is applied only to llhttp_sse42.c (not to the base llhttp.c), and reverting just the gyp change reproduces exactly the undefined reference to llhttp__internal_execute_sse42 you described.

Dispatch test: added test/dispatch/dispatch_test.c, run via make test-c and wired into CI right after the libllhttp.a build. It links against the built library (so api.c plus both parser instantiations are in play) and drives llhttp_execute on a request whose header values are long enough to take the SSE4.2 span scanners, once in a single call and once one byte at a time, asserting the two runs agree and parse correctly. The markdown suite compiles the generated parser with -msse4.2 directly but never compiles api.c, so this covers the gap you pointed out.

Happy to adjust the gyp shape (e.g. fold it into a single target with per-file settings) if you'd prefer that.

@ShogunPanda ShogunPanda left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR: thanks for addressing the gyp blocker, the approach is good. One remaining blocker: the dispatch is enabled by default in api.c, so every consumer that compiles the sources with its own build system (not our Makefile/CMake/gyp) now fails to link on x86-64. Please make it opt-in at the build level. A few smaller points below.

  1. Link break for out-of-tree builds (blocker). src/native/api.c:125-147 references llhttp__internal_execute_sse42 whenever the target is x86-64 and the compiler is GCC/Clang without -msse4.2. Projects that list llhttp.c, api.c and http.c themselves now fail. aiohttp does this in its setup.py (and so does our own Aiohttp CI job), and so do distro packagers and other bindings. Building build/c/llhttp.c src/native/api.c src/native/http.c from this branch as a shared object leaves U llhttp__internal_execute_sse42, so the module fails at load time instead of at build time. Suggestion: guard the dispatch behind a macro that the build system defines only when it also compiles llhttp_sse42.c (for example -DLLHTTP_SSE42_DISPATCH=1 in the Makefile, CMake and gyp), and keep the current behavior when the macro is missing.

  2. clang-cl / Windows. The api.c guard accepts __clang__ with _M_X64, which includes clang-cl (Node builds Windows with ClangCL). But gyp skips llhttp_sse42 on OS=="win" (src/llhttp.gyp:27,34), and CMake skips it when MSVC is set (true for clang-cl). Both produce an unresolved symbol. With clang-cl, __builtin_cpu_supports also needs compiler-rt's __cpu_model. The opt-in macro from point 1 fixes this too. Otherwise exclude _MSC_VER in the api.c guard.

  3. Cached flag. static int llhttp__sse42 (api.c:136) is written from any thread on the first call. The race is benign but is still a data race in C11. __builtin_cpu_supports is only a load and test of __cpu_model, which libgcc/compiler-rt initialize in a constructor, so you can call it directly every time and drop the cache.

  4. Makefile arch detection. ARCH ?= $(shell uname -m) (Makefile:18) checks the host, not the target. When cross-compiling (CLANG=... --target=aarch64-... on an x86-64 host), it adds an object built with -msse4.2 for a non-x86 target. Consider asking the compiler instead ($(CLANG) -dumpmachine), or let the wrapper's #if defined(__x86_64__) handle it and pass -msse4.2 only in that case.

  5. Nits.

    • bin/generate.ts changes from 100755 to 100644. That looks unintentional, please restore the executable bit.
    • test/dispatch/dispatch_test.c never asserts that the SSE4.2 path was actually selected. A small exported test hook, or a check on __builtin_cpu_supports in the test, would keep it from passing silently on the scalar path.
    • CI still has to run on this head (fork workflows awaiting approval), including make test-c on windows-latest.

This branch has not been deployed

No deployments
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