Can 'is' followed by cast by optimized away

  • Thread starter Thread starter Thorsten Ottosen
  • Start date Start date
T

Thorsten Ottosen

Hi,

Can the following ugly type-switching code by optimized
by the compiler/jitter?:

object o = ...;
if( o is Foo )
return (Foo)o;

Arguably, the cast is guranteed to succeed, but do the
compiler take advantage of that?

If yes, would it be possible to each that conclusion
by looking at the assembler output?

Thanks

-Thorsten
 
Thorsten said:
Hi,

Can the following ugly type-switching code by optimized
by the compiler/jitter?:

object o = ...;
if( o is Foo )
return (Foo)o;

Arguably, the cast is guranteed to succeed, but do the
compiler take advantage of that?

If yes, would it be possible to each that conclusion
by looking at the assembler output?

Thanks

-Thorsten

see the "as" operator

return o as Foo;

if o is not a Foo, a null is returned
(works only on reference types, not on value types, as they can't be null)

Hans Kesting
 
see the "as" operator

return o as Foo;

if o is not a Foo, a null is returned
(works only on reference types, not on value types, as they can't be null)

Thanks, but it does not quite answer my question. Since as is equivalent to

expression is type ? (type)expression : (type)null

my question remains unanswered: does this lead to essentially two casts (at
runtime) if the expression is really
of the type 'type'? (one cast to see if it is the right type, then a cast to
convert).

-Thorsten
 
Hans Kesting said:
see the "as" operator

return o as Foo;

if o is not a Foo, a null is returned
(works only on reference types, not on value types, as they can't be null)

Hans Kesting

No, it's not really the same.
Thorsten didn't say, what he want's to do in the else part.

Christof
 
my question remains unanswered: does this lead to essentially two casts (at
runtime) if the expression is really
of the type 'type'? (one cast to see if it is the right type, then a cast to
convert).

you are correct that your original example requires essentially two casts.
and I don't believe there is optimization of any kind to eliminate the extra
cast.
Thanks, but it does not quite answer my question. Since as is equivalent to

expression is type ? (type)expression : (type)null

no it's not. 'is' operator and 'as' operator performs the exact same IL
instruction, with the only difference being, 'as' will generate an extra
instruction to save the result of the cast. which is precisely the
optimization, so to speak, that you seek.
 
no it's not.

ok, but I copied this from the .Net reference documentation :-)
'is' operator and 'as' operator performs the exact same IL
instruction, with the only difference being, 'as' will generate an extra
instruction to save the result of the cast. which is precisely the
optimization, so to speak, that you seek.

ok, thanks.

-Thorsten
 
Thorsten Ottosen said:
ok, but I copied this from the .Net reference documentation :-)

the doc is a bit misleading I guess. I think in addition to saying the
expression is evaluated only once, they should also stress the fact that
casting only occurs once as well.
ok, thanks.

let me correct myself a bit. there is no extra assignment. that
instruction is from the assignment I was doing in code. (duh!) so 'is' and
'as' both translates to the exact same OpCode. Only at the C# end, the
return type is treated a little differently.
 
Back
Top