Thread (16 messages) flat view 16 messages, 3 authors, 2016-06-15
DORMANTno replies

[EGIT PATCH 09/10] Hash ObjectId faster.

From: Robin Rosenberg <hidden>
Date: 2016-06-15 22:42:59
Subsystem: the rest · Maintainer: Linus Torvalds

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 <redacted>
---

 .../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) {
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help