[EGIT PATCH 09/10] Hash ObjectId faster.
- From
Robin Rosenberg <robin.rosenberg@dewire.com>
- Date
- Mar 11, 2007, 18:16 UTC
- Message-ID
- <20070311181603.18012.78635.stgit@lathund.dewire.com>
- In-Reply-To
- <20070311180608.18012.13767.stgit@lathund.dewire.com>
ObjectId's are well distributed so we can grab the first four bytes as the hash code. The assumption makes hashing faster.
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> ---
.../src/org/spearce/jgit/lib/ObjectId.java | 13 +++++++------ 1 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java index 9e62424..45e23e6 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectId.java @@ -163,12 +163,13 @@ public class ObjectId implements Comparable { } public int hashCode() { - int r = 0; - for (int k = id.length - 1; k >= 0; k--) { - r *= 31; - r += id[k]; - } - return r; + // Object Id' are well distributed so grab the first four bytes + int b0 = id[0] & 0xff; + int b1 = id[1] & 0xff; + int b2 = id[2] & 0xff; + int b3 = id[3] & 0xff; + int h = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3; + return h; } public boolean equals(final ObjectId o) {