[PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit
- From
Alex Riesen <raa.lkml@gmail.com>
- Date
- Oct 7, 2007, 14:00 UTC
- Message-ID
- <20071007140052.GA3260@steel.home>
- In-Reply-To
- <1190625904-22808-2-git-send-email-madcoder@debian.org>
It is definitely less code (also object code). It is not always measurably faster (but mostly is).
Signed-off-by: Alex Riesen <raa.lkml@gmail.com> --- strbuf.c | 12 ------------ strbuf.h | 9 ++++++++- 2 files changed, 8 insertions(+), 13 deletions(-)
diff --git a/strbuf.c b/strbuf.c index f4201e1..215837b 100644 --- a/strbuf.c +++ b/strbuf.c @@ -58,18 +58,6 @@ void strbuf_rtrim(struct strbuf *sb) sb->buf[sb->len] = '\0'; } -int strbuf_cmp(struct strbuf *a, struct strbuf *b) -{ - int cmp; - if (a->len < b->len) { - cmp = memcmp(a->buf, b->buf, a->len); - return cmp ? cmp : -1; - } else { - cmp = memcmp(a->buf, b->buf, b->len); - return cmp ? cmp : a->len != b->len; - } -} - void strbuf_splice(struct strbuf *sb, size_t pos, size_t len, const void *data, size_t dlen) { diff --git a/strbuf.h b/strbuf.h index 9b9e861..3116387 100644 --- a/strbuf.h +++ b/strbuf.h @@ -78,7 +78,14 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) { /*----- content related -----*/ extern void strbuf_rtrim(struct strbuf *); -extern int strbuf_cmp(struct strbuf *, struct strbuf *); +static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b) +{ + int len = a->len < b->len ? a->len: b->len; + int cmp = memcmp(a->buf, b->buf, len); + if (cmp) + return cmp; + return a->len < b->len ? -1: a->len != b->len; +} /*----- add data in your buffer -----*/ static inline void strbuf_addch(struct strbuf *sb, int c) {
-- 1.5.3.4.223.g78587