Skip to content

Commit 84340d1

Browse files
committed
OPTIM: buffers: avoid a useless wrapping check for ofs == 0
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.
1 parent fca2122 commit 84340d1

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

include/haproxy/buf.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ static inline size_t b_peek_ofs(const struct buffer *b, size_t ofs)
183183
{
184184
size_t ret = __b_peek_ofs(b, ofs);
185185

186-
if (ret >= b->size)
187-
ret -= b->size;
186+
if (likely(!__builtin_constant_p(ofs) || ofs))
187+
if (ret >= b->size)
188+
ret -= b->size;
188189

189190
return ret;
190191
}

0 commit comments

Comments
 (0)