Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deque's usage of _Allocate_at_least_helper #4017

Merged

Conversation

achabense
Copy link
Contributor

deque::_Mapsize() should always be power of 2. Therefore, in deque::_Growmap, _Allocate_at_least_helper should not have chance to change _Newsize directly.

@achabense achabense requested a review from a team as a code owner September 8, 2023 16:08
@github-actions github-actions bot added this to Initial Review in Code Reviews Sep 8, 2023
@@ -51,7 +51,7 @@ struct signalling_allocator {

allocation_result<T*> allocate_at_least(size_t count) {
allocate_at_least_signal.set();
return {allocate(count * 2), count * 2};
return {allocate(count * 2 + 1), count * 2 + 1};
Copy link
Contributor Author

@achabense achabense Sep 8, 2023

Choose a reason for hiding this comment

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

count * 2 just happened to work; the deque will break easily if users try to provide their own allocate_at_least.

stl/inc/deque Outdated
Comment on lines 1566 to 1568
while (_Allocsize / 2 >= _Newsize) {
_Newsize *= 2;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems inefficient to try and get the biggest power of two less than _Allocsize (I really wish we had access to std::bit_floor(...) here)

Copy link
Member

Choose a reason for hiding this comment

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

Do we have an _Ugly (i.e., usable in C++14 mode) version of bit_floor?

Copy link
Contributor Author

@achabense achabense Sep 8, 2023

Choose a reason for hiding this comment

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

I think it's ok to (temporarily) keep this while loop, as it's highly likely that _Allocsize is just equal to or marginally larger than _Newsize, so the loop will skip immediatly or at least very short. (And I'm going to do a series of cleanups for <deque> recently :) we can also consider connverting another while loop in the function to a bit_ceil too )

Copy link
Contributor Author

@achabense achabense Sep 10, 2023

Choose a reason for hiding this comment

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

I made a tiny cleanup & optimization for its above loop: 850d8ae; I think this can partly compensate for (or overcome) the cost of new loop. Can I make a push to this pr?
godbolt result; the codegen looks really better (slighty fewer lines; tighter loop; fewer jmp)

------update------
I'm having difficulty deciding whether we can safely assume _Minimum_map_size will not cause overflow against (_Alty::)max_size()/_Block_size. And worse, that comparision looks just wrong to me. I'm doubting we should compare with _Al[p]ty::max_size instead.
Due to these problems I'd suggest putting off the commit to future prs. (And in that commit link I've added some more detailed comments about the above problems.)

@CaseyCarter CaseyCarter added the bug Something isn't working label Sep 8, 2023
@CaseyCarter
Copy link
Member

Thanks much for separating the bugfix from the cleanup!

@StephanTLavavej StephanTLavavej self-assigned this Sep 8, 2023
@StephanTLavavej StephanTLavavej moved this from Initial Review to Final Review in Code Reviews Sep 15, 2023
tests/std/tests/GH_003570_allocate_at_least/test.cpp Outdated Show resolved Hide resolved
stl/inc/deque Outdated Show resolved Hide resolved
@StephanTLavavej
Copy link
Member

@strega-nil-ms I pushed minor stylistic changes after you approved.

@StephanTLavavej StephanTLavavej removed their assignment Sep 15, 2023
@StephanTLavavej StephanTLavavej moved this from Final Review to Ready To Merge in Code Reviews Sep 15, 2023
_Mapptr _Myptr = _Newmap + _Myboff;
_STL_ASSERT(_Allocsize >= _Newsize, "_Allocsize >= _Newsize");
Copy link
Contributor Author

@achabense achabense Sep 16, 2023

Choose a reason for hiding this comment

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

I think we should put debug check in _Allocate_at_least_helper instead. (And is _STL_ASSERT a suitable choice for such check?)

@StephanTLavavej
Copy link
Member

I think we should put debug check in _Allocate_at_least_helper instead.

That would be reasonable.

(And is _STL_ASSERT a suitable choice for such check?)

Yeah, for users violating preconditions.

@achabense
Copy link
Contributor Author

achabense commented Sep 19, 2023

Thanks for the confirmation!
As to adding debug check for _Allocate_at_least_helper, I think we can make a good-first-issue around this.
For now I think we can just remove that added _STL_ASSERT, as it is not a necessary part of the fix, and should get removed finally.

@StephanTLavavej StephanTLavavej self-assigned this Sep 20, 2023
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 2ae7622 into microsoft:main Sep 21, 2023
35 checks passed
Code Reviews automation moved this from Ready To Merge to Done Sep 21, 2023
@StephanTLavavej
Copy link
Member

Thanks for noticing and fixing this regression before anyone else did! 🦅 👁️ 🐞

@achabense achabense deleted the _Deque_allocate_at_least_fix branch September 22, 2023 04:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants