From 6608914408462621906
X-Google-Thread: f78e5,bd52daa8ac2e51f1
X-Google-Thread: 109fba,bd52daa8ac2e51f1
X-Google-Attributes: gidf78e5,gid109fba,public,usenet
X-Google-Language: ENGLISH,ASCII
Path: g2news2.google.com!news2.google.com!news.glorb.com!news.alt.net!comp-std-cpp-robomod!not-for-mail
From: "terminator(jam)" <farid.mehrabi@gmail.com>
Newsgroups: comp.std.c++,comp.lang.c++
Subject: Re: pre return optimization
Date: Tue, 16 Oct 2007 11:55:54 CST
Organization: http://groups.google.com
Lines: 146
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <1192533048.201465.58760@k35g2000prh.googlegroups.com>
References: <1191919832.635392.256930@d55g2000hsg.googlegroups.com>
   <1192343829.469097.231500@i13g2000prf.googlegroups.com>
   <1192366892.063805.202140@k35g2000prh.googlegroups.com>
   <LwtQi.11302$ZA.7395@newsb.telia.net>
   <1192434165.863496.171650@q3g2000prf.googlegroups.com>
   <HXLQi.11372$ZA.7596@newsb.telia.net>
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1192533048 22656 127.0.0.1 (16 Oct 2007 11:10:48 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 16 Oct 2007 11:10:48 +0000 (UTC)
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
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Media Center PC 3.0; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2),gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: k35g2000prh.googlegroups.com; posting-host=87.247.164.107;
   posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0
X-Virus-Scanned: amavisd-new at ucar.edu
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
X-MIME-Autoconverted: from quoted-printable to 8bit by mulga.csse.unimelb.edu.au id l9GBB4gw014093
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
Xref: g2news2.google.com comp.std.c++:854 comp.lang.c++:22857

On Oct 15, 8:35 pm, Erik Wikstr�m <Erik-wikst...@telia.com> wrote:
> ===================================== MODERATOR'S COMMENT:
>
> Please trim unnecessary material when replying.
>
> ===================================== END OF MODERATOR'S COMMENT
> On 2007-10-15 17:52, terminator(jam) wrote:
>
>
>
>
>
> > On Oct 14, 10:00 pm, Erik-wikst...@telia.com (Erik Wikstr�m) wrote:
> >> On 2007-10-14 21:19, terminator wrote:
>
> >> > On Oct 14, 11:08 am, Greg Herlihy <gre...@pacbell.net> wrote:
> >> >> 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.
>
> >> > I knew about RVO but not NRVO(that is what I am talking about).Is NRVO
> >> > standard behavoir or unspecified?
> >> > At least my compiler does not perform the NRVO.
>
> >> The standard allows this optimisation, but it does not require it (see
> >> section 12.8 paragraph 15). I think that most newer compilers support
> >> it, but you might have to turn up the optimisation level a bit.
>
> >> One important thing to remember is that when NRVO is in use the
> >> destructor will not be called for the local object, nor will the copy-
> >> constructor for the non-local object (m1 to m3 in the code above) be
> >> called. Thus you should not depend on any side effects that the copy-
> >> constructor or destructor might have.
>
> > prior to return and after return are two different case to me:I mean
> > before return even move should be disabled ,while after the return
> > either of move/copy must perform .
>
> Sorry, but you lost me there, what move? Of what to where?
>

Move ctor of course.

regards,
FM.


---
[ 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                      ]



