]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
OPTIM: buffers: avoid a useless wrapping check for ofs == 0
authorWilly Tarreau <w@1wt.eu>
Fri, 18 Oct 2024 16:42:47 +0000 (18:42 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 18 Oct 2024 16:42:47 +0000 (18:42 +0200)
As mentioned in previous commit, b_peek_ofs() performs a wrapping check
but is often called with ofs == 0 as a constant. We can detect this case
with __builtin_const_p() so it makes sense to use it. A test shows a size
reduction of about 320 bytes, which is not much, but it happens in hot code
paths, and each 16 bytes reduction indicates an eliminated conditional
branch.

Some clear winners are ci_getblk_nc() (-48 bytes), h2c_dec_hdrs (-141B),
h1_copy_msg_data (-124B), tcpcheck_spop_expect_hello (-80B),
h1_parse_msg_data (-44B). These ones will definitely benefit from doing
less conditional jumps.

include/haproxy/buf.h

index bb0f0b73bed4592e4dd43f465cddf4aab862936b..2081a2ab7cb286d3ba9ea74b6b08c7820707e6f7 100644 (file)
@@ -183,8 +183,9 @@ static inline size_t b_peek_ofs(const struct buffer *b, size_t ofs)
 {
        size_t ret = __b_peek_ofs(b, ofs);
 
-       if (ret >= b->size)
-               ret -= b->size;
+       if (likely(!__builtin_constant_p(ofs) || ofs))
+               if (ret >= b->size)
+                       ret -= b->size;
 
        return ret;
 }