Repository navigation
Conversation
…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
|
Thanks, nice work! The runtime dispatch approach is solid and both the Makefile and CMake builds look right. One blocker: It would also be good to have a test that actually exercises the dispatch path, since the current test suite doesn't compile |
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
|
Thanks for the review! Both points addressed in 7807968. gyp link (the blocker): I verified this against the release layout with gyp-next: a consumer executable depending on the Dispatch test: added Happy to adjust the gyp shape (e.g. fold it into a single target with per-file settings) if you'd prefer that. |
ShogunPanda
left a comment
There was a problem hiding this comment.
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.
-
Link break for out-of-tree builds (blocker).
src/native/api.c:125-147referencesllhttp__internal_execute_sse42whenever the target is x86-64 and the compiler is GCC/Clang without-msse4.2. Projects that listllhttp.c,api.candhttp.cthemselves now fail. aiohttp does this in itssetup.py(and so does our own Aiohttp CI job), and so do distro packagers and other bindings. Buildingbuild/c/llhttp.c src/native/api.c src/native/http.cfrom this branch as a shared object leavesU 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 compilesllhttp_sse42.c(for example-DLLHTTP_SSE42_DISPATCH=1in the Makefile, CMake and gyp), and keep the current behavior when the macro is missing. -
clang-cl / Windows. The
api.cguard accepts__clang__with_M_X64, which includes clang-cl (Node builds Windows with ClangCL). But gyp skipsllhttp_sse42onOS=="win"(src/llhttp.gyp:27,34), and CMake skips it whenMSVCis set (true for clang-cl). Both produce an unresolved symbol. With clang-cl,__builtin_cpu_supportsalso needs compiler-rt's__cpu_model. The opt-in macro from point 1 fixes this too. Otherwise exclude_MSC_VERin theapi.cguard. -
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_supportsis 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. -
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.2for 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.2only in that case. -
Nits.
bin/generate.tschanges from100755to100644. That looks unintentional, please restore the executable bit.test/dispatch/dispatch_test.cnever asserts that the SSE4.2 path was actually selected. A small exported test hook, or a check on__builtin_cpu_supportsin 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-conwindows-latest.
Problem
llparse has emitted SSE4.2 span scanners (
_mm_cmpestri) for theheader_value,header_value_relaxedandheader_field_generalstates since #24 (2019), but they are guarded by#ifdef __SSE4_2__. Node.js builds with-O3and no x64-march, and so do the Debian/Ubuntu/Fedora/Alpine packages — so every shipped x86-64 binary contains zeropcmpestriand 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.2globally 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.2under renamed internal symbols, and letllhttp_execute()pick it once per process with__builtin_cpu_supports("sse4.2"):bin/generate.tsadditionally emitsbuild/c/llhttp_sse42_rename.h—#defines that rename the definedllhttp__internal*symbols to*_sse42, derived from the generated source so it stays correct across grammar changes — and the 7-linebuild/c/llhttp_sse42.cwrapper 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.cdispatches inllhttp_executeon x86-64 GCC/clang builds compiled without-msse4.2. Both instantiations come from the same generated source, so thellhttp_tlayout is identical and a parser may be driven by either.-msse4.2, on x86-64 only, skipped on MSVC (no__builtin_cpu_supports).-march ≥ x86-64-v2builds are unchanged (the dispatch compiles out; the main path already has the scanners).It is one well-predicted branch per
llhttp_executecall (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):
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.jsviabenchmark/compare.js --runs 30(Welch t-test), anodebuilt from this parser vs. the stock one: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 0pcmpestri, SSE object 3;libllhttp.aexports bothllhttp__internal_executeandllhttp__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_relaxedset, malformed header values can makellhttp__internal__runspin — identically in the scalar and SSE builds. It looks fixed onmain(#882, unreleased in 9.4.3); happy to file a separate issue with the reproducer.Notes
Companion one-liner for Node:
deps/llhttp/llhttp.gypneeds the same second target with-msse4.2once a release includes this — I can open that PR against nodejs/node after a release. If you'd prefer llparse to emit thellhttp__internal__c_*helpers asstatic, 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