From 7736545f5bf036ed59b7274cd41e126abeaeba4b Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Thu, 27 Aug 2009 03:53:41 +0000
Subject: [PATCH] speed up data hashcodes

---
 core/java/src/net/i2p/data/Destination.java       |  6 ++++--
 core/java/src/net/i2p/data/Hash.java              | 10 ++++++++--
 core/java/src/net/i2p/data/LeaseSet.java          |  8 ++++----
 core/java/src/net/i2p/data/PrivateKey.java        | 10 ++++++++--
 core/java/src/net/i2p/data/PublicKey.java         | 10 ++++++++--
 core/java/src/net/i2p/data/RouterAddress.java     |  6 +++---
 core/java/src/net/i2p/data/RouterIdentity.java    |  8 +++++---
 core/java/src/net/i2p/data/SessionKey.java        | 10 ++++++++--
 core/java/src/net/i2p/data/Signature.java         | 10 ++++++++--
 core/java/src/net/i2p/data/SigningPrivateKey.java | 10 ++++++++--
 core/java/src/net/i2p/data/SigningPublicKey.java  | 10 ++++++++--
 11 files changed, 72 insertions(+), 26 deletions(-)

diff --git a/core/java/src/net/i2p/data/Destination.java b/core/java/src/net/i2p/data/Destination.java
index 5946056b38..7419e11ed5 100644
--- a/core/java/src/net/i2p/data/Destination.java
+++ b/core/java/src/net/i2p/data/Destination.java
@@ -137,10 +137,12 @@ public class Destination extends DataStructureImpl {
                && DataHelper.eq(getPublicKey(), dst.getPublicKey());
     }
     
+    /** the public key has enough randomness in it to use it by itself for speed */
     @Override
     public int hashCode() {
-        return DataHelper.hashCode(getCertificate()) + DataHelper.hashCode(getSigningPublicKey())
-               + DataHelper.hashCode(getPublicKey());
+        if (_publicKey == null)
+            return 0;
+        return _publicKey.hashCode();
     }
     
     @Override
diff --git a/core/java/src/net/i2p/data/Hash.java b/core/java/src/net/i2p/data/Hash.java
index 7dcce1ddbe..a573b6835d 100644
--- a/core/java/src/net/i2p/data/Hash.java
+++ b/core/java/src/net/i2p/data/Hash.java
@@ -147,9 +147,15 @@ public class Hash extends DataStructureImpl {
         return DataHelper.eq(_data, ((Hash) obj)._data);
     }
     
+    /** a Hash is a hash, so just use the first 4 bytes for speed */
     @Override
     public int hashCode() {
-        return DataHelper.hashCode(_data);
+        int rv = 0;
+        if (_data != null) {
+            for (int i = 0; i < 4; i++)
+                rv ^= (_data[i] << (i*8));
+        }
+        return rv;
     }
     
     @Override
@@ -267,4 +273,4 @@ public class Hash extends DataStructureImpl {
         }
         _log.debug("Fill check test passed");
     }
-}
\ No newline at end of file
+}
diff --git a/core/java/src/net/i2p/data/LeaseSet.java b/core/java/src/net/i2p/data/LeaseSet.java
index 3dcbf97a6a..4a683c257d 100644
--- a/core/java/src/net/i2p/data/LeaseSet.java
+++ b/core/java/src/net/i2p/data/LeaseSet.java
@@ -345,12 +345,12 @@ public class LeaseSet extends DataStructureImpl {
 
     }
     
+    /** the destination has enough randomness in it to use it by itself for speed */
     @Override
     public int hashCode() {
-        return DataHelper.hashCode(getEncryptionKey()) +
-        //(int)_version +
-               DataHelper.hashCode(_leases) + DataHelper.hashCode(getSignature())
-               + DataHelper.hashCode(getSigningKey()) + DataHelper.hashCode(getDestination());
+        if (_destination == null)
+            return 0;
+        return _destination.hashCode();
     }
     
     @Override
diff --git a/core/java/src/net/i2p/data/PrivateKey.java b/core/java/src/net/i2p/data/PrivateKey.java
index 7ea0473144..eed7f1eb77 100644
--- a/core/java/src/net/i2p/data/PrivateKey.java
+++ b/core/java/src/net/i2p/data/PrivateKey.java
@@ -70,9 +70,15 @@ public class PrivateKey extends DataStructureImpl {
         return DataHelper.eq(_data, ((PrivateKey) obj)._data);
     }
     
+    /** the key has enough randomness in it, use the first 4 bytes for speed */
     @Override
     public int hashCode() {
-        return DataHelper.hashCode(_data);
+        int rv = 0;
+        if (_data != null) {
+            for (int i = 0; i < 4; i++)
+                rv ^= (_data[i] << (i*8));
+        }
+        return rv;
     }
     
     @Override
@@ -100,4 +106,4 @@ public class PrivateKey extends DataStructureImpl {
         return KeyGenerator.getPublicKey(this);
     }
 
-}
\ No newline at end of file
+}
diff --git a/core/java/src/net/i2p/data/PublicKey.java b/core/java/src/net/i2p/data/PublicKey.java
index d653ea9a42..01bca46e5c 100644
--- a/core/java/src/net/i2p/data/PublicKey.java
+++ b/core/java/src/net/i2p/data/PublicKey.java
@@ -72,9 +72,15 @@ public class PublicKey extends DataStructureImpl {
         return DataHelper.eq(_data, ((PublicKey) obj)._data);
     }
     
+    /** the key has enough randomness in it, use the first 4 bytes for speed */
     @Override
     public int hashCode() {
-        return DataHelper.hashCode(_data);
+        int rv = 0;
+        if (_data != null) {
+            for (int i = 0; i < 4; i++)
+                rv ^= (_data[i] << (i*8));
+        }
+        return rv;
     }
     
     @Override
@@ -94,4 +100,4 @@ public class PublicKey extends DataStructureImpl {
         return buf.toString();
     }
 
-}
\ No newline at end of file
+}
diff --git a/core/java/src/net/i2p/data/RouterAddress.java b/core/java/src/net/i2p/data/RouterAddress.java
index 393bf9621f..f353f7d739 100644
--- a/core/java/src/net/i2p/data/RouterAddress.java
+++ b/core/java/src/net/i2p/data/RouterAddress.java
@@ -130,10 +130,10 @@ public class RouterAddress extends DataStructureImpl {
                && DataHelper.eq(getTransportStyle(), addr.getTransportStyle());
     }
     
+    /** the style should be sufficient, for speed */
     @Override
     public int hashCode() {
-        return getCost() + DataHelper.hashCode(getTransportStyle()) + DataHelper.hashCode(getExpiration())
-               + DataHelper.hashCode(getOptions());
+        return DataHelper.hashCode(getTransportStyle());
     }
     
     @Override
@@ -152,4 +152,4 @@ public class RouterAddress extends DataStructureImpl {
         buf.append("]");
         return buf.toString();
     }
-}
\ No newline at end of file
+}
diff --git a/core/java/src/net/i2p/data/RouterIdentity.java b/core/java/src/net/i2p/data/RouterIdentity.java
index d7375b2903..94f4760c1a 100644
--- a/core/java/src/net/i2p/data/RouterIdentity.java
+++ b/core/java/src/net/i2p/data/RouterIdentity.java
@@ -101,10 +101,12 @@ public class RouterIdentity extends DataStructureImpl {
                && DataHelper.eq(getPublicKey(), ident.getPublicKey());
     }
     
+    /** the public key has enough randomness in it to use it by itself for speed */
     @Override
     public int hashCode() {
-        return DataHelper.hashCode(getCertificate()) + DataHelper.hashCode(getSigningPublicKey())
-               + DataHelper.hashCode(getPublicKey());
+        if (_publicKey == null)
+            return 0;
+        return _publicKey.hashCode();
     }
     
     @Override
@@ -140,4 +142,4 @@ public class RouterIdentity extends DataStructureImpl {
         __calculatedHash = SHA256Generator.getInstance().calculateHash(identBytes);
         return __calculatedHash;
     }
-}
\ No newline at end of file
+}
diff --git a/core/java/src/net/i2p/data/SessionKey.java b/core/java/src/net/i2p/data/SessionKey.java
index b89bee5051..1b2ae8a1e0 100644
--- a/core/java/src/net/i2p/data/SessionKey.java
+++ b/core/java/src/net/i2p/data/SessionKey.java
@@ -76,9 +76,15 @@ public class SessionKey extends DataStructureImpl {
         return DataHelper.eq(_data, ((SessionKey) obj)._data);
     }
     
+    /** the key has enough randomness in it, use the first 4 bytes for speed */
     @Override
     public int hashCode() {
-        return DataHelper.hashCode(_data);
+        int rv = 0;
+        if (_data != null) {
+            for (int i = 0; i < 4; i++)
+                rv ^= (_data[i] << (i*8));
+        }
+        return rv;
     }
 
     @Override
@@ -98,4 +104,4 @@ public class SessionKey extends DataStructureImpl {
         buf.append("]");
         return buf.toString();
     }
-}
\ No newline at end of file
+}
diff --git a/core/java/src/net/i2p/data/Signature.java b/core/java/src/net/i2p/data/Signature.java
index 41a56aacd7..cb43741c5e 100644
--- a/core/java/src/net/i2p/data/Signature.java
+++ b/core/java/src/net/i2p/data/Signature.java
@@ -62,9 +62,15 @@ public class Signature extends DataStructureImpl {
         return DataHelper.eq(_data, ((Signature) obj)._data);
     }
     
+    /** the sig has enough randomness in it, use the first 4 bytes for speed */
     @Override
     public int hashCode() {
-        return DataHelper.hashCode(_data);
+        int rv = 0;
+        if (_data != null) {
+            for (int i = 0; i < 4; i++)
+                rv ^= (_data[i] << (i*8));
+        }
+        return rv;
     }
 
     @Override
@@ -83,4 +89,4 @@ public class Signature extends DataStructureImpl {
         buf.append("]");
         return buf.toString();
     }
-}
\ No newline at end of file
+}
diff --git a/core/java/src/net/i2p/data/SigningPrivateKey.java b/core/java/src/net/i2p/data/SigningPrivateKey.java
index 4a1a99ccb6..1d2e95cb0a 100644
--- a/core/java/src/net/i2p/data/SigningPrivateKey.java
+++ b/core/java/src/net/i2p/data/SigningPrivateKey.java
@@ -68,9 +68,15 @@ public class SigningPrivateKey extends DataStructureImpl {
         return DataHelper.eq(_data, ((SigningPrivateKey) obj)._data);
     }
     
+    /** the key has enough randomness in it, use the first 4 bytes for speed */
     @Override
     public int hashCode() {
-        return DataHelper.hashCode(_data);
+        int rv = 0;
+        if (_data != null) {
+            for (int i = 0; i < 4; i++)
+                rv ^= (_data[i] << (i*8));
+        }
+        return rv;
     }
 
     @Override
@@ -96,4 +102,4 @@ public class SigningPrivateKey extends DataStructureImpl {
     public SigningPublicKey toPublic() {
         return KeyGenerator.getSigningPublicKey(this);
     }
-}
\ No newline at end of file
+}
diff --git a/core/java/src/net/i2p/data/SigningPublicKey.java b/core/java/src/net/i2p/data/SigningPublicKey.java
index 09097593f1..44b8f3c137 100644
--- a/core/java/src/net/i2p/data/SigningPublicKey.java
+++ b/core/java/src/net/i2p/data/SigningPublicKey.java
@@ -67,9 +67,15 @@ public class SigningPublicKey extends DataStructureImpl {
         return DataHelper.eq(_data, ((SigningPublicKey) obj)._data);
     }
 
+    /** the key has enough randomness in it, use the first 4 bytes for speed */
     @Override
     public int hashCode() {
-        return DataHelper.hashCode(_data);
+        int rv = 0;
+        if (_data != null) {
+            for (int i = 0; i < 4; i++)
+                rv ^= (_data[i] << (i*8));
+        }
+        return rv;
     }
 
     @Override
@@ -88,4 +94,4 @@ public class SigningPublicKey extends DataStructureImpl {
         buf.append("]");
         return buf.toString();
     }
-}
\ No newline at end of file
+}
-- 
GitLab