[Mono-dev] Fwd: About Sebastien blog "Gendarme gains XML support, more..."

Rafael Teixeira monoman at gmail.com
Mon Sep 18 16:01:11 EDT 2006


Sorry forgot to cc the list.

I may have the details wrong, so anyone better informed please correct
my ramblings on why using String.Empty is more desirable over using
the "" literal, if needed.

:)

---------- Forwarded message ----------
Wrom: WFAOBUZXUWLSZLKBRNVWWCUFPEGAUTFJMVR
Date: Sep 18, 2006 4:56 PM
Subject: Re: [Mono-dev] About Sebastien blog "Gendarme gains XML
support,more..."
To: Jonathan Gilbert <2a5gjx302 at sneakemail.com>


inline

On 9/18/06, Jonathan Gilbert <2a5gjx302 at sneakemail.com> wrote:
> At 01:32 PM 18/09/2006 -0300, Rafael Teixeira wrote:
> >>From the high number of UseStringEmptyRule hits in the example report,
> >I think we can add some magic to gmcs/mcs, so that when run with
> >-optimize it could automatically generate:
> >
> >x = String.Empty;
> >
> >from any
> >
> >x = "";
> >
> >it finds. And after that we should compile all mono managed libs with
> >optimization turned on.
> >
> >Just a bit of my craziness...  :)
>
> I'm a bit confused by this suggestion. My understanding is that at assembly
> load time, the JIT interns all string literals that are present in the
> module, canonicalizing them, and that therefore the two statements above
> are 100% identical, semantically at least. For instance,
>
> class MainClass
> {
>   static void Main()
>   {
>     string from_literal = "";
>     System.Console.WriteLine(
>       ReferenceEquals(from_literal, string.Empty));
>   }
> }


> This sample program outputs "True". This automatic interning of strings is
> crucial for string 'switch' blocks to work. Also, it seems to me that 'x =
> ""' will perform better (on the order of a couple of clock cycles) than 'x
> = string.Empty', since the former is resolved to a constant assignment (a
> constant value, really, propagated to the use of variable 'x') at JIT time
> while the latter, barring some special-case optimization in the JIT, will
> get translated to a load from a static field.

AFAIK, String.Empty is interned just once, each literal ""  has to be
interned in its turn when the method body it is contained is jitted.
Also the "" literal is embedded in the IL as many times as it appear
in the code, so it is costing space in the image. To top it all, the
JIT in reality knows string.Empty is constant, and generates native
code that is as fast or faster than the constant assignment (as it
doesn't have to create the in-stack reference to the interned
literal).


>
> I also happen to think that 'x = ""' is more readable than 'x =
> string.Empty'. :-)

That's is why I suggested for the compiler to do the translation, so
that your source could be kept in the less optimized form. See that
Gendarme is looking at your compiled IL, not your source files, when
pointing out the optimization rule isn't being followed.

>
> Do you know of a compelling reason to use 'x = string.Empty' over 'x = ""'?
>
> Jonathan Gilbert
>


--
Rafael "Monoman" Teixeira
---------------------------------------
"The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man." George Bernard Shaw


-- 
Rafael "Monoman" Teixeira
---------------------------------------
"The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man." George Bernard Shaw



More information about the Mono-devel-list mailing list