Browse Source

Fix comparison order in AnyObjectId

The previous version suggested testing w2 first because w1 was used
for hashing, but in fact, hashCode returns w2.  The order (w3, w4, w5,
w1, w2) might be better on 64-bit processors too, since it allows
comparing 64 bits at a time, although perhaps on a modern SIMD
processor, the entire 160 bytes would be compared at once anyway.

Change-Id: Ieb69606d3c1456aeff36bffe99a71587ea76e977
Signed-off-by: David Turner <dturner@twosigma.com>
stable-5.0
David Turner 6 years ago
parent
commit
d4f3ae0c43
  1. 18
      org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java

18
org.eclipse.jgit/src/org/eclipse/jgit/lib/AnyObjectId.java

@ -73,17 +73,19 @@ public abstract class AnyObjectId implements Comparable<AnyObjectId> {
if (firstObjectId == secondObjectId) if (firstObjectId == secondObjectId)
return true; return true;
// We test word 2 first as odds are someone already used our // We test word 3 first since the git file-based ODB
// word 1 as a hash code, and applying that came up with these // uses the first byte of w1, and we use w2 as the
// two instances we are comparing for equality. Therefore the // hash code, one of those probably came up with these
// first two words are very likely to be identical. We want to // two instances which we are comparing for equality.
// break away from collisions as quickly as possible. // Therefore the first two words are very likely to be
// identical. We want to break away from collisions as
// quickly as possible.
// //
return firstObjectId.w2 == secondObjectId.w2 return firstObjectId.w3 == secondObjectId.w3
&& firstObjectId.w3 == secondObjectId.w3
&& firstObjectId.w4 == secondObjectId.w4 && firstObjectId.w4 == secondObjectId.w4
&& firstObjectId.w5 == secondObjectId.w5 && firstObjectId.w5 == secondObjectId.w5
&& firstObjectId.w1 == secondObjectId.w1; && firstObjectId.w1 == secondObjectId.w1
&& firstObjectId.w2 == secondObjectId.w2;
} }
int w1; int w1;

Loading…
Cancel
Save