Re: Does binding to const-reference outperform copy-initialization from returned value?

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Alex Blekhman" <tkfx.REMOVE@xxxxxxxxx> wrote in message
news:ety3rnr3JHA.4960@xxxxxxxxxxxxxxxxxxxx
"Niels Dekker" wrote:
When VC++ 2008 SP1 compiles the following test as "Release", the
compiler inlines constValue.VirtualFunc(), but it doesn't inline
constReference.VirtualFunc(). Unfortunately...

[...]

I guess there's an opportunity for further improvement of the
optimizer of Visual C++, right?

Such optimization won't happen in the foreseeable future.
Otherwise it would defy the whole purpose of virtual functions,
namely polymorphism. In order polymorphism to work in C++ a
function call must be made via polymorphic type. The only
polymorphic types are pointers and references. So, a call to
virtual method of a class via reference cannot be optimized
without breaking C++ rules.

Except that, in this particular code, the reference is guaranteed to be
bound to an instance of Foo and not to an instance of its derived class,
and thus polymorphism is not in the picture. So the optimizer could, in
principle, generate a non-virtual call to VirtualFunc without changing
the observable behavior of this program. Remember - a compiled program
doesn't have to follow rules, it just has to behave as if it does.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: [EGN] Hoisting Loop Invariants (Was: Re: [EGN] Numerical Accuracy)
    ... >> circumvent the automatic optimizer have as their purpose vanity alone. ... > While this may be true of certain practices, hoisting loop invariants is ... the programmer has this knowledge, then the programmer should not use ... >> this scenario is the fact that the function uses a REFERENCE parameter ...
    (comp.programming)
  • Re: Problem with Garbage Collection
    ... garbage collector deletes the class from my first worker thread before the ... The specific purpose of GC.KeepAlive is to add a line of code that won't be optimized away and thus keeping a point in your code where you're guaranteed to have a reference to the object in that variable. ... The GC is very aggressive, and couple this with the optimizer which might notice that there's really no point in setting dlm to null, and you have the problem that the object in dlm is collected before GetObjectList returns, simply because that's the last point in your code ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: shared memory between processes
    ... > a register and then refer to the register, not the memory location, ... (I haven't checked the assembler output). ... > interlock and that the optimizer never removes the reference to the ... What do you mean by "reference to the actual memory location"? ...
    (comp.os.linux.development.system)
  • Re: Does binding to const-reference outperform copy-initialization from returned value?
    ... compiler inlines constValue.VirtualFunc, but it doesn't inline ... optimizer of Visual C++, right? ... function call must be made via polymorphic type. ... polymorphic types are pointers and references. ...
    (microsoft.public.vc.language)
  • Re: reference VS. pointer
    ... The pointer is faster than reference! ... > There is no point in benchmarking most compiler performances in the ... Also when the optimizer is on, there is big chance it doesn't matter whether ...
    (comp.lang.cpp)