[std-interval] C++ interval std
eliasen at mindspring.com
Wed Apr 5 14:27:57 PDT 2006
> Alan Eliasen <eliasen at mindspring.com> writes:
> > I was curious to see about the speed difference between passing by
> >value and passing by reference, because passing by value always has been
> >slower and always *has* to be slower because it (unnecessarily) creates
> >a copy of the object when passed in.
Lawrence.Crowl at sun.com wrote:
> It does not unnecessarily create an object. In particular, when the
> parameter to a function is the result of an expression, the const ref
> approach requires allocating a temporary and passing its address. In
> contrast, pass-by-value can put the result of the expression directly
> into the parameter register; no copies are required.
This is totally implementation-dependent in the compiler. Of course,
a smart compiler could put the result of pass-by-constant reference into
a register too. It may already be in a register.
It could be argued that pass-by-value *always* creates temporaries
because of calls to the copy constructor for each argument passed in to
a function or method. As I noted before, a smart compiler can optimize
this away, but these copy constructor calls are almost always unnecessary.
> >I tried three ways on x86_64, gcc 3.4.4, linux:
> I would need the benchmark source in order to evaluate it and to
> execute it on other systems.
That was posted last night. My benchmark was very simple indeed, and
you may choose to write your own.
> In developing the SPARC V9 ABI, I showed a 7x performance advantage for
> pass-by-value over pass-by-reference when computing Mandelbrot with
> complex numbers.
That's an impressive difference! I'd like to see that benchmark
source too and test it on other compilers.
> > Passing in unmodifiable objects as constant references when
> >appropriate both obviates the need for copying and gives explicit
> >const-correctness from the start. We don't have to worry about
> >dereferencing pointers or memory ownership issues. It's indeed the
> >pattern followed by most modern libraries.
> You are assuming that "const" means "unmodifiable".
No, of course I'm not.
> It does not. It
> means that non-mutuable fields may not change when accessed through the
> const-qualified name. The object may still change via access through
> non-const-qualified names. The distinction between a value and a
> reference is critical here, because the former is unaliased and the
> latter is potentially aliased. This aliasing can also have substantial
> effect on the ability of the compiler to apply global optimizations.
I can see the point that aliasing could be an issue if functions are
modifying the objects passed in. That would seem to me to be very bad
programming practice, (one in which explicitly declaring const-ness
would help the compiler point these out.)
If the issue is complete safety against bad programmers, then we
should do pass-by-value everywhere and copy the object every time it's
used. It'd be safer.
However, my entire point earlier was to argue against the statements
made that pass-by-value would or should be faster. My benchmarks showed
that in the most common compilers used in the world, on the most common
hardware architectures, that pass-by-value indeed wasn't faster, but
rather significantly slower at all optimization levels. I'd be
interested to see a platform where the reverse was true.
> > I recommend that for long-term performance and const-correctness,
> >that objects be passed as constant references when possible.
> Would you recommend that ints be passed by const reference?
Of course not. That's why I said "objects", meaning instantiations
of classes. ints don't incur copy constructors and destructors like
objects do when passed as function arguments. To save time, let's
assume that any other rookie mistakes that you might want to try to
ascribe to me in an effort to make me look foolish will also be answered
the same way. :)
Alan Eliasen | "When trouble is solved before it
eliasen at mindspring.com | forms, who calls that clever?"
http://futureboy.us/ | --Sun Tzu
More information about the Std-interval