From: Junio C Hamano Date: Sun, 30 Dec 2007 00:05:57 GMT Subject: Re: [PATCH] Speedup prefixcmp() common case Message-ID: <7v1w95avx6.fsf@gitster.siamese.dyndns.org> In-Reply-To: "Marco Costalba" writes: > On Dec 29, 2007 8:32 PM, Junio C Hamano 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)); }