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

Eyal Alaluf eyala at mainsoft.com
Thu Jan 3 03:21:20 EST 2008

Hi, all.

Let me try to clarify a few things:
  1. The "Hex support" is not for hexadecimal formatting but for
speeding up decimal formatting.
  2. Following the reviews about the array size, the _decHexLen' array
is removed and the '_decHexDigits' is reduced to size of 400 bytes.
  3. The third array that is part of the double ToString algorithm is
already defined within a nested class and will be initialized only when
a double/float ToString is invoked.
Attached is a revised version of 'NumberFormatter.cs' that includes
these changes.


-----Original Message-----
From: Andreas Nahr [mailto:ClassDevelopment at A-SoftTech.com] 
Sent: 03 January 2008 03:17
To: 'Prakash Punnoor'; mono-devel-list at lists.ximian.com
Cc: 'Miguel de Icaza'; 'Andreas Nahr'; 'Atsushi Eno'; 'Juraj Skripsky';
Eyal Alaluf
Subject: AW: [Mono-dev] [SPAM] Re: ToString() performace in Mono

> > 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
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)
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()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: NumberFormatter.cs
Type: application/octet-stream
Size: 56580 bytes
Desc: NumberFormatter.cs
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080103/38b0b15f/attachment.obj 

More information about the Mono-devel-list mailing list