473,231 Members | 1,695 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,231 software developers and data experts.

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 2506
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
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
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
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
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
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
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
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
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
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 5 - Expressions * exercises 5.1 and 5.2 */ #include <iostream>
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.