Search

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

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)
0
User(s) can reproduce this bug
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 (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling version

Visual Studio 2010 SP1

Steps to reproduce

Inspect code of std::vector::_Insert_n().

Product Language

English

Operating System

Windows XP

Operating System Language

English

Actual results

"if (max_size() - size() < _Count)" check is done even no reallocation follows.

Expected results

"if (max_size() - size() < _Count)" check should only be done if reallocation follows, otherwise it is redundant and should be omitted.
File Attachments
0 attachments
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.