From 2048114512460faf04d24c1edb0925428fd4e0d4 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Tue, 9 Apr 2019 20:40:04 -0700 Subject: [PATCH] Reduce chance for wxPoint hash collision Hash collisions reduce the speed of unordered maps by increasing the comparison time of underlying values. This implements the inverse golden ration offset into the x value to ensure that consecutive points are far from each other in the map. --- common/common.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index d2dec02727..8f27b8032c 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -594,8 +594,12 @@ size_t std::hash::operator()( const wxString& s ) const #ifdef USE_KICAD_WXPOINT_LESS_AND_HASH size_t std::hash::operator() ( const wxPoint& k ) const { - return ( ( std::hash()( k.x ) - ^ ( std::hash()( k.y ) << 1 ) ) >> 1 ); + auto xhash = std::hash()( k.x ); + + // 0x9e3779b9 is 2^33 / ( 1 + sqrt(5) ) + // Adding this value ensures that consecutive bits of y will not be close to each other + // decreasing the likelihood of hash collision in similar values of x and y + return xhash ^ ( std::hash()( k.y ) + 0x9e3779b9 + ( xhash << 6 ) + ( xhash >> 2 ) ); } bool std::less::operator()( const wxPoint& aA, const wxPoint& aB ) const