public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Hash Function for "switch statement"
@ 2010-03-14 20:48 Jae Hyuk Kwak
  2010-03-15  3:12 ` Basile Starynkevitch
       [not found] ` <20100317200410.GA13807@hungry-tiger.westford.ibm.com>
  0 siblings, 2 replies; 27+ messages in thread
From: Jae Hyuk Kwak @ 2010-03-14 20:48 UTC (permalink / raw)
  To: gcc

Hello, GCC developers.

I'm not sure whether this is a proper mail address to talk about this or not.
But I am giving a shot.

Last week, I was pondering a way to get Enum values from other unique
values like string and integer.
My thought reached at an idea of using Hash table.... as usual..

In addition, I found that switch statement can use "Hash Table" rather
than just replacing with "else if".
Besides using "jump table", "Hash Table" can increase speed.
Hash table idea can alleviate the problem of a huge size of jump table as well.

I explained this at more detail on my blog.
I hope anybody to have a look.

http://wrice.blogspot.com/2010/03/use-perfect-hash-table-for-alternative.html

I wanted to share my idea and wanted to hear from others.
Please let me know where I can talk to, if this mailing-list was not proper.
Thank you for reading.

Sincerely.

Jay

^ permalink raw reply	[flat|nested] 27+ messages in thread
* Re: Hash Function for "switch statement"
@ 2010-03-22 12:44 Unruh, Erwin
  2010-03-22 13:22 ` Robert Dewar
  0 siblings, 1 reply; 27+ messages in thread
From: Unruh, Erwin @ 2010-03-22 12:44 UTC (permalink / raw)
  To: gcc

Hi,

the discussion so far did omit one specific aspect. When comparing two implementations for a switch, you have to compare the full code. For the hash you have to include the code to calculate the hash function. This might be more code than a simple tree lookup.
The example function:

>public int hash32shift(int key)
>{
>  key = ~key + (key << 15); // key = (key << 15) - key - 1;
>  key = key ^ (key >>> 12);
>  key = key + (key << 2);
>  key = key ^ (key >>> 4);
>  key = key * 2057; // key = (key + (key << 3)) + (key << 11);
>  key = key ^ (key >>> 16);
>  return key;
>}

has 12 operations. Add a table and verification you get to about 18. That is worse than a tree search with 9 levels. So for all switches with less than 512 elements, the hash is not faster.

	Erwin

^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2010-03-22 13:32 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-14 20:48 Hash Function for "switch statement" Jae Hyuk Kwak
2010-03-15  3:12 ` Basile Starynkevitch
2010-03-15  8:00   ` Jae Hyuk Kwak
2010-03-16  1:41     ` Jae Hyuk Kwak
2010-03-16  7:12       ` Basile Starynkevitch
2010-03-16  6:00     ` Dave Korn
     [not found] ` <20100317200410.GA13807@hungry-tiger.westford.ibm.com>
2010-03-18  6:39   ` Jae Hyuk Kwak
2010-03-18 11:20     ` Andrew Haley
     [not found]     ` <20100318151753.GA4065@hungry-tiger.westford.ibm.com>
2010-03-19  5:26       ` Jae Hyuk Kwak
2010-03-19 12:26         ` Frank Ch. Eigler
2010-03-19 18:11         ` Jae Hyuk Kwak
     [not found]         ` <20100319165443.GA9993@hungry-tiger.westford.ibm.com>
2010-03-19 21:26           ` Jae Hyuk Kwak
2010-03-19 21:30             ` Robert Dewar
2010-03-19 21:53               ` Jae Hyuk Kwak
2010-03-19 22:18                 ` Robert Dewar
2010-03-19 22:43                   ` Dave Korn
2010-03-19 23:28                     ` Robert Dewar
2010-03-19 22:57                   ` Jae Hyuk Kwak
2010-03-19 23:33                     ` Robert Dewar
2010-03-19 23:33                     ` Robert Dewar
2010-03-19 22:24               ` Dave Korn
2010-03-19 23:09                 ` Robert Dewar
2010-03-19 23:17                   ` Robert Dewar
2010-03-19 19:14     ` Andrew Haley
2010-03-22 12:44 Unruh, Erwin
2010-03-22 13:22 ` Robert Dewar
2010-03-22 16:29   ` Andrew Haley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).