473,513 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Performance of operators is/as vs cast

There was an earlier discussion regarding which one is faster - a
throwing cast - "(Foo)obj", or a non-throwing one - "obj as Foo", when
both are available for a given type and value. The consensus was that,
since a check has to be made by the compiler in either case, the speed
should be equivalent in case object is indeed of a requested type.
Sounds perfectly reasonable, but I've spotted the following in
the .NET source code (from MS symbol servers) today, in file
BindingList.cs, method Child_PropertyChanged:

// The change event is broken should someone pass
an item to us that is not
// of type T. Still, if they do so, detect it and
ignore. It is an incorrect
// and rare enough occurrence that we do not want
to slow the mainline path
// with "is" checks.
T item;
try {
item = (T)sender;
}
catch(InvalidCastException) {
ResetBindings();
return;
}

Now this gets interesting, since the comment clearly implies that a
throwing cast is, after all, faster than a check for the non-throw
case (and since "is" and "as" are both compiled to "isinst" IL, we
know that it is really about "castclass" vs "isinst"). I wonder how
true this really is, and if so, why is it the case. I'm going to play
with debugger to see what the JIT produces in either case later, but
perhaps someone already knows the reason?
Jun 27 '08 #1
4 2526
implies that a throwing cast is, after all, faster than a check for the non-throw
No - the point here is that even if they do an "is" test, they are
*still* going to have to do the cast. BindingList<Tdoesn't impose
that T : class, so "as" isn't an option, so they would have to write:

if(sender is T) {
T item = (T) sender;
}

So they've only introduced *extra* code by using "is"; the cast will
still do the check anyway. And as the comment explains, the exception
is expected to be the rare case, so it makes reasonable sense to
simply catch it if a problem happens. In reality: we are talking about
data-binding here - lots of events/delegate invokes/indirection via
System.ComponentModel. Minute things like the difference between is/as/
cast are going to be such a tiny portion of the overall CPU cost that
even if it was done the most expensive way (of the three mentioned) it
would never manifest as a bottleneck; and I'd rather MS concentrate on
the bigger picture ;-p

Marc
Jun 27 '08 #2
On Tue, 24 Jun 2008 23:52:48 -0700, Marc Gravell <ma**********@gmail.com
wrote:
>implies that a throwing cast is, after all, faster than a check for the
non-throw

No - the point here is that even if they do an "is" test, they are
*still* going to have to do the cast. BindingList<Tdoesn't impose
that T : class, so "as" isn't an option, so they would have to write:

if(sender is T) {
T item = (T) sender;
}
To be fair, I think they'd have written:

T item = sender as T;

if (item != null)
{
...
}

Now, whether that's in fact more costly than casting in the successful
case, I don't know. But let's make sure we're comparing apples to apples
here, rather than introducing some artificial performance problem. :)

Do you have some reason to believe that they wouldn't write the above,
rather than the code you suggested? Or are you saying that writing "as"
winds up basically doing the "is" followed by the cast, just hidden by the
compiler?
[...] Minute things like the difference between is/as/
cast are going to be such a tiny portion of the overall CPU cost that
even if it was done the most expensive way (of the three mentioned) it
would never manifest as a bottleneck;
I agree, which begs the question: why did the author of that code think it
was important enough not only to write it that way, but to specifically
call out a performance difference?

It's a bit of a moot question, I realize. After all, even if a
performance optimization is deemed worthwhile within the framework, that
doesn't mean the same optimization is justifiable in end-user code. ButI
admit to having my curiosity piqued, now that the question has been
asked. It'd be nice to have something a little more definitive as an
answer, I think.

Pete
Jun 27 '08 #3
To be fair, I think they'd have written:

No; they *can't* have; I'll repeat: BindingList<Tdoes not impose
that T : class, therefore the following is invalid:

T item = sender as T;
Error 1 The type parameter 'T' cannot be used with the 'as' operator
because it does not have a class type constraint nor a 'class'
constraint

Add that in with the rest of the comments, and I think that explains
everything.

Marc
Jun 27 '08 #4
On Wed, 25 Jun 2008 00:32:56 -0700, Marc Gravell <ma**********@gmail.com>
wrote:
>To be fair, I think they'd have written:

No; they *can't* have; I'll repeat: BindingList<Tdoes not impose
that T : class
Ah, okay. I guess the answer to my question "Do you have some reason to
believe that they wouldn't write the above" is "yes" then. :)

Teach me to post when I'm sleep deprived. I apparently skipped over some
important words in your previous post. Thanks for the clarification.

All that said, I still agree that I find it odd that they'd go out of
their way to avoid using "is", given that there are probably better ways
to spend their time. :)

Pete
Jun 27 '08 #5

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

Similar topics

4
2946
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer...
6
3651
by: Zenon | last post by:
Folks, I am having a terrible time overloading operators. I have tried what I thought was the correct way, I tried the cheating (friend declarations), all to no avail. Sorry for posting tons of...
2
7277
by: Ulrich Heinen | last post by:
Let's say I have two classes A and B with similar goals but completely different implementations. In my application code I want to use class A exclusively, but instances of B should also be...
5
1496
by: Gernot Frisch | last post by:
Hi, I have a problem. I want to be able to write this: a = b + d + c; Where a can be a double or class A; b,d,c can each indiviually be one of these: double, class A or class B. The result...
11
6468
by: A Traveler | last post by:
I was just curious if anyone knows how the combinations of And/AndAlso and Or/OrElse compare in terms of performance. Which takes more work for the system, performing two evaluations on an And or...
16
1906
by: D. Stimits | last post by:
A non-profit organization is interested in a new data application that would use a SQL storage system. I'm interested to know how non-profit companies that are not selling products are considered...
5
2404
by: Suman | last post by:
Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++ archives and Stroustrup's FAQs (particularly the following: <url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>) I am...
3
2121
by: Ross | last post by:
I have a problem regarding the precedence of overloaded cast operators. My program is as follows: #include <stdio.h> class A; class B { public: B(A &a) {
7
1839
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 5 - Expressions * exercises 5.1 and 5.2 */ #include <iostream>
0
7260
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
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.