[Mono-list] Re: bug report for new JIT

Chris Day ChrisD@monkey.biz
Thu, 10 Apr 2003 09:47:00 +1000


> From: dietmar [mailto:dietmar@ximian.com]=20
> On Wed, 2003-04-09 at 16:10, Jeroen Frijters wrote:
> > Another thing to consider is garbage collection. Hopefully=20
> the intern=20
> > table has weak references to the strings that the user put=20
> in there,=20
> > but strings put in there by ldstr shouldn't be gargbage collected=20
> > (except when the AppDomain unloads).
> >=20
> > Imagine what happens if:
> >=20
> > > 	mono_ldstr (...);
> > > 	mono_ldstr (...);
> > > 	... repeat for each string in the switch ...
> > *GC happens (because of another thread)*
> > > 	check isinterned
> >=20
> > If the ldstr strings were garbage collected the isinterned=20
> check would=20
> > fail.

There is an article on CodeProject which talks about what the MS CLR
does in the cases of switches on strings.

http://www.codeproject.com/csharp/strings.asp

The relevant part is towards the bottom, it also shows the IL that is
output for these cases.

>>>
C# supports switches on strings. In doing so, it uses an efficient
mechanism for switching.

For small number of cases, the compiler will generate code that looks at
the internal string hash table. The compiler will first call
String.IsIntern on the switch value. String.IsIntern actually returns a
string instead of boolean, returning null for failure, and the interned
value of the string for success.

Every string constant in a case label is automatically known at
compile-time and is therefore guaranteed to be intern. At that point,
the compiler will compare the interned value of the string to each
string constant using reference equality.

If the number of cases is large (in this example it was 14), the
compiler generates a sparse hashtable constructed with a loadfactor of
.5 and with twice the capacity. [[ In actuality, a hashtable of with a
loadfactor of .5 is generated with nearly a 3-1 ratio, since the
Hashtable will try to maintain keep make sure that the ratio of used
buckets to available buckets in the table is at most (loadfactor * .72),
where the default loadfactor is 1. The magic number .72 represents the
optimal ratio to balance speed and memory as determined by Microsoft
performance tests. ]]

The hashtable will map each string to a corresponding index as shown in
the C# illustration of what the compiler generates below.
<<<

HTH

Chris