]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hpack: simplify the len to bytes conversion
authorWilly Tarreau <w@1wt.eu>
Mon, 10 Dec 2018 12:36:56 +0000 (13:36 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 11 Dec 2018 08:06:46 +0000 (09:06 +0100)
The len-to-bytes conversion can be slightly simplified and optimized
by hardcoding a tree lookup. Just doing this increases by 1% the
request rate on H2. It could be made almost branch-free by using
fls() but it looks overkill for most situations since most headers
are very short.

src/hpack-enc.c

index 836584ab41e43524c3eea6723c9311ee885cf9bf..7c6ef6f80077ce89812307657bfd53eb2bc8846a 100644 (file)
  */
 static inline int len_to_bytes(size_t len)
 {
-       if (len < 127)
+       ssize_t slen = len;
+
+       slen -= 127;
+       if (__builtin_expect(slen < 0, 1))
                return 1;
-       if (len < 127 + (1 << 7))
-               return 2;
-       if (len < 127 + (1 << 14))
-               return 3;
-       if (len < 127 + (1 << 21))
+       if (slen < (1 << 14)) {
+               if (__builtin_expect(slen < (1 << 7), 1))
+                       return 2;
+               else
+                       return 3;
+       }
+       if (slen < (1 << 21))
                return 4;
        return 0;
 }