* [PATCH] Avoid allocator operator== in _Safe_container
@ 2021-08-09 10:23 François Dumont
2021-08-09 10:33 ` Jonathan Wakely
0 siblings, 1 reply; 7+ messages in thread
From: François Dumont @ 2021-08-09 10:23 UTC (permalink / raw)
To: libstdc++; +Cc: gcc-patches
[-- Attachment #1: Type: text/plain, Size: 908 bytes --]
Some newly introduced tests in
23_containers/unordered_map/cons/default.cc revealed that we are forcing
the allocator type to have a operator==.
It can be avoided by checking allocator_traits::is_always_equal.
libstdc++: [_GLIBCXX_DEBUG] Avoid allocator operator== when always
equal
Use std::allocator_traits::is_always_equal to find out if we need
to compare
allocator instances on safe container allocator aware move constructor.
libstdc++-v3/ChangeLog:
* include/debug/safe_container.h
(_Safe_container(_Safe_container&&, const _Alloc&,
std::true_type)): New.
(_Safe_container(_Safe_container&&, const _Alloc&,
std::false_type)): New.
(_Safe_container(_Safe_container&&, const _Alloc&)): Use
latters.
Tested under Linux x86 Debug mode.
Ok to commit ?
François
[-- Attachment #2: safe_container.patch --]
[-- Type: text/x-patch, Size: 1095 bytes --]
diff --git a/libstdc++-v3/include/debug/safe_container.h b/libstdc++-v3/include/debug/safe_container.h
index d9636c29e9b..b06c33dac7e 100644
--- a/libstdc++-v3/include/debug/safe_container.h
+++ b/libstdc++-v3/include/debug/safe_container.h
@@ -57,7 +57,12 @@ namespace __gnu_debug
_Safe_container(const _Safe_container&) = default;
_Safe_container(_Safe_container&&) = default;
- _Safe_container(_Safe_container&& __x, const _Alloc& __a)
+ private:
+ _Safe_container(_Safe_container&& __x, const _Alloc& __a, std::true_type)
+ : _Safe_container()
+ { _Base::_M_swap(__x); }
+
+ _Safe_container(_Safe_container&& __x, const _Alloc& __a, std::false_type)
: _Safe_container()
{
if (__x._M_cont().get_allocator() == __a)
@@ -65,6 +70,12 @@ namespace __gnu_debug
else
__x._M_invalidate_all();
}
+
+ protected:
+ _Safe_container(_Safe_container&& __x, const _Alloc& __a)
+ : _Safe_container(std::move(__x), __a,
+ typename std::allocator_traits<_Alloc>::is_always_equal{})
+ { }
#endif
public:
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Avoid allocator operator== in _Safe_container
2021-08-09 10:23 [PATCH] Avoid allocator operator== in _Safe_container François Dumont
@ 2021-08-09 10:33 ` Jonathan Wakely
2021-08-09 10:34 ` Jonathan Wakely
0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2021-08-09 10:33 UTC (permalink / raw)
To: François Dumont; +Cc: libstdc++, gcc-patches
On Mon, 9 Aug 2021 at 11:26, François Dumont via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> Some newly introduced tests in
> 23_containers/unordered_map/cons/default.cc revealed that we are forcing
> the allocator type to have a operator==.
All allocators are required to have operator== so that should not be a
problem. What is the error?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Avoid allocator operator== in _Safe_container
2021-08-09 10:33 ` Jonathan Wakely
@ 2021-08-09 10:34 ` Jonathan Wakely
2021-08-09 10:45 ` Jonathan Wakely
0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2021-08-09 10:34 UTC (permalink / raw)
To: François Dumont; +Cc: libstdc++, gcc-patches
On Mon, 9 Aug 2021 at 11:33, Jonathan Wakely wrote:
>
> On Mon, 9 Aug 2021 at 11:26, François Dumont via Libstdc++
> <libstdc++@gcc.gnu.org> wrote:
> >
> > Some newly introduced tests in
> > 23_containers/unordered_map/cons/default.cc revealed that we are forcing
> > the allocator type to have a operator==.
>
> All allocators are required to have operator== so that should not be a
> problem. What is the error?
OK, I see it. I just forgot to define operator== and operator!= for
the custom allocator in that new test, and that should be added.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Avoid allocator operator== in _Safe_container
2021-08-09 10:34 ` Jonathan Wakely
@ 2021-08-09 10:45 ` Jonathan Wakely
2021-08-09 11:52 ` François Dumont
0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2021-08-09 10:45 UTC (permalink / raw)
To: François Dumont; +Cc: libstdc++, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 735 bytes --]
On Mon, 9 Aug 2021 at 11:34, Jonathan Wakely wrote:
>
> On Mon, 9 Aug 2021 at 11:33, Jonathan Wakely wrote:
> >
> > On Mon, 9 Aug 2021 at 11:26, François Dumont via Libstdc++
> > <libstdc++@gcc.gnu.org> wrote:
> > >
> > > Some newly introduced tests in
> > > 23_containers/unordered_map/cons/default.cc revealed that we are forcing
> > > the allocator type to have a operator==.
> >
> > All allocators are required to have operator== so that should not be a
> > problem. What is the error?
>
> OK, I see it. I just forgot to define operator== and operator!= for
> the custom allocator in that new test, and that should be added.
Fixed like this instead. Tested x86_64-linux with -D_GLIBCXX_DEBUG.
Pushed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1827 bytes --]
commit 2eff2a3cb521c58212885a3dca638764285b5691
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Mon Aug 9 11:36:07 2021
libstdc++: Make allocator equality comparable in tests
libstdc++-v3/ChangeLog:
* testsuite/23_containers/unordered_map/cons/default.cc: Add
equality comparison operators to allocator.
* testsuite/23_containers/unordered_set/cons/default.cc:
Likewise.
diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/default.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/default.cc
index d64d078a7da..7a785e980b1 100644
--- a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/default.cc
+++ b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/default.cc
@@ -18,6 +18,9 @@ template<typename T>
void deallocate(T *p, std::size_t n)
{ std::allocator<T>().deallocate(p, n); }
+
+ bool operator==(const NoDefaultConsAlloc&) const { return true; }
+ bool operator!=(const NoDefaultConsAlloc&) const { return false; }
};
using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>,
diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/cons/default.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/default.cc
index 41281d3d774..fb87c96ce9d 100644
--- a/libstdc++-v3/testsuite/23_containers/unordered_set/cons/default.cc
+++ b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/default.cc
@@ -18,6 +18,9 @@ template<typename T>
void deallocate(T *p, std::size_t n)
{ std::allocator<T>().deallocate(p, n); }
+
+ bool operator==(const NoDefaultConsAlloc&) const { return true; }
+ bool operator!=(const NoDefaultConsAlloc&) const { return false; }
};
using Set = std::unordered_set<int, std::hash<int>, std::equal_to<int>,
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Avoid allocator operator== in _Safe_container
2021-08-09 10:45 ` Jonathan Wakely
@ 2021-08-09 11:52 ` François Dumont
2021-08-09 12:08 ` François Dumont
0 siblings, 1 reply; 7+ messages in thread
From: François Dumont @ 2021-08-09 11:52 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: libstdc++, gcc-patches
On 09/08/21 12:45 pm, Jonathan Wakely wrote:
> On Mon, 9 Aug 2021 at 11:34, Jonathan Wakely wrote:
>> On Mon, 9 Aug 2021 at 11:33, Jonathan Wakely wrote:
>>> On Mon, 9 Aug 2021 at 11:26, François Dumont via Libstdc++
>>> <libstdc++@gcc.gnu.org> wrote:
>>>> Some newly introduced tests in
>>>> 23_containers/unordered_map/cons/default.cc revealed that we are forcing
>>>> the allocator type to have a operator==.
>>> All allocators are required to have operator== so that should not be a
>>> problem. What is the error?
>> OK, I see it. I just forgot to define operator== and operator!= for
>> the custom allocator in that new test, and that should be added.
> Fixed like this instead. Tested x86_64-linux with -D_GLIBCXX_DEBUG.
> Pushed to trunk.
Ok, I thought my change was better because we have many allocator types
in tests without operator ==/!= (see
23_containers/*/cons/noexcept_default_construct.cc). But of course the
tests are not making any use of it for the moment, no big deal.
So this patch is just an optimization, may I still commit it ? Unless
you like the fact that Debug mode is checking that those operators are
provided when allocator-aware move constructor is being used.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Avoid allocator operator== in _Safe_container
2021-08-09 11:52 ` François Dumont
@ 2021-08-09 12:08 ` François Dumont
2021-08-09 14:24 ` Jonathan Wakely
0 siblings, 1 reply; 7+ messages in thread
From: François Dumont @ 2021-08-09 12:08 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: libstdc++, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 1393 bytes --]
On 09/08/21 1:52 pm, François Dumont wrote:
> On 09/08/21 12:45 pm, Jonathan Wakely wrote:
>> On Mon, 9 Aug 2021 at 11:34, Jonathan Wakely wrote:
>>> On Mon, 9 Aug 2021 at 11:33, Jonathan Wakely wrote:
>>>> On Mon, 9 Aug 2021 at 11:26, François Dumont via Libstdc++
>>>> <libstdc++@gcc.gnu.org> wrote:
>>>>> Some newly introduced tests in
>>>>> 23_containers/unordered_map/cons/default.cc revealed that we are
>>>>> forcing
>>>>> the allocator type to have a operator==.
>>>> All allocators are required to have operator== so that should not be a
>>>> problem. What is the error?
>>> OK, I see it. I just forgot to define operator== and operator!= for
>>> the custom allocator in that new test, and that should be added.
>> Fixed like this instead. Tested x86_64-linux with -D_GLIBCXX_DEBUG.
>> Pushed to trunk.
>
> Ok, I thought my change was better because we have many allocator
> types in tests without operator ==/!= (see
> 23_containers/*/cons/noexcept_default_construct.cc). But of course the
> tests are not making any use of it for the moment, no big deal.
>
> So this patch is just an optimization, may I still commit it ? Unless
> you like the fact that Debug mode is checking that those operators are
> provided when allocator-aware move constructor is being used.
>
>
A simpler version, using _Safe_container move constructor when allocator
instances are always equals.
[-- Attachment #2: safe_container.patch --]
[-- Type: text/x-patch, Size: 1084 bytes --]
diff --git a/libstdc++-v3/include/debug/safe_container.h b/libstdc++-v3/include/debug/safe_container.h
index d9636c29e9b..97c47167fe8 100644
--- a/libstdc++-v3/include/debug/safe_container.h
+++ b/libstdc++-v3/include/debug/safe_container.h
@@ -57,7 +57,12 @@ namespace __gnu_debug
_Safe_container(const _Safe_container&) = default;
_Safe_container(_Safe_container&&) = default;
- _Safe_container(_Safe_container&& __x, const _Alloc& __a)
+ private:
+ _Safe_container(_Safe_container&& __x, const _Alloc&, std::true_type)
+ : _Safe_container(std::move(__x))
+ { }
+
+ _Safe_container(_Safe_container&& __x, const _Alloc& __a, std::false_type)
: _Safe_container()
{
if (__x._M_cont().get_allocator() == __a)
@@ -65,6 +70,12 @@ namespace __gnu_debug
else
__x._M_invalidate_all();
}
+
+ protected:
+ _Safe_container(_Safe_container&& __x, const _Alloc& __a)
+ : _Safe_container(std::move(__x), __a,
+ typename std::allocator_traits<_Alloc>::is_always_equal{})
+ { }
#endif
public:
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Avoid allocator operator== in _Safe_container
2021-08-09 12:08 ` François Dumont
@ 2021-08-09 14:24 ` Jonathan Wakely
0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Wakely @ 2021-08-09 14:24 UTC (permalink / raw)
To: François Dumont; +Cc: libstdc++, gcc-patches
On Mon, 9 Aug 2021 at 13:08, François Dumont <frs.dumont@gmail.com> wrote:
>
> On 09/08/21 1:52 pm, François Dumont wrote:
> > On 09/08/21 12:45 pm, Jonathan Wakely wrote:
> >> On Mon, 9 Aug 2021 at 11:34, Jonathan Wakely wrote:
> >>> On Mon, 9 Aug 2021 at 11:33, Jonathan Wakely wrote:
> >>>> On Mon, 9 Aug 2021 at 11:26, François Dumont via Libstdc++
> >>>> <libstdc++@gcc.gnu.org> wrote:
> >>>>> Some newly introduced tests in
> >>>>> 23_containers/unordered_map/cons/default.cc revealed that we are
> >>>>> forcing
> >>>>> the allocator type to have a operator==.
> >>>> All allocators are required to have operator== so that should not be a
> >>>> problem. What is the error?
> >>> OK, I see it. I just forgot to define operator== and operator!= for
> >>> the custom allocator in that new test, and that should be added.
> >> Fixed like this instead. Tested x86_64-linux with -D_GLIBCXX_DEBUG.
> >> Pushed to trunk.
> >
> > Ok, I thought my change was better because we have many allocator
> > types in tests without operator ==/!= (see
> > 23_containers/*/cons/noexcept_default_construct.cc). But of course the
> > tests are not making any use of it for the moment, no big deal.
Those tests are all OK, the allocators inherit from std::allocator so
can use its equality operators.
But if I did mess up any more test allocators then we should fix those ones too.
> > So this patch is just an optimization, may I still commit it ? Unless
> > you like the fact that Debug mode is checking that those operators are
> > provided when allocator-aware move constructor is being used.
> >
> >
> A simpler version, using _Safe_container move constructor when allocator
> instances are always equals.
Yes, this optimization is good. OK for trunk, thanks!
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2021-08-09 14:24 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-09 10:23 [PATCH] Avoid allocator operator== in _Safe_container François Dumont
2021-08-09 10:33 ` Jonathan Wakely
2021-08-09 10:34 ` Jonathan Wakely
2021-08-09 10:45 ` Jonathan Wakely
2021-08-09 11:52 ` François Dumont
2021-08-09 12:08 ` François Dumont
2021-08-09 14:24 ` Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).