[patch] No allocation for empty unordered containers

François Dumont frs.dumont@gmail.com
Tue Aug 12 19:22:00 GMT 2014


On 05/08/2014 22:23, Paolo Carlini wrote:
> Hi,
>
> On 08/05/2014 10:10 PM, Jonathan Wakely wrote:
>> It doesn't have to be noexcept, but IMHO there is no point changing
>> the containers to avoid allocation unless that allows us to mark it
>> noexcept.  If it can throw anyway, we might as well allocate the
>> initial buckets.
> I have been following this discussion on and off. In general, I must 
> say that AFAIK a default constructor which doesn't allocate memory is 
> normally considered superior from the QoI point of view, even if isn't 
> noexcept. As probably I have already mentioned, a "well known" C++ 
> library implementer used to repeat it all the time and used also to 
> say that our std::list implementation was very good exactly because of 
> that feature, *well* before the invention of noexcept. That said, 
> marking the constructor noexcept is of course the obvious next step, I 
> don't have much to say about the general development plans we have got 
> here.
>
> Paolo.
>
Hi

     Based on your feedbacks I think we should stay with just targeting 
good QoI by not allocating on default construction. As I said the 
noexcept qualification would need to not conform strictly to the Standard.

     So here is the patch again even if a little bit simplified. 
Regarding the doc I try to put information in Doxygen comments this way 
we do not need to maintain another page for that.

2014-08-12  François Dumont <fdumont@gcc.gnu.org>

     * include/bits/hashtable.h: Make use of the internal single bucket to
     limit allocation as long as container remains empty.
     * include/bits/unordered_map.h: Set default bucket hint to 0 in
     constructors to avoid allocation.
     * include/bits/unordered_set.h: Likewise.
     * include/debug/unordered_map: Likewise.
     * include/debug/unordered_set: Likewise.
     * src/c++11/hashtable_c++0x.cc (_Prime_rehash_policy::_M_next_bkt):
     Returns 1 for hint 0.
     * testsuite/23_containers/unordered_map/allocator/
     empty_instantiation.cc:    New.
     * testsuite/23_containers/unordered_multimap/allocator/
     empty_instantiation.cc:    New.
     * testsuite/23_containers/unordered_set/allocator/
     empty_instantiation.cc: New.
     * testsuite/23_containers/unordered_multiset/allocator/
     empty_instantiation.cc: New.

Tested under Linux x86_64.

François

-------------- next part --------------
A non-text attachment was scrubbed...
Name: hashtable.patch
Type: text/x-patch
Size: 19231 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20140812/74379326/attachment.bin>


More information about the Libstdc++ mailing list