Dear Ken,

Another very nice patch, thanks a lot. I've merged it as-is to Git master and released GNU libmicrohttpd v1.0.11 with both of your changes just now.

Happy hacking!

Christian

On 11/18/26 8:46 PM, Ken Zangelin wrote:
Hello,

A second patch from the same profiling, independent of the MHD_epoll one I
sent earlier today.

get_req_header() handles a header value one character at a time. For each
one it tests CR, LF, SP/HTAB and NUL, and writes ws_start back to the
connection. A long value costs in proportion: a 700-byte
"Authorization: Bearer <JWT>" took 0.86 us of a ~10 us request on one core,
8.5 % of the throughput. Almost all of that was in get_req_headers().

After a value's first character, every following character above SP takes
exactly that path. The patch skips them in a loop that only compares. CR, LF,
SP, HTAB, NUL and the other control characters stop the loop and go through
the existing code, so line ends, folding, bare CR/LF and the client
discipline levels are untouched. With MHD_FAVOR_SMALL_CODE the code is
unchanged. The same token then costs 0.31 us (3.2 %).

`make check` on 1.0.10 with the patch: 180/180 (with HTTPS), and 164/164
together with the epoll patch (without HTTPS). The patch below is against
master, where it applies cleanly.

It was first posted as https://www.google.com/url?q=https://github.com/ Karlson2k/libmicrohttpd/issues/42&source=gmail&ust=1795117599068000&sa=E <https://www.google.com/url?q=https://github.com/Karlson2k/ libmicrohttpd/issues/42&source=gmail&ust=1795117599068000&sa=E>,
before we noticed the mirror stops at 1.0.5.

Thanks,
Ken Zangelin

---

Subject: [PATCH] get_req_header(): skip a header value's plain characters in one loop

get_req_header() reads a header line one character at a time, and for
every character of a value it re-tests CR, LF, whitespace and NUL and
writes the line's parse state (ws_start) back to the connection. A
long value costs in proportion: a 700-byte "Authorization: Bearer
<JWT>" took 0.86 us of a 10 us request (8.5 % of the throughput on
one core), almost all of it in get_req_headers().

After the first character of a value, every following character
above the space character is processed exactly as that one is
(nothing to check, nothing to record): skip them in a loop that only
compares. CR, LF, SP, HTAB, NUL and the other control characters
stop it and go through the loop as before - the line end, folding,
bare CR / LF and the client discipline levels are untouched. With
MHD_FAVOR_SMALL_CODE the code is unchanged.

The same token then costs 0.31 us (3.2 %). make check: 180 of 180
pass.
---
src/microhttpd/connection.c | 9 +++++++++
1 file changed, 9 insertions(+)

--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -6524,6 +6524,15 @@ get_req_header (struct MHD_Connection *c,
c->rq.hdrs.hdr.value_start = p;
#ifndef MHD_FAVOR_SMALL_CODE
c->rq.hdrs.hdr.ws_start = 0; /* Not on whitespace anymore */
+ /* The characters above the space character that follow are all
+ processed exactly as this one: skip them in one tight loop.
+ Everything else (CR, LF, whitespace, NUL, other control
+ characters) goes through the loop above. */
+ do
+ p++;
+ while ((p < c->read_buffer_offset) &&
+ (((unsigned char) c->read_buffer[p]) > ' '));
+ continue;
#endif /* ! MHD_FAVOR_SMALL_CODE */
}
#ifdef MHD_FAVOR_SMALL_CODE

Attachment: OpenPGP_0xF757745E6F890E90.asc
Description: OpenPGP public key

Reply via email to