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_PropertyC hanged:
// 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(InvalidCa stException) {
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? 4 2541
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<Tdo esn'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.Componen tModel. 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
On Tue, 24 Jun 2008 23:52:48 -0700, Marc Gravell <ma**********@g mail.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<Tdo esn'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
To be fair, I think they'd have written:
No; they *can't* have; I'll repeat: BindingList<Tdo es 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
On Wed, 25 Jun 2008 00:32:56 -0700, Marc Gravell <ma**********@g mail.com>
wrote:
>To be fair, I think they'd have written:
No; they *can't* have; I'll repeat: BindingList<Tdo es 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
http://alexvn.freeservers.com/s1/perfometer.html
|
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 code but I
really need help.
thanks,
Zenon
|
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 accepted. This can be
achieved when B provides an appropriate cast operator, e.g.
class A {
public:
/* ... */
protected:
/* ... */
|
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 of an addition is always class A, or double (double +
double can't be changed).
|
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 performing short-circuiting on an AndAlso?
Purely for enlightenment.
Thanks in advance.
- Arthur Dent.
| |
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 for licensing.
Can they use PostgreSQL just like anyone else or do non-profits qualify
as commercial use and need to consider other licensing?
D. Stimits, stimits AT comcast DOT net
---------------------------(end of...
|
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 left to wondering why Stroustrup doesn't mention the
*_cast<> operators or operator .* in that particular FAQ.
Archived discussions have convinced me that .* _cannot_ be overloaded.
I have gathered a notion (perhaps incorrectly) that the *_cast<>...
|
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) {
|
by: arnuld |
last post by:
/* C++ Primer - 4/e
* chapter 5 - Expressions
* exercises 5.1 and 5.2
*/
#include <iostream>
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |