Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit
- From
Alex Riesen <raa.lkml@gmail.com>
- Date
- Oct 7, 2007, 21:54 UTC
- Message-ID
- <20071007215432.GC2765@steel.home>
- In-Reply-To
- <857ilylxhm.fsf@lola.goethe.zz>
David Kastrup, Sun, Oct 07, 2007 18:07:17 +0200:
Show 32 quoted lines
> Miles Bader <miles@gnu.org> writes:
>
> > Pierre Habouzit <madcoder@debian.org> writes:
> >>> strbuf->buf is always non-NULL and NUL-terminated so you could just do
> >>>
> >>> static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
> >>> {
> >>> int len = a->len < b->len ? a->len : b->len;
> >>> return memcmp(a->buf, b->buf, len + 1);
> >>> }
> >>
> >> doesn't work, because a buffer can have (in some very specific cases)
> >> an embeded NUL.
> >
> > Couldn't you then just do:
> >
> > int len = a->len < b->len ? a->len : b->len;
> > int cmp = memcmp(a->buf, b->buf, len);
> > if (cmp == 0)
> > cmp = b->len - a->len;
> > return cmp;
> >
> > [In the case where one string is a prefix of the other, then the longer
> > one is "greater".]
> >
> > ?
>
> I fail to see where this variant is simpler than what we started the
> journey of simplification from.
>
> The only change I consider worth checking from the whole series in
> this thread is making the function inline. It also makes arguments const (which admittedly wont make it faster).
> ... All the rest pretty much > was worse than what we started from in that it needed to reevaluate > more conditions and turned out more complicated and obfuscate even to > the human reader.
it _is_ smaller. And it is _measurably_ faster on that thing I have at home (and old p4).