Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Range-based loop optimization

Reply
Thread Tools

Range-based loop optimization

 
 
Juha Nieminen
Guest
Posts: n/a
 
      05-11-2012
Assume that I have something along the lines of:

//----------------------------------------------------
struct Something
{
std::vector<SomethingElse> aBigVector;
std::set<FooBar> aBigSet;
};
//----------------------------------------------------

and then I inadvertently do something like this:

//----------------------------------------------------
std::vector<Something> container;
populate(container);

for(auto element : container)
// code that does not modify 'element'
//----------------------------------------------------

What I *should* have done is, of course, this:

//----------------------------------------------------
for(const auto& element : container)
...
//----------------------------------------------------

Or even just:

//----------------------------------------------------
for(auto& element : container)
...
//----------------------------------------------------

If, however, I mistakenly do it without a reference, will the compiler
be able to optimize the copying of the large elements if it sees that
they are not modified in the loop body?

If the compiler is unable to optimize the copying away, I'm thinking
that the range-based loop is way too easy to use in an inefficient manner
by mistake. This is not a very good thing.
 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      05-11-2012
Juha Nieminen wrote:

> Assume that I have something along the lines of:
>
> //----------------------------------------------------
> struct Something
> {
> std::vector<SomethingElse> aBigVector;
> std::set<FooBar> aBigSet;
> };
> //----------------------------------------------------
>
> and then I inadvertently do something like this:
>
> //----------------------------------------------------
> std::vector<Something> container;
> populate(container);
>
> for(auto element : container)
> // code that does not modify 'element'
> //----------------------------------------------------
>
> What I *should* have done is, of course, this:
>
> //----------------------------------------------------
> for(const auto& element : container)
> ...
> //----------------------------------------------------
>
> Or even just:
>
> //----------------------------------------------------
> for(auto& element : container)
> ...
> //----------------------------------------------------
>
> If, however, I mistakenly do it without a reference, will the compiler
> be able to optimize the copying of the large elements if it sees that
> they are not modified in the loop body?


I don't think it is allowed to. Copy elision happens in a very limited
set of explicitly listed cases, and they are all about omitting a
temporary when creating a new object, which isn't the case here.

On the other hand, if your copy constructor has no side effect and is
simple enough for the compiler to understand, it will optimize and
remove most of the actions of copying. It depends what is in the body
of your for loop (if it is empty...), but it would require a very
clever compiler for a std::set to fall into this category. For
std::vector<POD>, it seems almost within reach of current compilers.
 
Reply With Quote
 
 
 
 
woodbrian77@gmail.com
Guest
Posts: n/a
 
      05-12-2012
On Friday, May 11, 2012 6:30:52 AM UTC-5, Juha Nieminen wrote:
> Assume that I have something along the lines of:
>
> //----------------------------------------------------
> struct Something
> {
> std::vector<SomethingElse> aBigVector;
> std::set<FooBar> aBigSet;
> };
> //----------------------------------------------------
>
> and then I inadvertently do something like this:
>
> //----------------------------------------------------
> std::vector<Something> container;
> populate(container);
>
> for(auto element : container)
> // code that does not modify 'element'
> //----------------------------------------------------
>
> What I *should* have done is, of course, this:
>
> //----------------------------------------------------
> for(const auto& element : container)
> ...
> //----------------------------------------------------
>
> Or even just:
>
> //----------------------------------------------------
> for(auto& element : container)
> ...
> //----------------------------------------------------
>
> If, however, I mistakenly do it without a reference, will the compiler
> be able to optimize the copying of the large elements if it sees that
> they are not modified in the loop body?
>
> If the compiler is unable to optimize the copying away, I'm thinking
> that the range-based loop is way too easy to use in an inefficient manner
> by mistake. This is not a very good thing.


I've thought about that also, but probably if an author
isn't aware of it, someone will catch it in a code review.

I remember a thread a few months ago about studying code
from a project. If such an author studied this code --
http://webEbenezer.net/misc/direct.tar.bz2
they might notice the use of & in this context and
investigate it further.

http://webEbenezer.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Zero Optimization and Sign Optimization??? Ravikiran C Programming 22 11-24-2008 03:19 AM
java loop optimization star Java 38 09-22-2005 09:13 AM
Loop Optimization, Array Alignment Rajeev C Programming 7 09-18-2004 12:37 PM
Simple loop optimization mrTriffid Java 7 02-08-2004 06:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57