[Mono-dev] [U-SPAM] Re: String.GetHashCode()

Andreas Nahr ClassDevelopment at A-SoftTech.com
Mon Dec 3 16:36:00 EST 2007

> Don't forget that 4 bytes per Hashcode isn't enough. You also need a 
> boolean to store if the hash is already computed (as e.g. 0 is a valid 
> hash, too).
>    You could assume that any string over N would contain the precomputed
hashcode immediately after the string >in a sizeof(IntPtr) aligned 32-bit

I don't think precomputing (a.k. non-lazy init) may be a good thing. I for
one have quite some applications that handle enormous amount of strings.
However none of these Strings ever needs to compute the hashcode. I'd rather
pay 1 (or 4) additional bytes and not precompute it.
However for other projects things will be different...

> 5) GetHashCode should never be called for a string that is not yet fully
built (like in StringBuilder), so  
> there is no worry aout the string changing after the hash code field has
been set

Well string at least contains:
internal unsafe void InternalSetChar (int idx, char val)
internal unsafe void InternalSetLength (int newLength)
Which may be usefull for more advanced optimizations in case they aren't
already used.


More information about the Mono-devel-list mailing list