diff mbox

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

Message ID 1476352715-110467-5-git-send-email-bhanuprakash.bodireddy@intel.com
State Superseded
Delegated to: Daniele Di Proietto
Headers show

Commit Message

Bodireddy, Bhanuprakash Oct. 13, 2016, 9:58 a.m. UTC
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@intel.com>
Co-authored-by: Antonio Fischetti <antonio.fischetti@intel.com>
---
 lib/hash.h | 5 +++++
 1 file changed, 5 insertions(+)
diff mbox

Patch

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;