Use a prefix increment operator (++i) in iterators instead of a postfix (i++) operator

The fragment is taken from the Unreal Engine 4 project. Ineffective code is detected by the following diagnostic: V803 Decreased performance. In case ‘itr’ is iterator it’s more effective to use prefix form of increment. Replace iterator++ with ++iterator.

void FSlateNotificationManager::GetWindows(....) const
{
  for( auto Iter(NotificationLists.CreateConstIterator());
       Iter; Iter++ )
  {
    TSharedPtr<SNotificationList> NotificationList = *Iter;
    ....
  }
}

Explanation

If you hadn’t read the title of the article, we think it would’ve been quite hard to notice an issue in the code. At first sight, it looks like the code is quite correct, but it’s not perfect. Yes, we are talking about the postfix increment – ‘Iter++’. Instead of a postfix form of the increment iterator, you should rather use a prefix analogue, i.e. to substitute ‘Iter++’ for ‘++Iter’. Why should we do it, and what’s the practical value of it? Here is the story.

Effective code:

for( auto Iter(NotificationLists.CreateConstIterator());
     Iter; ++Iter)

Recommendation

The difference between a prefix and a postfix form is well known to everybody. We hope that the internal structure distinctions (which show us the operational principles) are not a secret as well. If you have ever done the operator overloading, then you must be aware of it. If not – we’ll give a brief explanation. (All the others can skip this paragraph and go to the one, which follows the code examples with operator overloading)

increment_variable_i

The prefix increment operator changes an object’s state, and returns itself in the changed form. No temporary objects required. Then the prefix increment operator may look like this:

MyOwnClass& operator++()
{
  ++meOwnField;
  return (*this);
}

A postfix operator also changes the object’s state but returns the previous state of the object. It does so by creating a temporary object, then the postfix increment operator overloading code will look like this:

MyOwnClass operator++(int)
{
  MyOWnCLass tmp = *this;
  ++(*this);
  return tmp;
}

Looking at these code fragments, you can see that an additional operation of creating a temporary object is used. How crucial is it in practice?

Today’s compilers are smart enough to do the optimization, and to not create temporary objects if they are of no use. That’s why in the Release version it’s really hard to see the difference between ‘it++’ and ‘++it’.

But it is a completely different story when debugging the program in the Debug-mode. In this case the difference in the performance can be really significant.

For example, in this article there are some examples of estimation of the code running time using prefix and postfix forms of increment operators in the Debug-version. We see that is almost 4 times longer to use the postfix forms.

Those, who will say, “And? In the Release version it’s all the same!” will be right and wrong at the same time. As a rule we spend more time working on the Debug-version while doing the Unit-tests, and debugging the program. So quite a good deal of time is spent working with the Debug version of software, which means that we don’t want to waste time waiting.

In general we think we’ve managed to answer the question – “Should we use the prefix increment operator (++i) instead a of postfix operator (i++) for iterators”. Yes, you really should. You’ll get a nice speed-up in the Debug version. And if the iterators are quite “heavy”, then the benefit will be even more appreciable.

References (reading recommendation):

Written by Andrey Karpov.
This error was found with PVS-Studio static analysis tool.

One thought on “Use a prefix increment operator (++i) in iterators instead of a postfix (i++) operator

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s