Boost logo

Boost :

From: Mat Marcus (mmarcus_at_[hidden])
Date: 2002-10-13 09:43:59


--On Sunday, October 13, 2002 8:52 AM -0400 David Abrahams
<dave_at_[hidden]> wrote:

> Furthermore, if the function being called is not inlined,
> pass-by-const& can result in a whole lot less code being
> generated than if pass-by-value is used.

Its interesting that you mention this. I'm not sure if this is
a useful data point,, but I recently ran across a case where
the opposite was true. I don't think it change the discsussion
much, and it is probably just be a compiler bug, but I thought
i'd mention it jsut the same.

With the Metrowerks Macintosh CFM targeted compilers
call-by-const& can sometimes result in worse code. The issue
involves passing actaul constants to a function by value versus
by const&. If, say, an integer constant is passed by value the
compiler generates reasonable code (see line 0000000C: in the
CallByVal disassembly below). But in the pass by const& case
the compiler decides that it has to add an entry to for the
constant kNumbeOner the TOC (think of it as global data area).
So we pay for at least an extra TOC entry (space +
initialization code). Perhaps the compiler is trying to be
"friendly" to folks who might cast-away-const? In any case, we
actually had to clean up a bunch of code that suffered from
this bloat recently.

 - Mat

const int kNumberOne=1;

void ByVal(int i) {
        
};
void ByRef(const int& i) {
};

void CallByVal()
{
        ByVal(kNumberOne);
}

/*
Hunk: Kind=HUNK_GLOBAL_CODE
Align=16 Class=PR Name=".CallByVal__Fv"(3) Size=36
00000000: 7C0802A6 mflr r0
00000004: 90010008 stw r0,8(SP)
00000008: 9421FFC0 stwu SP,-64(SP)
0000000C: 38600001 li r3,1
00000010: 48000001 bl .ByVal__Fi
00000014: 80010048 lwz r0,72(SP)
00000018: 38210040 addi SP,SP,64
0000001C: 7C0803A6 mtlr r0
00000020: 4E800020 blr
*/

void CallByRef()
{
        ByRef(kNumberOne);
}

/*
Hunk: Kind=HUNK_GLOBAL_CODE
Align=16 Class=PR Name=".CallByRef__Fv"(6) Size=36
00000000: 7C0802A6 mflr r0
00000004: 90010008 stw r0,8(SP)
00000008: 9421FFC0 stwu SP,-64(SP)
0000000C: 38620000 addi r3,RTOC,kNumberOne
00000010: 48000001 bl .ByRef__FRCi
00000014: 80010048 lwz r0,72(SP)
00000018: 38210040 addi SP,SP,64
0000001C: 7C0803A6 mtlr r0
00000020: 4E800020 blr
*/


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk