Re: Bob Jenkins' hashing implementation in Ruby
From:
nobu.nokada@...
Date:
2003-03-01 18:28:45 UTC
List:
ruby-core #898
Hi,
At Sun, 2 Mar 2003 03:05:36 +0900,
Mauricio Fern疣dez wrote:
> What about the following?
> I get a speed increase of 3% (compared to the non-inlined version)
> with the second test of the GCLS.
> I eliminate 2 instructions per iteration (on x86) out of 10 in the loop.
> It's OK to use compiler extensions if we detect them in configure, isn't
> it?
Instead, use -funroll-loops option for gcc.
#ifdef __GNUC__
> +#define HAVE_COMPUTED_GOTO 1 /* this should be detected by configure */
#endif
> +#elif HAVE_COMPUTED_GOTO
And you don't need compiler extensions at all in this case:
switch (len) {
case 31: key = key*65599 + *p++;
case 30: key = key*65599 + *p++;
case 29: key = key*65599 + *p++;
/* snip */
case 1: key = key*65599 + *p++;
case 0: break;
default:
key = key*65599 + *p++;
while (len--) {
key = key*65599 + *p;
p++;
}
}
Or famous tricky code:
if (len > 0) {
switch (len & 31) {
do {
case 0: key = key*65599 + *p++;
case 31: key = key*65599 + *p++;
case 30: key = key*65599 + *p++;
case 29: key = key*65599 + *p++;
case 28: key = key*65599 + *p++;
/* snip */
case 1: key = key*65599 + *p++;
} while ((len -= 32) > 0);
}
}
--
Nobu Nakada