Home Dashboard Directory Help
Search

std::vector::_Insert_n() contains a suboptimal check against max_size by Dmitry Me


Status: 

Closed
 as Fixed Help for as Fixed


1
0
Sign in
to vote
Type: Bug
ID: 694887
Opened: 10/14/2011 12:14:52 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

As of version 10.0.40219.1 SP1Rel std::vector::_Insert_n() code goes like this:

if (_Count == 0)
;
else if (max_size() - size() < _Count)
_Xlen();    // result too long
else if (capacity() < size() + _Count)
//reallocate
else //skipped

Now capacity() <= max_size() at all times. Which means that if capacity() >= size() + _Count (enough room) then automatically max_size() >= size() + _Count. Which means that whenever that code is invoked when a vector already has room (that's quite often) the check max_size() >= size() + _Count is redundant.

_Insert_n() should be redesigned like this:

if (_Count == 0)
;
else if(capacity() - size() < _Count )
{
if( (max_size() - _Size < _Count)
     _Xlen();
//reallocate
} else //as previously

so that the check is done only if there's a need for reallocation in the first place.
Details
Sign in to post a comment.
Posted by Microsoft on 11/4/2011 at 9:14 PM
Hi,

Thanks for suggesting this micro-optimization. We've implemented it, "flipping" the _Xlen() check, and it will be available in VC11.

All of _Insert(), _Insert_n(), _Reserve(), and reserve() have been changed. I hope you don't mind if I copy and paste this response for your four bugs - in the future, please consider filing a single bug when a single issue appears in multiple places.

If you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by MS-Moderator10 [Feedback Moderator] on 10/14/2011 at 2:29 AM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by MS-Moderator01 on 10/14/2011 at 12:45 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.