[Mono-dev] [SPAM] Re: ToString() performace in Mono revisited

Andreas Nahr ClassDevelopment at A-SoftTech.com
Wed Jan 2 20:16:36 EST 2008

> > The array initialization should also be done lazily and not in the 
> > static constructor (should be removed completely because it drags in 
> > other static fields that need to be preinitialized, code compiled and so
> > Especially the Hex support is IMHO completely off bounds. I 
> > (personally) rarely see hex output and making EVERYBODY pay for a 
> > hex speedup doesn't seem right. A simple if (array == null) Init (); 
> > will be enough. You will pay per-call, but it is relatively cheap.
> The only thing that is worth keeping in mind is that if this is a 
> static field, initialization probably needs to be protected by a lock 
> (I say probably, because we *could* ignore the race by carefully 
> making sure that we assign the public array only after it has been 
> initialized, so we would end up with N copies of an array initialized 
> in the worst case, but N-1 will be discarded by the GC).

You can also use the Bill Pugh's trick by using a nested class, so the jit
should lazily initialize it (it works with .net, no idea whether it does

I don't know whether it would work with mono either but you may get
additional problems having a nested class in a structure ;)

Also there is no need to lock for multithreading. As Miguel wrote we can
simply ignore the race in this situation. Worst case two (or n) Lookuparrays
get created and all except one immediately garbage-collected after their

All you need to have is
void Init () {
int[] temp = new int[x];
staticField = temp; // Must not happen before doinits()


More information about the Mono-devel-list mailing list