Optimisation, function returns, STL containers



 DEVELOP > c-Plus-Plus > Optimisation, function returns, STL containers

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Simon Elliott"
Date: 12 Nov 2003 10:53:03 AM
Object: Optimisation, function returns, STL containers
What optimisation do compilers typically provide when passing STL
containers around?
For example, if I do something like this:
struct Tbloggs
{
std::string s1;
};
typedef std::vector<Tbloggs> TbloggsList;
typedef std::vector<TbloggsList> TbloggsListContainer;
TbloggsList getBloggsList(void)
{
TbloggsList tempBloggsList;
Tbloggs b1;
tempBloggsList.push_back(b1);
return(tempBloggsList);
}
TbloggsListContainer myBloggsListContainer;
TbloggsList myBloggsList = getBloggsList();
myBloggsListContainer.push_back(myBloggsList);
In theory there's a whole lot of unnecessary copying going on here, in
the "push_back"s and the getBloggsList() return. How much of this is the
compiler allowed to optimise away?
--
Simon Elliott
http://www.ctsn.co.uk/
.

User: "Victor Bazarov"

Title: Re: Optimisation, function returns, STL containers 12 Nov 2003 11:32:18 AM
"Simon Elliott" <simon@nospam.demon.co.uk> wrote...

What optimisation do compilers typically provide when passing STL
containers around?

The same as for any other classes. That's the whole point.

For example, if I do something like this:

struct Tbloggs
{
std::string s1;

Supposedly 'std::string' has been defined here...

};
typedef std::vector<Tbloggs> TbloggsList;
typedef std::vector<TbloggsList> TbloggsListContainer;

TbloggsList getBloggsList(void)
{
TbloggsList tempBloggsList;
Tbloggs b1;
tempBloggsList.push_back(b1);
return(tempBloggsList);
}

TbloggsListContainer myBloggsListContainer;
TbloggsList myBloggsList = getBloggsList();
myBloggsListContainer.push_back(myBloggsList);

In theory there's a whole lot of unnecessary copying going on here, in
the "push_back"s and the getBloggsList() return. How much of this is the
compiler allowed to optimise away?

The construction of 'myBloggList' object is permitted to happen
"in place" (see 12.8/15). It's called "return value optimisation".
Victor
.
User: "lilburne"

Title: Re: Optimisation, function returns, STL containers 12 Nov 2003 02:25:43 PM
Victor Bazarov wrote:

"Simon Elliott" <simon@nospam.demon.co.uk> wrote...

What optimisation do compilers typically provide when passing STL
containers around?




In theory there's a whole lot of unnecessary copying going on here, in
the "push_back"s and the getBloggsList() return. How much of this is the
compiler allowed to optimise away?



The construction of 'myBloggList' object is permitted to happen
"in place" (see 12.8/15). It's called "return value optimisation".

Though it won't happen in assignments, so you'll need to be
carefull of any other statements like:
myBloggsList = getBloggsList();
which isn't a construction. But you should be aware of this
with any object that is returned by value.
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER