Skip to content

Conversation

@lhecker
Copy link
Member

@lhecker lhecker commented Jun 7, 2023

This commit achieves fixes the issue as described in the title by
checking whether the this and other pointer are identical.
As an added bonus it makes the copy and move constructors slightly
cheaper, as they don't try to destruct existing data anymore,
which doesn't exist anyways.

Validation Steps Performed

  • It blends ✅

@lhecker lhecker added Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-3 A description (P3) labels Jun 7, 2023
Comment on lines +899 to +900
size_t _capacity;
size_t _size;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By not initializing these fields, the move constructor avoids writing to these members twice.

Comment on lines -367 to -370
reserve(other._size);

std::uninitialized_copy(other.begin(), other.end(), _uninitialized_begin());
_size = other._size;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 3 lines are now moved 1:1 into _copy_assign. This allows us to call _copy_assign without a prior clear in the constructor, because a newly constructed vector can't possibly have anything that needs to be cleared.

This makes the diff large, but actually barely anything changed.

Comment on lines -389 to -409
if (other._capacity == N)
{
_data = &_buffer[0];
_capacity = N;
_size = other._size;
// The earlier static_assert(std::is_nothrow_move_constructible_v<T>)
// ensures that we don't exit in a weird state with invalid `_size`.
#pragma warning(suppress : 26447) // The function is declared 'noexcept' but calls function '...' which may throw exceptions (f.6).
std::uninitialized_move(other.begin(), other.end(), _uninitialized_begin());
std::destroy(other.begin(), other.end());
}
else
{
_data = other._data;
_capacity = other._capacity;
_size = other._size;
_move_assign(other);
}

other._data = &other._buffer[0];
other._capacity = N;
other._size = 0;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 21 lines are moved 1:1 into _move_assign, for the same reason as for _copy_assign.

Copy link
Member

@carlos-zamora carlos-zamora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty straightforward. Thanks for writing some comments in the PR! Made it much easier to review!

@carlos-zamora carlos-zamora added the AutoMerge Marked for automatic merge by the bot when requirements are met label Jun 9, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot deleted the dev/lhecker/small-vector-fixup branch June 9, 2023 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-3 A description (P3)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants