Opened 12 years ago

Closed 12 years ago

Last modified 8 years ago

#1064 closed enhancement (fixed)

[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: STL
Cc: Patch:

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 (2)

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

Download all attachments as: .zip

Change History (8)

by Echelon9, 12 years ago

Attachment: iterator-fix.patch added

Proposed fix for STL iterators prefix/suffix increment

comment:1 by Erik Johansson, 12 years ago

Milestone: Alpha 8Alpha 9

comment:2 by Philip Taylor, 12 years ago

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 by philip, 12 years ago

Resolution: fixed
Status: newclosed

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

by Echelon9, 12 years ago

Additional two cases missed

comment:4 by philip, 12 years ago

(In [10860]) Fix #1064 more

comment:5 by ben, 11 years ago

In 13418:

Use prefix increment operator for iterators (for consistency), refs #1852, #1064

comment:6 by sanderd17, 8 years ago

Keywords: review removed
Note: See TracTickets for help on using tickets.