From ab322d4fd45c51fa38b3128d3ef76f1ff4614009 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 20 Jul 2018 16:07:42 +0200 Subject: [PATCH] MINOR: buffers: simplify b_contig_space() This function is used a lot in block copies and is needlessly complicated since it still uses pointer arithmetic. Let's fall back to regular offsets and simplify it. This removed around 23 bytes from b_putblk() and it removed any conditional jump. --- include/common/buf.h | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/include/common/buf.h b/include/common/buf.h index 92db0a174..4ccc8b0ed 100644 --- a/include/common/buf.h +++ b/include/common/buf.h @@ -300,21 +300,28 @@ static inline size_t b_contig_data(const struct buffer *b, size_t start) } /* b_contig_space() : returns the amount of bytes that can be appended to the - * buffer at once. + * buffer at once. We have 8 possible cases : + * + * [____________________] return size + * [______|_____________] return size - tail_ofs + * [XXXXXX|_____________] return size - tail_ofs + * [___|XXXXXX|_________] return size - tail_ofs + * [______________XXXXXX] return head_ofs + * [XXXX|___________|XXX] return head_ofs - tail_ofs + * [XXXXXXXXXX|XXXXXXXXX] return 0 + * [XXXXXXXXXXXXXXXXXXXX] return 0 */ static inline size_t b_contig_space(const struct buffer *b) { - const char *left, *right; + size_t left, right; - right = b_head(b); + right = b_head_ofs(b); left = right + b_data(b); - if (left >= b_wrap(b)) - left -= b_size(b); - else - right = b_wrap(b); - - return right - left; + left = b_size(b) - left; + if ((ssize_t)left <= 0) + left += right; + return left; } /* b_getblk() : gets one full block of data at once from a buffer, starting