[std-interval] C++ interval std

Lawrence.Crowl at Sun.com Lawrence.Crowl at Sun.com
Wed Apr 5 06:24:54 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.

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.

Pass-by-reference aften introduces additional copies because the
programmer must copy the parameter into a local variable in order to
avoid aliases between parameters.

 >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.

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.

 >   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".  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.

 >   To the caller of the functions, it just looks like an ordinary
 >pass-by-value, but is faster, and allows the compiler to catch a lot of
 >types of problems.

But it doesn't look the same, because of the aliasing issue.  With
reference parameters, the function must make an explicit declaration
about its behavior in the presence of aliasing.

 >   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?

  Lawrence Crowl             650-786-6146   Sun Microsystems, Inc.
                   Lawrence.Crowl at Sun.com   16 Network Circle, UMPK16-303
           http://www.Crowl.org/Lawrence/   Menlo Park, California, 94025


More information about the Std-interval mailing list