Re: g_str_hash



Havoc Pennington <hp@redhat.com> writes: 
> guint
> tcl_hash(gconstpointer v)
> {
>   const char* string = (char*)v;
>   unsigned int result = 0;
>   int c;
> 
>   while (1) {
>     c = *string;
>     string++;
>     if (c == 0) {
>       break;
>     }
>     result += (result<<3) + c;
>   }
>   return result;
> }
> 

Of course this function can just be:

{
 const gchar* s = (char*)v;
 guint result = 0;
 while (*s)
  {
    result += (result << 3) + *s;
    ++s;
  }
 return result;
}

And it's even microscopically but measurably faster that way. Don't
ask me what's up with the structure of the original version's loop.

Havoc



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]