Re: [PATCH] git gc: Speed it up by 18% via faster hash comparisons
- From
Dmitry Potapov <dpotapov@gmail.com>
- Date
- Apr 28, 2011, 08:52 UTC
- Message-ID
- <BANLkTim7bbFiSsj3PRr-_yM5gh1txYQR5w@mail.gmail.com>
- In-Reply-To
- <20110427225114.GA16765@elte.hu>
2011/4/28 Ingo Molnar <mingo@elte.hu>:
Show 7 quoted lines
> +static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
> {
> - return !memcmp(sha1, null_sha1, 20);
> + int i;
> +
> + for (i = 0; i < 20; i++, sha1++, sha2++) {
> + if (*sha1 != *sha2) {At the very least, you may want to put 'likely' in this 'if' condition, otherwise the compiler may optimize this loop in the same way as with memcmp. So, it may work well now, but it may not work much slower with future versions or different level of optimization. (AFAIK, -O3 is far more aggressive in optimizing of loops).
Another thing is that so far this optimization was only with GCC, and we do not know whether it helps or harms to compilers. So, maybe placing this code under 'ifdef __GNUC__' makes more sense than pushing this change on other compilers too.
Dmitry