[Mono-devel-list] Re: System.Reflection Performance

Ben Maurer bmaurer at users.sourceforge.net
Sun Jan 4 11:33:17 EST 2004


Hello!

On Sun, 2004-01-04 at 02:00, Gaute B Strokkenes wrote:
> On 2 jan 2004, bmaurer at users.sourceforge.net wrote:
> 
> > For example, in the string case, under the hood, the .Length == 0
> > translates to:
> >
> > string->Length == 0
> >
> > however, string == "" translates to:
> >
> > String.Equals (string, "");
> >
> > Thats pretty much a no brainer.
> 
> How come the compiler does not do this, then?  I mean, String is a
> sealed class.  If this case is actually sufficiently common to be
> worth looking for when a method is slow, then it sounds like MCS
> should be doing it.

This is a pretty interesting question. Now, technically the compiler
would have to emit:

if (string != null && string.Length ==0)

If string were an expression, this would be somewhat hard to emit, we
would have to do some weird dup/pop thingy. However, if string is a
local variable reference or a param reference, it would be nice and
easy. We would just do:

ldarg.1
brfalse AFTER_IF
ldarg.1
call <get length> // Note we need not do callvirt, because we know it is not null
brtrue AFTER_IF

> Is there some underlying reason why this a priori very simple
> optimisation has not been implemented, or is there some deeper reason?

I am not sure. It is interesting to note that in their 1.2 beta, CSC
does not do this. When I saw this, i ran FX cop on their new BCL, it did
not give any warnings about comparing to "" (which it does on 1.1). So,
it seems they took the route of changing their source (i hope they know
how to use sed :).

Miguel, any ideas why they dont? Maybe we should ask the MS team?

-- Ben




More information about the Mono-devel-list mailing list