[prev in list] [next in list] [prev in thread] [next in thread] 

List:       git
Subject:    Re: [PATCH] Speedup prefixcmp() common case
From:       Junio C Hamano <gitster () pobox ! com>
Date:       2007-12-30 0:05:57
Message-ID: 7v1w95avx6.fsf () gitster ! siamese ! dyndns ! org
[Download RAW message or body]

"Marco Costalba" <mcostalba@gmail.com> writes:

> 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));
 }
-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic