[LC++]Using the "return value optimization"

Jack Lloyd lloyd at acm.jhu.edu
Tue Aug 14 00:28:04 UTC 2001


> or do void functions give some performance gain I'm not aware of?

I guess you don't have to mess around with the stack quite so much (x86
standard calling convention are very non-optimal, unfortunately). But I think
the performance gain would have to be *quite* minimal (consider than basically
every single ANSI C/Unix function call returns an int for error signaling, so
I imagine that most C (and by extension C++) compilers take care to make that
relatively efficient.)

Of course on a decent machine (ie RISC), there will be enough registers and a
sane enough calling convention that the return value will just get left in a
register and get nabbed by the calling function if it wants it.

BTW, I don't think the return value optimization is relevant to built-in
types. IIRC the idea is that you can replace a constructor/operator= pair with
just the constructor. Eg,

classA bar() { return A(a, b, quux(c)); }

classA foo = bar();

Is converted into something that calls the constructor only once, for foo,
instead of contructing a temporary, copying it, and the destroying the temp.
If copying involves lots of ops (generally when a class is using lots of
memory), this can be a big win.

J




More information about the tuxCPProgramming mailing list