Skip to content

http: use memchr() in Curl_verify_header - #23589

Closed
mvanslobbe wants to merge 1 commit into
curl:masterfrom
mvanslobbe:perf/verify-header-memchr
Closed

mvanslobbe wants to merge 1 commit into
curl:masterfrom
mvanslobbe:perf/verify-header-memchr

Conversation

@mvanslobbe

Copy link
Copy Markdown
Contributor

#23075 changed Curl_verify_header() to check each header line in a single byte-by-byte pass. With the respheaders perf test from #23580 it turns out that three memchr() calls are faster, since they check 16-32 bytes per step. This goes back to memchr():

  • look for NUL first, then for CR only in front of it, so whichever error comes first in the line is still the one reported
  • look for the colon only on lines that need it (not the status line, not folded continuation lines), and it usually stops right after the header name

./perf respheaders 50000, master 178e507 against this branch, -O2, median of 9 alternating runs.

On GitHub Actions, building master and this branch from GitHub: https://github.com/mvanslobbe/curl-fork/actions/runs/40088938913
(source: https://github.com/mvanslobbe/curl-fork/tree/4d8058fecf)

runner compiler 5 headers 26 headers 200 headers
x86-64 (Xeon 8370C) gcc 13 10.48 → 10.43 µs (0%) 13.80 → 12.69 µs (−8%) 41.02 → 29.92 µs (−27%)
x86-64 (EPYC 7763) clang 18 15.44 → 15.45 µs (0%) 19.26 → 18.32 µs (−5%) 47.46 → 38.75 µs (−18%)
arm64 (Neoverse-N2) gcc 13 9.94 → 9.91 µs (0%) 13.41 → 12.38 µs (−8%) 41.78 → 33.00 µs (−21%)
arm64 (Neoverse-N2) clang 18 9.61 → 9.60 µs (0%) 12.70 → 11.99 µs (−6%) 38.23 → 32.03 µs (−16%)
macOS arm64 (M2 Pro) Apple clang 21 57.17 → 57.24 µs (0%) 60.64 → 59.13 µs (−2%) 80.09 → 74.14 µs (−7%)

Locally, i7-12700H, Linux, glibc 2.39, pinned to one core:

compiler 5 headers 26 headers 200 headers
gcc 15 6.28 → 6.16 µs (−2%) 8.71 → 7.72 µs (−11%) 30.02 → 22.13 µs (−26%)
clang 22 6.12 → 6.07 µs (−1%) 8.24 → 7.62 µs (−8%) 28.24 → 21.70 µs (−23%)

On macOS the socket pair round trips take most of the time, so the header checks are a smaller part of it.

The full test suite passes with ASan and UBSan on both gcc and clang, as do the torture runs of the header tests. I also compared the old and new checks on 20 million random header lines, with status, normal and folded lines, and they gave the same result for every one.

Follow-up to f228410

AI disclosure

This change was written with the help of Claude Code. I reviewed and tested it myself.

Scanning the header line one byte at a time is slower than three
memchr() calls, which check 16-32 bytes per step. The colon search
usually stops after the header name, and it is now only done for lines
that need it.

Keep reporting whichever error comes first in the line.

Follow-up to f228410
@github-actions github-actions Bot added the HTTP label Oct 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused optimization preserves existing validation behavior, supported by caller review and equivalence checks, with no unresolved findings.

No open issues 🎉

What changed in this PR

Optimizes HTTP response header validation in Curl_verify_header() by replacing byte-by-byte scanning with targeted memchr() calls, while preserving validation behavior and error precedence.

Changes:

  • Searches for NUL and carriage-return bytes separately, preserving which error is reported first.
  • Searches for a colon only when required, skipping status lines and eligible folded continuation lines.
File Description
lib/​http.c Uses targeted memchr() searches to reduce header validation overhead.

Review effort: Balanced

@bagder bagder closed this in 1f0346f Oct 25, 2026
@bagder

bagder commented Oct 25, 2026

Copy link
Copy Markdown
Member

Thanks, let's see what https://curl.se/perf/#respheaders thinks about this within a few hours...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants