[ovs-dev] [PATCH v3 04/12] hash: Skip Invoking mhash_add__() with zero input.

Bhanuprakash Bodireddy bhanuprakash.bodireddy at intel.com
Fri Oct 14 14:37:07 UTC 2016


mhash_add__() is expensive and should be only called with valid input.
zero-valued 'data' will not affect the 'hash' value and expensive hash
computation can be skipped when input is zero.

This patch will validate the input in mhash_add__ to save some cpu
cycles.

Signed-off-by: Bhanuprakash Bodireddy <bhanuprakash.bodireddy at intel.com>
Co-authored-by: Antonio Fischetti <antonio.fischetti at intel.com>
Signed-off-by: Antonio Fischetti <antonio.fischetti at intel.com>
---
 lib/hash.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/lib/hash.h b/lib/hash.h
index 114a419..f2dd510 100644
--- a/lib/hash.h
+++ b/lib/hash.h
@@ -62,6 +62,11 @@ static inline uint32_t hash_string(const char *, uint32_t basis);
 
 static inline uint32_t mhash_add__(uint32_t hash, uint32_t data)
 {
+    /* zero-valued 'data' will not change the 'hash' value */
+    if (!data) {
+        return hash;
+    }
+
     data *= 0xcc9e2d51;
     data = hash_rot(data, 15);
     data *= 0x1b873593;
-- 
2.4.11




More information about the dev mailing list