Skip to content

perf(parser): SIMD crlf-pair match and scalar hot-path fixes - #374

Merged
gg582 merged 1 commit into
devfrom
perf/simd-parser
Oct 13, 2026
Merged

gg582 merged 1 commit into
devfrom
perf/simd-parser

Conversation

@gg582

@gg582 gg582 commented Oct 13, 2026

Copy link
Copy Markdown
Member

Adds a paired SIMD matcher for "\r\n\r\n" inside the existing compile-time arch ladder in simd_parser.h, eliminating the O(n·k) rescan on bare-'\r' data, and rewrites the query.c hot paths: url_decode uses a hex table instead of per-escape sscanf, and cwist_query_map_parse is a single memchr-driven pass without strtok_r.

End-to-end wrk on cwist_bench is flat (this workload barely touches the query path), but the url_decode micro-bench (2000 escapes, 1M decodes) shows 12.8x:

benchmark before after
wrk / req/s (avg of 3) 279,117 273,652
wrk avg latency 0.757 ms 0.780 ms
url_decode 2000-escape string 129.5 µs 10.1 µs

All existing tests pass, plus new tests/test_simd_parser.c (paired matcher, bare-'\r'-heavy and vector-boundary cases) and malformed-escape query-map cases in test_core_utils.

Replace the find-CR-then-check loop in cwist_simd_find_crlfcrlf with a
paired matcher inside the existing compile-time arch ladder (AVX2/SSE2/
NEON/RVV/AltiVec/MSA): four unaligned loads at j..j+3 are compared
against \r\n\r\n and ANDed, so every offset is tested in one pass and
bare-'\r'-heavy data no longer causes an O(n*k) rescan. The memchr/SWAR
scalar tail is kept for the vector-safety margin and non-vector builds.

query.c hot paths: url_decode drops the per-escape sscanf("%2x") call
in favor of an isxdigit guard plus a 256-entry nibble table, copying
plain runs in bulk via strcspn; malformed escapes still pass through
literally, byte for byte. cwist_query_map_parse replaces strtok_r +
strchr with a single pass over the query_strdup copy using memchr,
preserving strtok's empty-token collapsing and the arena/lifetime
contract.

Tests: new tests/test_simd_parser.c covers the paired matcher including
bare-'\r'-heavy input and vector-boundary-straddling matches;
test_core_utils gains malformed-escape and delimiter-edge cases for the
query map.
@gg582
gg582 merged commit 9661225 into dev Oct 13, 2026
25 checks passed
gg582 added a commit that referenced this pull request Oct 13, 2026
perf(parser): SIMD crlf-pair match and scalar hot-path fixes
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.

1 participant