include/rtl/string.hxx  |   13 ++++++++++++-
 include/rtl/ustring.hxx |   13 ++++++++++++-
 2 files changed, 24 insertions(+), 2 deletions(-)

New commits:
commit db04b3e154a1fb8f222232ef969bb3617e051329
Author:     Noel Grandin <[email protected]>
AuthorDate: Sat Aug 20 10:57:19 2022 +0200
Commit:     Noel Grandin <[email protected]>
CommitDate: Mon Aug 22 09:35:31 2022 +0200

    return 64-bit hash for O[U]String
    
    which gives better performance when putting strings into hashed
    containers that expect good key distribution.
    This hash implementation is the same one that Java uses.
    
    Change-Id: Iae5cf3cd27309856acfa51781295f2e56c8e77db
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138574
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <[email protected]>

diff --git a/include/rtl/string.hxx b/include/rtl/string.hxx
index 0e3dad6926e8..837fa89bd2b4 100644
--- a/include/rtl/string.hxx
+++ b/include/rtl/string.hxx
@@ -2301,7 +2301,18 @@ template<>
 struct hash<::rtl::OString>
 {
     std::size_t operator()(::rtl::OString const & s) const
-    { return std::size_t(s.hashCode()); }
+    {
+        if constexpr (sizeof(std::size_t) == 8)
+        {
+            // return a hash that uses the full 64-bit range instead of a 
32-bit value
+            size_t n = 0;
+            for (sal_Int32 i = 0, len = s.getLength(); i < len; ++i)
+                n = 31 * n + s[i];
+            return n;
+        }
+        else
+            return std::size_t(s.hashCode());
+    }
 };
 
 }
diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx
index e6d3ed682932..79ad75d8304d 100644
--- a/include/rtl/ustring.hxx
+++ b/include/rtl/ustring.hxx
@@ -3547,7 +3547,18 @@ template<>
 struct hash<::rtl::OUString>
 {
     std::size_t operator()(::rtl::OUString const & s) const
-    { return std::size_t(s.hashCode()); }
+    {
+        if constexpr (sizeof(std::size_t) == 8)
+        {
+            // return a hash that uses the full 64-bit range instead of a 
32-bit value
+            size_t n = 0;
+            for (sal_Int32 i = 0, len = s.getLength(); i < len; ++i)
+                n = 31 * n + s[i];
+            return n;
+        }
+        else
+            return std::size_t(s.hashCode());
+    }
 };
 
 }

Reply via email to