[Mono-dev] String.GetHashCode()

Robert Jordan robertj at gmx.net
Sat Dec 1 08:35:24 EST 2007


Alan McGovern wrote:
> A thought struck me while i was dozing on the plane on the way home from the
> summit.
> 
> Since strings are immutable, shouldn't it be possible to compute the
> hashcode once and store it rather than recomputing it over and over again?
> Is there some really obvious reason that i can't think of that would make
> this a bad idea?

This idea already came up a couple of years ago (2002 or so).

If memory serves (I was not able find the thread in the archives),
the drawback was the additional "gint32" that every MonoString*
will get, regardless how small it is.

Someone (Paolo?) pointed out that adding this field will increase
the average string size by some unacceptable amount.

Therefore, the optimization should consider the string length
and append the hash code after the string's bytes
only if its length is greater than some amount (1K or so).

Robert




More information about the Mono-devel-list mailing list