{{ message }}
Fix potential quadratic complexity caused by reserve(size + 1) - #188
Merged
Conversation
Owner
martinus
added a commit
that referenced
this pull request
Aug 10, 2026
PR #188 fixed segmented_vector growing its block pointer array with reserve(size + 1), which reallocated it for every new segment and made a chain of inserts quadratic in the number of segments. Count the allocations for a 256-segment fill: geometric growth needs one allocation per segment plus a logarithmic handful for the index, while the quadratic version lands near three per segment. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Bug
I believe
v4.9.0introduced a performance regression while aiming for exception safety. The code introduced:which leads to m_blocks reallocation on every insert in a chain of inserts with libstdc++ and others, which is quadratic.
Sample program
The following program demonstrates this:
when running on
mainI get the following output on machine:when running with the fix:
Alternative fixes
Another option could be to remove the
reserveall-together and wrap thepush_backin a try-catch block, like:pros:
std::vectorimplementation handle the scaling factor (in the PR diff, we hardcode 2x)cons: