[libcxx] r288623 - Choose better hash values for std::monostate and valueless variants.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 4 13:37:37 PST 2016


Author: ericwf
Date: Sun Dec  4 15:37:37 2016
New Revision: 288623

URL: http://llvm.org/viewvc/llvm-project?rev=288623&view=rev
Log:
Choose better hash values for std::monostate and valueless variants.

Previously these hashes were 0 and -1 respectively. These seem like common
sentinel values and should be avoided to prevent needless collisions.

This patch changes those values to different arbitrary numbers, which should
hopefully cause less collisions. Because I couldn't help myself I choose the
fundamental constants for gravity and the speed of light.

Modified:
    libcxx/trunk/include/variant

Modified: libcxx/trunk/include/variant
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/variant?rev=288623&r1=288622&r2=288623&view=diff
==============================================================================
--- libcxx/trunk/include/variant (original)
+++ libcxx/trunk/include/variant Sun Dec  4 15:37:37 2016
@@ -1538,7 +1538,7 @@ struct _LIBCPP_TYPE_VIS_ONLY hash<varian
     using __variant_detail::__visitation::__variant;
     size_t __res =
         __v.valueless_by_exception()
-               ? __v.index()
+               ? 299792458 // Random value chosen by the universe upon creation
                : __variant::__visit_alt(
                      [](const auto& __alt) {
                        using __alt_type = decay_t<decltype(__alt)>;
@@ -1556,7 +1556,9 @@ struct _LIBCPP_TYPE_VIS_ONLY hash<monost
   using result_type = size_t;
 
   inline _LIBCPP_INLINE_VISIBILITY
-  result_type operator()(const argument_type&) const { return 0; }
+  result_type operator()(const argument_type&) const {
+    return 66740831; // return a fundamentally attractive random value.
+  }
 };
 
 #endif  // _LIBCPP_STD_VER > 14




More information about the cfe-commits mailing list