PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Re: pre return optimization
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: pre return optimization

Réponse
 
LinkBack Outils de la discussion
Vieux 16/10/2007, 06h44   #1 (permalink)
int19h@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pre return optimization

On Oct 9, 7:56 pm, "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


It won't. It certainly won't do that with the example given, since
there's no clear association in it between the copy constructor and
the assignment operator (considering that the latter isn't even
defined). Assuming there was such an association, though, I'd rather
expect to see:

mem pig default
mem pig copy // first assignment
mem pig copy // second assignment
mem pig finish

Which is correct and fine - if you have explicitly written the
assignment operator twice, then surely you want the associated side-
effects? And if you do not, then perhaps you shouldn't have written it
that way.

> that is you can refrence the returned object inside the function and
> decrease the overhead for copying large objects.C++ lacks such syntax
> and IMHO we should be able to mark the result object as referencing
> the actual return so that there is no need for the extra copy
> construction


This is precisely what NRVO does.

I have a feeling that you're confusing assignment with initialization
in this case. Assignment operator always applies to an already-
initialized object - the compiler cannot elide the initialization
except for the trivial cases (e.g. POD types), which is why operator=
still results in unnecessary copying even when the compiler implements
NRVO. It's not a problem with NRVO though - it's because assignment is
used where just initialization would suffice. Direct initialization is
still more efficient then assignment-following-initialization, even
with the move semantics of C++0x.

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

  Réponse avec citation
Vieux 16/10/2007, 20h26   #2 (permalink)
terminator(jam)
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pre return optimization

On Oct 16, 8:44 am, int...@gmail.com wrote:
> On Oct 9, 7:56 pm, "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

>
> It won't. It certainly won't do that with the example given, since
> there's no clear association in it between the copy constructor and
> the assignment operator (considering that the latter isn't even
> defined). Assuming there was such an association, though, I'd rather
> expect to see:
>
> mem pig default
> mem pig copy // first assignment
> mem pig copy // second assignment
> mem pig finish
>

As I wrote before its just an illustration and I did not mean any
assignment,replace it with any function( eg. modify(result) .

> Which is correct and fine - if you have explicitly written the
> assignment operator twice, then surely you want the associated side-
> effects? And if you do not, then perhaps you shouldn't have written it
> that way.
>
> > that is you can refrence the returned object inside the function and
> > decrease the overhead for copying large objects.C++ lacks such syntax
> > and IMHO we should be able to mark the result object as referencing
> > the actual return so that there is no need for the extra copy
> > construction

>
> This is precisely what NRVO does.
>


I was certain about the NRVO by the time I started the post.However
NRVO is a compiler option(ugly!!!) not a demand .With NRVO we have two
distinct phases optimize simultaniously : prior to return inside the
callee and after that inside the caller:

A foo(){
A ret;//this will be return under any conditions.
//....
return ret;//pre return :no copy/move
};
class A a=foo();//after return : copy/move to a


> I have a feeling that you're confusing assignment with initialization
> in this case. Assignment operator always applies to an already-
> initialized object - the compiler cannot elide the initialization
> except for the trivial cases (e.g. POD types), which is why operator=
> still results in unnecessary copying even when the compiler implements
> NRVO. It's not a problem with NRVO though - it's because assignment is
> used where just initialization would suffice. Direct initialization is
> still more efficient then assignment-following-initialization, even
> with the move semantics of C++0x.


Everybody makes mistakes while learning , but confusing assignment
with copy certainly has never been the mistake I can make.

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 ]

  Réponse avec citation
Vieux 17/10/2007, 18h53   #3 (permalink)
int19h@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pre return optimization

On 16 Oct, 23:26, "terminator(jam)" <farid.mehr...@gmail.com> wrote:
> A foo(){
> A ret;//this will be return under any conditions.
> //....
> return ret;//pre return :no copy/move};
>
> class A a=foo();//after return : copy/move to a


This is not the case. If the compiler correctly implements NRVO, the
example above will result in a single construction of A using the
default constructor, and then a subsequent destruction. No copies or
moves are made at any point.

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h33.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13869 seconds with 11 queries