473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can 'is' followed by cast by optimized away

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
Nov 17 '05 #1
5 1064
Thorsten Ottosen wrote:
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
Nov 17 '05 #2

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:Oe**************@TK2MSFTNGP12.phx.gbl...
Thorsten Ottosen wrote:
Hi,

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

object o = ...;
if( o is Foo )
return (Foo)o;
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
Nov 17 '05 #3
"Hans Kesting" <ne***********@spamgourmet.com> schrieb im Newsbeitrag
news:Oe**************@TK2MSFTNGP12.phx.gbl...
Thorsten Ottosen wrote:
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


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

Christof
Nov 17 '05 #4
> 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.

-Thorsten

Nov 17 '05 #5

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
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.


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
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Graham Nicholls | last post by:
Hi. I'm having some fun with numbers. I've extraced an image sizes from a jpeg file img_x,img_y=image.getsize() then I'm trying to use those sizes to scale the image, but because python...
22
by: The Doctor | last post by:
Hi all, Recently I was asked the question about whether ++U or U++ is faster if U is a user defined type. What's the answer? Thank you
11
by: Markus Dehmann | last post by:
I have a static const bool debug = false; variable in my class. Debug output goes like this, trivially: if(debug) cerr << "debug message"; Is it guaranteed that this if/then clause is always...
15
by: Oleg Kornilov | last post by:
Hello ! Who can explain why readind from array is fast, but writing (same place) might take a lot of time (I need many loops) How to solve it (declare as static or force some compiler (VC 6.0)...
14
by: vicky | last post by:
SUMMARY: THREE YEARS of and continuing MENTAL TORTURE, TERRORISM, SADISM and BLATANT human rights violations by FBI SADISTS and PERVERTS. Please SAVE this post on your hard disks or email...
13
by: Kelvin Moss | last post by:
Hi group, In C++ it's undefined behavior if one tries to un-const the constness of a const variable with const_cast<>. I want to know if the same holds good in C too. E.g. const char *s =...
18
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
9
by: Tim H | last post by:
class Foo {}; template <typename Tdata> const Foo &operator<<(const Foo &foo, const Tdata &data) { std::cout << "FOO: " << data << std::endl; } Foo
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.