Repository navigation
http: use memchr() in Curl_verify_header - #23589
Closed
mvanslobbe wants to merge 1 commit into
Closed
mvanslobbe wants to merge 1 commit into
mvanslobbe wants to merge 1 commit into
Conversation
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
bagder
approved these changes
Oct 25, 2026
There was a problem hiding this comment.
🟢 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
Member
|
Thanks, let's see what https://curl.se/perf/#respheaders thinks about this within a few hours... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#23075 changed
Curl_verify_header()to check each header line in a single byte-by-byte pass. With therespheadersperf test from #23580 it turns out that threememchr()calls are faster, since they check 16-32 bytes per step. This goes back tomemchr():./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)
Locally, i7-12700H, Linux, glibc 2.39, pinned to one core:
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.