From f94c61b3b0850b8c4902f2e40f5f8b2b029b6829 Mon Sep 17 00:00:00 2001 From: gopinath-vasalamarri Date: Fri, 13 Dec 2024 22:56:04 -0500 Subject: [PATCH] Update BloomFilterStrategies.java Remove imul instruction from murmur operations of 32bit hash functions -- reduces the number of instructions by 1 CPU cycle. Before -- https://godbolt.org/z/GWvnEoPa3 After -- https://godbolt.org/z/4zEoM5G1W --- .../common/hash/BloomFilterStrategies.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/guava/src/com/google/common/hash/BloomFilterStrategies.java b/guava/src/com/google/common/hash/BloomFilterStrategies.java index 7c5650b2d531..c2ecb0c278d5 100644 --- a/guava/src/com/google/common/hash/BloomFilterStrategies.java +++ b/guava/src/com/google/common/hash/BloomFilterStrategies.java @@ -57,13 +57,13 @@ enum BloomFilterStrategies implements BloomFilter.Strategy { int hash2 = (int) (hash64 >>> 32); boolean bitsChanged = false; + int signedCombinedHash = 0; + int combinedHash = hash1; for (int i = 1; i <= numHashFunctions; i++) { - int combinedHash = hash1 + (i * hash2); + combinedHash += hash2; // Flip all the bits if it's negative (guaranteed positive number) - if (combinedHash < 0) { - combinedHash = ~combinedHash; - } - bitsChanged |= bits.set(combinedHash % bitSize); + signedCombinedHash = (combinedHash < 0) ? ~combinedHash : combinedHash; + bitsChanged |= bits.set(signedCombinedHash % bitSize); } return bitsChanged; } @@ -79,13 +79,13 @@ enum BloomFilterStrategies implements BloomFilter.Strategy { int hash1 = (int) hash64; int hash2 = (int) (hash64 >>> 32); + int signedCombinedHash = 0; + int combinedHash = hash1; for (int i = 1; i <= numHashFunctions; i++) { - int combinedHash = hash1 + (i * hash2); + combinedHash += hash2; // Flip all the bits if it's negative (guaranteed positive number) - if (combinedHash < 0) { - combinedHash = ~combinedHash; - } - if (!bits.get(combinedHash % bitSize)) { + signedCombinedHash = (combinedHash < 0) ? ~combinedHash : combinedHash; + if (!bits.get(signedCombinedHash % bitSize)) { return false; } }