Re: [PATCH] Speedup prefixcmp() common case
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Dec 30, 2007, 00:05 UTC
- Message-ID
- <7v1w95avx6.fsf@gitster.siamese.dyndns.org>
- In-Reply-To
- <e5bfff550712291214p7554ea52o875667906ce1a22d@mail.gmail.com>
"Marco Costalba" <mcostalba@gmail.com> writes:
Show 8 quoted lines
> On Dec 29, 2007 8:32 PM, Junio C Hamano <gitster@pobox.com> wrote: >> >> Why isn't it like this? >> >> if (!prefix[1]) >> > > well, what about if prefix == NULL ?
What about it? Do not trim what's relevant when your quote, please.
Your slow path does this:
> return strncmp(str, prefix, strlen(prefix)); > }
So it will barf when prefix == NULL anyway due to strlen(). I think passing NULL as prefix to prefixcmp() is a caller-error.
I think my version is also buggy. Passing "" as prefix to prefixcmp() is nonsense but is supported, and checking prefix[1] without looking at prefix[0] reads past the end of the string.
So, in summary, I think the following is what we would want.
static inline int prefixcmp(const char *str, const char *prefix)
{
+ // shortcut common case of a single char prefix
+ if (prefix[0] && !prefix[1])
+ return *str - *prefix;
+
return strncmp(str, prefix, strlen(prefix));
}