[libcxx-commits] [libcxx] r367605 - Change default bucket count in hash_set/hash_map.

Eric Fiselier via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 1 12:48:29 PDT 2019


Author: ericwf
Date: Thu Aug  1 12:48:29 2019
New Revision: 367605

URL: http://llvm.org/viewvc/llvm-project?rev=367605&view=rev
Log:
Change default bucket count in hash_set/hash_map.

Previously these types rehashed to a table of 193 elements
upon construction. But this is non-ideal, first because default
constructors should not allocate unless necessary, and second
because 193 is big and can waste a bunch of memory.

This number had previously been chosen to match GCC's implementation.

Added:
    libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map_name_lookup.pass.cpp
      - copied, changed from r367356, libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map.pass.cpp
    libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set_name_lookup.pass.cpp
      - copied, changed from r367356, libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set.pass.cpp
Modified:
    libcxx/trunk/include/ext/hash_map
    libcxx/trunk/include/ext/hash_set
    libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map.pass.cpp
    libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set.pass.cpp

Modified: libcxx/trunk/include/ext/hash_map
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/ext/hash_map?rev=367605&r1=367604&r2=367605&view=diff
==============================================================================
--- libcxx/trunk/include/ext/hash_map (original)
+++ libcxx/trunk/include/ext/hash_map Thu Aug  1 12:48:29 2019
@@ -39,14 +39,17 @@ public:
     typedef /unspecified/ iterator;
     typedef /unspecified/ const_iterator;
 
-    explicit hash_map(size_type n = 193, const hasher& hf = hasher(),
+    hash_map();
+    explicit hash_map(size_type n, const hasher& hf = hasher(),
                            const key_equal& eql = key_equal(),
                            const allocator_type& a = allocator_type());
     template <class InputIterator>
-        hash_map(InputIterator f, InputIterator l,
-                      size_type n = 193, const hasher& hf = hasher(),
-                      const key_equal& eql = key_equal(),
-                      const allocator_type& a = allocator_type());
+    hash_map(InputIterator f, InputIterator l);
+    template <class InputIterator>
+    hash_map(InputIterator f, InputIterator l,
+                size_type n, const hasher& hf = hasher(),
+                const key_equal& eql = key_equal(),
+                const allocator_type& a = allocator_type());
     hash_map(const hash_map&);
     ~hash_map();
     hash_map& operator=(const hash_map&);
@@ -502,7 +505,7 @@ public:
     typedef __hash_map_iterator<typename __table::iterator>       iterator;
     typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
 
-    _LIBCPP_INLINE_VISIBILITY hash_map() {__table_.rehash(193);}
+    _LIBCPP_INLINE_VISIBILITY hash_map() { }
     explicit hash_map(size_type __n, const hasher& __hf = hasher(),
                            const key_equal& __eql = key_equal());
     hash_map(size_type __n, const hasher& __hf,
@@ -623,7 +626,6 @@ template <class _InputIterator>
 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
         _InputIterator __first, _InputIterator __last)
 {
-    __table_.rehash(193);
     insert(__first, __last);
 }
 
@@ -775,7 +777,7 @@ public:
     typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
 
     _LIBCPP_INLINE_VISIBILITY
-    hash_multimap() {__table_.rehash(193);}
+    hash_multimap() { }
     explicit hash_multimap(size_type __n, const hasher& __hf = hasher(),
                                 const key_equal& __eql = key_equal());
     hash_multimap(size_type __n, const hasher& __hf,
@@ -890,7 +892,6 @@ template <class _InputIterator>
 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
         _InputIterator __first, _InputIterator __last)
 {
-    __table_.rehash(193);
     insert(__first, __last);
 }
 

Modified: libcxx/trunk/include/ext/hash_set
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/ext/hash_set?rev=367605&r1=367604&r2=367605&view=diff
==============================================================================
--- libcxx/trunk/include/ext/hash_set (original)
+++ libcxx/trunk/include/ext/hash_set Thu Aug  1 12:48:29 2019
@@ -237,7 +237,7 @@ public:
     typedef typename __table::const_iterator       const_iterator;
 
     _LIBCPP_INLINE_VISIBILITY
-    hash_set() {__table_.rehash(193);}
+    hash_set() { }
     explicit hash_set(size_type __n, const hasher& __hf = hasher(),
                            const key_equal& __eql = key_equal());
     hash_set(size_type __n, const hasher& __hf, const key_equal& __eql,
@@ -347,7 +347,6 @@ template <class _InputIterator>
 hash_set<_Value, _Hash, _Pred, _Alloc>::hash_set(
         _InputIterator __first, _InputIterator __last)
 {
-    __table_.rehash(193);
     insert(__first, __last);
 }
 
@@ -459,7 +458,7 @@ public:
     typedef typename __table::const_iterator       const_iterator;
 
     _LIBCPP_INLINE_VISIBILITY
-    hash_multiset() {__table_.rehash(193);}
+    hash_multiset() { }
     explicit hash_multiset(size_type __n, const hasher& __hf = hasher(),
                                 const key_equal& __eql = key_equal());
     hash_multiset(size_type __n, const hasher& __hf,
@@ -569,7 +568,6 @@ template <class _InputIterator>
 hash_multiset<_Value, _Hash, _Pred, _Alloc>::hash_multiset(
         _InputIterator __first, _InputIterator __last)
 {
-    __table_.rehash(193);
     insert(__first, __last);
 }
 

Modified: libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map.pass.cpp?rev=367605&r1=367604&r2=367605&view=diff
==============================================================================
--- libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map.pass.cpp (original)
+++ libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map.pass.cpp Thu Aug  1 12:48:29 2019
@@ -6,31 +6,31 @@
 //
 //===----------------------------------------------------------------------===//
 
+
 // Prevent emission of the deprecated warning.
 #ifdef __clang__
 #pragma clang diagnostic ignored "-W#warnings"
 #endif
 
-// Poison the std:: names we might use inside __gnu_cxx to ensure they're
-// properly qualified.
-struct allocator;
-struct pair;
-struct equal_to;
-struct unique_ptr;
 #include <ext/hash_map>
+#include <cassert>
 
 #include "test_macros.h"
+#include "count_new.hpp"
 
-
-namespace __gnu_cxx {
-template class hash_map<int, int>;
+void test_default_does_not_allocate() {
+  DisableAllocationGuard g;
+  ((void)g);
+  {
+    __gnu_cxx::hash_map<int, int> h;
+    assert(h.bucket_count() == 0);
+  }
+  {
+    __gnu_cxx::hash_multimap<int, int> h;
+    assert(h.bucket_count() == 0);
+  }
 }
 
 int main(int, char**) {
-  typedef __gnu_cxx::hash_map<int, int> Map;
-  Map m;
-  Map m2(m);
-  ((void)m2);
-
-  return 0;
+  test_default_does_not_allocate();
 }

Copied: libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map_name_lookup.pass.cpp (from r367356, libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map.pass.cpp)
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map_name_lookup.pass.cpp?p2=libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map_name_lookup.pass.cpp&p1=libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_map.pass.cpp&r1=367356&r2=367605&rev=367605&view=diff
==============================================================================
    (empty)

Modified: libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set.pass.cpp?rev=367605&r1=367604&r2=367605&view=diff
==============================================================================
--- libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set.pass.cpp (original)
+++ libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set.pass.cpp Thu Aug  1 12:48:29 2019
@@ -6,29 +6,31 @@
 //
 //===----------------------------------------------------------------------===//
 
+
 // Prevent emission of the deprecated warning.
 #ifdef __clang__
 #pragma clang diagnostic ignored "-W#warnings"
 #endif
-// Poison the std:: names we might use inside __gnu_cxx to ensure they're
-// properly qualified.
-struct allocator;
-struct pair;
-struct equal_to;
-struct unique_ptr;
+
 #include <ext/hash_set>
+#include <cassert>
 
 #include "test_macros.h"
+#include "count_new.hpp"
 
-namespace __gnu_cxx {
-template class hash_set<int>;
+void test_default_does_not_allocate() {
+  DisableAllocationGuard g;
+  ((void)g);
+  {
+    __gnu_cxx::hash_set<int> h;
+    assert(h.bucket_count() == 0);
+  }
+  {
+    __gnu_cxx::hash_multiset<int> h;
+    assert(h.bucket_count() == 0);
+  }
 }
 
 int main(int, char**) {
-  typedef __gnu_cxx::hash_set<int> Set;
-  Set s;
-  Set s2(s);
-  ((void)s2);
-
-  return 0;
+  test_default_does_not_allocate();
 }

Copied: libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set_name_lookup.pass.cpp (from r367356, libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set.pass.cpp)
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set_name_lookup.pass.cpp?p2=libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set_name_lookup.pass.cpp&p1=libcxx/trunk/test/libcxx/containers/gnu_cxx/hash_set.pass.cpp&r1=367356&r2=367605&rev=367605&view=diff
==============================================================================
    (empty)




More information about the libcxx-commits mailing list