Ticket #1064 (closed enhancement: fixed)

Opened 17 months ago

Last modified 17 months ago

[PATCH] Performance improvement. It is more effective to use the prefix form of ++iterator (vs iterator++)

Reported by: Echelon9 Owned by:
Priority: Should Have Milestone: Alpha 9
Component: Core engine Keywords: review, STL
Cc:

Description

An iterator is changed in the program code by the increment/decrement postfix operator. Since the previous iterator's value is not used, you may replace the postfix operator with the prefix one. In some cases, the prefix operator will work faster than the postfix one, especially in Debug-versions.

The prefix increment operator changes the object's state and returns itself already changed.

The situation with the postfix increment operator is more complicated. The object's state must change but it is the previous state which is returned. So an additional temporary object is created.

Reference: Scott Meyers' Rule 6. Distinguish between prefix increment and decrement operators, in:

Meyers, Scott. More Effective C++: 35 New Ways to Improve Your Programs and Designs. Addison-Wesley, Reading, Mass., 1996. ISBN-10: 020163371X. ISBN-13: 9780201633719.

Attachments

iterator-fix.patch (10.2 KB) - added by Echelon9 17 months ago.
Proposed fix for STL iterators prefix/suffix increment
t1064-additional-profile-iterator-fixes.patch (781 bytes) - added by Echelon9 17 months ago.
Additional two cases missed

Change History

Changed 17 months ago by Echelon9

Proposed fix for STL iterators prefix/suffix increment

comment:1 Changed 17 months ago by feneur

  • Milestone changed from Alpha 8 to Alpha 9

comment:2 Changed 17 months ago by Philip

I don't think performance is a good reason for this specific patch, since the affected functions are far from performance bottlenecks and since an optimising compiler can easily figure out how to do std::vector::iterator++ efficiently. But stylistic consistency is nice, and we do ++it much more commonly than it++, so I think the patch is worthwhile for improving consistency.

comment:3 Changed 17 months ago by philip

  • Status changed from new to closed
  • Resolution set to fixed

(In [10822]) Fix #1064 (use ++it instead of it++ for consistency), based on patch by Echelon9

Changed 17 months ago by Echelon9

Additional two cases missed

comment:4 Changed 17 months ago by philip

(In [10860]) Fix #1064 more

Note: See TracTickets for help on using tickets.