<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx</link><description>When you have struct objects as the key in a hashtable, the lookup operation of the hashtable performs miserably. This can be attributes to the GetHashCode() function which is used internally to do the lookup. If a struct contains only simple value types</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: How can I speed up hashtable lookups?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#556400</link><pubDate>Tue, 21 Mar 2006 13:29:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556400</guid><dc:creator>nat</dc:creator><description>Hi Patel,&lt;br&gt;&lt;br&gt;Will it actually slow things down as you have to concatenate two strings while have to get the hashcode from string? It seems to be an expensive operation. Would it be better to multiply the value with some prime value like in other sample?&lt;br&gt;&lt;br&gt;	public override int GetHashCode()&lt;br&gt;	{&lt;br&gt;		return a.GetHashCode() * 29 + b.GetHashCode();&lt;br&gt;	}&lt;br&gt;</description></item><item><title>Interesting Finds</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#556440</link><pubDate>Tue, 21 Mar 2006 15:34:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556440</guid><dc:creator>Jason Haley</dc:creator><description /></item><item><title>My Faq on &amp;amp;quot;hashtable lookups for struct types&amp;amp;quot; is published  at http://blogs.msdn.com/CSharpFaq</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#556644</link><pubDate>Tue, 21 Mar 2006 18:53:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556644</guid><dc:creator>C#, VS Deployment and all geek talk</dc:creator><description>&amp;amp;amp;nbsp;&lt;br&gt;Check out &lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx&amp;amp;amp;nbsp;"&gt;http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx&amp;amp;amp;nbsp;&lt;/a&gt;&lt;br&gt;for a FAQ on...</description></item><item><title>re: How can I speed up hashtable lookups?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#556674</link><pubDate>Tue, 21 Mar 2006 19:04:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556674</guid><dc:creator>vipul</dc:creator><description>Hi Nat,&lt;br&gt;&lt;br&gt;I agree that concatenate operations on strings are expensive.&lt;br&gt;&lt;br&gt;But the lookup on struct if GetHashCode() is not overridden is very time-consuming. I got the idea of an faq about facing a real-life problem.&lt;br&gt;&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemobjectclassgethashcodetopic.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemobjectclassgethashcodetopic.asp&lt;/a&gt; mentions the above point.</description></item><item><title>re: How can I speed up hashtable lookups?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#556834</link><pubDate>Tue, 21 Mar 2006 21:20:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556834</guid><dc:creator>mashiharu</dc:creator><description>After thinking about this for a while I wonder what you're trying to accomplish.&lt;br&gt;&lt;br&gt;My first problem is your statement:&lt;br&gt;&lt;br&gt;&amp;quot;Since you are generating hashcode explicitly which is guaranteed to be unique, it will boost the performance of your lookup.&amp;quot;&lt;br&gt;&lt;br&gt;Hashes are not unique.&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://en.wikipedia.org/wiki/Hash_function"&gt;http://en.wikipedia.org/wiki/Hash_function&lt;/a&gt;</description></item><item><title>re: How can I speed up hashtable lookups?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#556882</link><pubDate>Tue, 21 Mar 2006 22:19:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556882</guid><dc:creator>vipul</dc:creator><description>Hi mashiharu,&lt;br&gt;&lt;br&gt;The purpose of this faq is to enable better lookup for structs. I will get in touch with the csharpfaq team to get the subject title of this post corrected.&lt;br&gt;&lt;br&gt;Basically when you are trying to use struct objects as-is in the hashtable as a the key, the lookup is terrible. The purpose of the above code snippet is an example on how to generate hash code explicitly. Here, I have taken example of creating a string by concatenating all the fields of the struct and joining them using a predetermined key ':'. &lt;br&gt;&lt;br&gt;Using string is just an example, you can use whatever mechanism you want, as long it guarantees &lt;br&gt;a. A hashcode generated for a struct type is unique and same, no matter how many times it is generated.&lt;br&gt;b. If your two struct objects are same, then the hash code generated should be the same.&lt;br&gt;&lt;br&gt;Hope it clarifies. Please feel free to suggest any changes which will result in a better example. We are all here to learn and share.&lt;br&gt;&lt;br&gt;Thanks&lt;br&gt;Vipul</description></item><item><title>re: How can I speed up hashtable lookups?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#556891</link><pubDate>Tue, 21 Mar 2006 22:27:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556891</guid><dc:creator>nat</dc:creator><description>My point is that using string to do hashing may actually slow your code down rather than making it faster especially in scenario of small data points or when the first field in the struct is well dispersed.&lt;br&gt;&lt;br&gt;BTW, your perception on GetHashCode may be a little bit incorrect. GetHashCode on struct (or ValueType in C#) is overridden inside ValueType object. The default implementation is to get hash code from the first field only. Please read this for more detail... &lt;a rel="nofollow" target="_new" href="http://www.informit.com/content/images/0321245660/items/wagner_item10.pdf"&gt;http://www.informit.com/content/images/0321245660/items/wagner_item10.pdf&lt;/a&gt;&lt;br&gt;&lt;br&gt;But in Roter, it seems to use every field to calculate hash code though... &lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/fx/bcl/valuetype_8cs-source.html"&gt;http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/fx/bcl/valuetype_8cs-source.html&lt;/a&gt;</description></item><item><title>re: How can I speed up hashtable lookups?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#556892</link><pubDate>Tue, 21 Mar 2006 22:27:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556892</guid><dc:creator>vipul</dc:creator><description>Hi mashiharu,&lt;br&gt;&lt;br&gt;Asnwering your original question, the string I am generating is bound to be unique as it is being generated from a set of struct objects which are used as a key for the hashtable. And since the string is unique, the GetHashCode on the string will definitely generate a unique hash code.&lt;br&gt;&lt;br&gt;Thanks&lt;br&gt;Vipul</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#556933</link><pubDate>Tue, 21 Mar 2006 22:49:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556933</guid><dc:creator>vipul</dc:creator><description>hi Nat,&lt;br&gt;&lt;br&gt;&amp;lt;quote&amp;gt;&lt;br&gt;especially in scenario of small data points or when the first field in the struct is well dispersed. &lt;br&gt;&amp;lt;quote&amp;gt;&lt;br&gt;&lt;br&gt;This is explicitly the condition which was not available to me for a real-life situation. My struct was composed of 11 fields(short, int, short, int,....) &lt;br&gt;&lt;br&gt;and many(about 15%) of the value types had the same first field, they used to differ on the other fields. With 1,000,000+ records, my performance was dismal. &amp;nbsp;&lt;br&gt;&lt;br&gt;&amp;lt;quote&amp;gt;&lt;br&gt;GetHashCode on struct (or ValueType in C#) is overridden inside ValueType object. The default implementation is to get hash code from the first field only&amp;lt;quote&amp;gt;&lt;br&gt;In the reference, you have a string as the first member, which is quaranteed to be unique. That may not the case with other structs on other examples.</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#556951</link><pubDate>Tue, 21 Mar 2006 22:58:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556951</guid><dc:creator>nat</dc:creator><description>btw, good news or not I don't know... .NET 2.0 seems to change the behavior to be what Rotor does. So you get the same behavior but it will be slower as it uses reflection to do the job.</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#557016</link><pubDate>Tue, 21 Mar 2006 23:27:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:557016</guid><dc:creator>mashiharu</dc:creator><description>&amp;quot;And since the string is unique, the GetHashCode on the string will definitely generate a unique hash code.&amp;quot;&lt;br&gt;&lt;br&gt;Wrong.&lt;br&gt;&lt;br&gt;Try these examples:&lt;br&gt;&lt;br&gt;Console.WriteLine(&amp;quot;0:93121&amp;quot;.GetHashCode());&lt;br&gt;Console.WriteLine(&amp;quot;0:2546870&amp;quot;.GetHashCode()); &lt;br&gt;&lt;br&gt;</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#557029</link><pubDate>Tue, 21 Mar 2006 23:34:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:557029</guid><dc:creator>mashiharu</dc:creator><description>Or if you are on .net 1.1:&lt;br&gt;&lt;br&gt;Console.WriteLine(&amp;quot;0:22980214&amp;quot;.GetHashCode());&lt;br&gt;Console.WriteLine(&amp;quot;0:12538469&amp;quot;.GetHashCode()); &lt;br&gt;</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#557058</link><pubDate>Tue, 21 Mar 2006 23:49:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:557058</guid><dc:creator>vipul</dc:creator><description>mashiharu,&lt;br&gt;&lt;br&gt;yes, there is something fishy about ':'. &amp;nbsp;I beleive it has something to do with the formating specifier.&lt;br&gt;&lt;br&gt;I tried concatenating with '%' and '_' and it generated unique codes. Can you please verify, if they are also prone to a loop-hole. After your response, I will update the FAQ article accordingly. We need readers to have the best solution.</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#557093</link><pubDate>Tue, 21 Mar 2006 23:59:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:557093</guid><dc:creator>mashiharu</dc:creator><description>No, there is no problem with the &amp;quot;:&amp;quot; and it was a clever way to ensure the string are unique. &amp;nbsp;The hash however is not (see wikipedia link above).&lt;br&gt;&lt;br&gt;Example: You have a 2-bit number (0-3) which you want to generate a 1-bit (0-1) unique key for.&lt;br&gt;&lt;br&gt;0 -&amp;gt; 0&lt;br&gt;1 -&amp;gt; 1&lt;br&gt;2 -&amp;gt; ?&lt;br&gt;3 -&amp;gt; ?&lt;br&gt;&lt;br&gt;Can't be done. &amp;nbsp;&lt;br&gt;&lt;br&gt;You get the same problem when you try to take a 32 + 16 = 48 bit number and turn into a 32-bit unique key.</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#557130</link><pubDate>Wed, 22 Mar 2006 00:16:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:557130</guid><dc:creator>vipul</dc:creator><description>Mashiharu, can you please suggest a good workaround which can be used in the FAQ sample?</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#557140</link><pubDate>Wed, 22 Mar 2006 00:19:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:557140</guid><dc:creator>nat</dc:creator><description>why don't you use the way i suggest? It should work...and it performs much better.&lt;br&gt;&lt;br&gt;public override int GetHashCode() &lt;br&gt;{ &lt;br&gt;return a.GetHashCode() * 29*29 + b.GetHashCode()*29 + c.GetHashCode() + ....; &lt;br&gt;} &lt;br&gt;</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#557145</link><pubDate>Wed, 22 Mar 2006 00:23:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:557145</guid><dc:creator>nat</dc:creator><description>If you say so, then you might need to change the wording in your blog that &amp;quot;Since you are generating hashcode explicitly which is guaranteed to be unique&amp;quot;. The key to generate code may be unique in your case but not the hash code itself.</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#558704</link><pubDate>Thu, 23 Mar 2006 10:09:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:558704</guid><dc:creator>Omer van Kloeten</dc:creator><description>The Framework Design Guidelines specify that the maximum size of a struct should be around 16 bytes. That's around four integer fields, which is probably a lot less than the 11 fields you have.&lt;br&gt;Maybe the performance hit should be solved by changing the struct to a class...</description></item><item><title>.Net Performance pointers</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#8202253</link><pubDate>Fri, 14 Mar 2008 15:33:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8202253</guid><dc:creator>Dotmad (on .Net)</dc:creator><description>&lt;p&gt;After going this week to the Microsoft performance open house , here are few things to consider: Create&lt;/p&gt;
</description></item><item><title>Great C# Resource! &amp;laquo; Alexander The Great</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#8398069</link><pubDate>Tue, 15 Apr 2008 22:10:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8398069</guid><dc:creator>Great C# Resource! &amp;laquo; Alexander The Great</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://alexandersarchive.wordpress.com/2008/04/15/great-c-sharp-resource/"&gt;http://alexandersarchive.wordpress.com/2008/04/15/great-c-sharp-resource/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx#9915263</link><pubDate>Fri, 30 Oct 2009 13:57:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9915263</guid><dc:creator>Brian</dc:creator><description>&lt;p&gt;The easiest way to speed up hashcode lookups on any object is to override the GetHashCode function to return an int that is generated at object creation time. &amp;nbsp;This makes it so the HashCode only gets generated once (when the object is created) rather than every time the GetHashCode function is called. &amp;nbsp;There are issues with using this technique with mutable objects...but this is true even if you don't use this technique.&lt;/p&gt;
</description></item></channel></rss>