Re: [PATCH] Speedup prefixcmp() common case
From: Junio C Hamano <hidden>
Date: 2016-06-15 22:44:01
"Marco Costalba" [off-list ref] writes:
On Dec 29, 2007 8:32 PM, Junio C Hamano [off-list ref] wrote:quoted
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));
}