From -4976750708421112924
X-Google-Thread: f78e5,bd52daa8ac2e51f1
X-Google-Thread: 109fba,bd52daa8ac2e51f1
X-Google-Attributes: gidf78e5,gid109fba,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Sun, 14 Oct 2007 02:10:24 -0500
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.csse.unimelb.edu.au: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Original-To: std-c++@mailman.ucar.edu
Delivered-To: std-c++@mailman.ucar.edu
X-SMTP-Auth: no
From: Greg Herlihy <greghe@pacbell.net>
Newsgroups: comp.std.c++,comp.lang.c++
Subject: Re: pre return optimization
Organization: http://groups.google.com
Message-ID: <1192343829.469097.231500@i13g2000prf.googlegroups.com>
References: <1191919832.635392.256930@d55g2000hsg.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
X-Complaints-To: groups-abuse@google.com
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/523.1 (KHTML, like Gecko) Version/3.0.3 Safari/523.1,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: i13g2000prf.googlegroups.com; posting-host=209.204.165.36;
   posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0
X-Virus-Scanned: amavisd-new at ucar.edu
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
Date: Sun, 14 Oct 2007 02:08:09 CST
Lines: 128
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-wLjmP7Q0yv8aZ67ABZnPG2e1df0kpO4uTwScXpNUoZdf6s4pioR3eDQt94y/hsn1L4U2BzPffsfMPp6!9oW0cu5T/HKn/7dkDp0yNvaJK7e2Sjpf06HyY3Y74dxmrZclSJf+i7uoCjh+lPsGNGhORfuevBzC!YtMCLEnZ0ys=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.36
Xref: g2news2.google.com comp.std.c++:833 comp.lang.c++:22539

On Oct 9, 8:56 am, "terminator(jam)" <farid.mehr...@gmail.com> wrote:
> consider:
>
> struct memory_pig{//a really large type:
>
>         memory_pig(){
>                 std::cout<<"mem pig default\n";
>                 //etc...
>         };
>
>         memory_pig(memory_pig const&){
>                 std::cout<<"mem pig copy\n";
>                 //etc...
>         };
>
>         ~memory_pig(){
>                 std::cout<<"mem pig finish\n";
>                 //etc...
>         };
>
>         //etc...
>
> };///struct memory_pig
>
> memory_pig foo(){
>         memory_pig result;
>         result=something;
>         //etc...
>         result=something_else;
>         return result;
>
> };
>
> any time 'foo' is called the output will contain the following
> sequence:
>
> mem pig default
> mem pig copy
> mem pig finish
>
> the last line of output may repeat based on how the result is
> stored(rvo) or not.
> So,two objects of a large type will be constructed and at least one is
> destructed on every call to 'foo'

The C++ Standard already allows the "(Named) Return Value
Optimization" (NRVO or RVO for short). The optimization allows (under
certain conditions) the compiler to construct the result of a function
call "in place" - that is, directly in the object initialized with the
function call result.

So, to take advantage of this optimization, a program should use the
result of a function call  to initialize an object, instead of
assigning the function result to an existing object.

For example:

    #include <iostream>

    using std::cout;

    struct memory_pig
    {    // a really large type:
        memory_pig()
        {
            cout << "mem pig default\n";
        }
        memory_pig(memory_pig const&)
        {
            cout << "mem pig copy\n";
        }
        ~memory_pig()
        {
            cout << "mem pig finish\n";
        }
    };

    memory_pig foo()
    {
        memory_pig result;
        // ...
        return result;
    }

    int main()
    {
        memory_pig m1 = foo();
        memory_pig m2 = foo();
        memory_pig m3 = foo();
    }

I compiled the above program twice, once with and once without NRVO.
The output of both programs is shown in the two columns below. As this
comparison shows, NRVO can be a particular effective optimization -
even for a small C++ program like the one used in this example.

Program Output

Without NRVO:                 With NRVO:

mem pig default               mem pig default
mem pig copy                  mem pig default
mem pig finish                mem pig default
mem pig copy                  mem pig finish
mem pig finish                mem pig finish
mem pig default               mem pig finish
mem pig copy
mem pig finish
mem pig copy
mem pig finish
mem pig default
mem pig copy
mem pig finish
mem pig copy
mem pig finish
mem pig finish
mem pig finish
mem pig finish

Greg


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



