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
,
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
From f2a128b891cead1b24d22326887c54caceda5967 Mon Sep 17 00:00:00 2001
From: kzangeli <[email protected]>
Date: Wed, 18 Nov 2026 20:46:08 +0100
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(+)

diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index 50cb729f..723f62bd 100644
--- 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
-- 
2.53.0

Reply via email to