| 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.
.
|
|
|
|
|

|
Related Articles |
|
|