From: Shawn O. Pearce Date: Thu, 10 Mar 2011 18:09:58 +0000 (-0800) Subject: ObjectIdSubclassMap: Micro-optimize wrapping at end of table X-Git-Tag: v0.12.1~86^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fchanges%2F89%2F2689%2F1;p=jgit.git ObjectIdSubclassMap: Micro-optimize wrapping at end of table During a review of the class, Josh Bloch pointed out we can use "i = (i + 1) & mask" to wrap around at the end of the table, instead of a conditional with a branch. This is generally faster due to one less branch that will be mis-predicted by the CPU. Change-Id: Ic88c00455ebc6adde9708563a6ad4d0377442bba Signed-off-by: Shawn O. Pearce --- diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java index c1bff7ecfe..d53901b912 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java @@ -92,16 +92,15 @@ public class ObjectIdSubclassMap implements Iterable { * @return the instance mapped to toFind, or null if no mapping exists. */ public V get(final AnyObjectId toFind) { - int i = toFind.w1 & mask; + final int msk = mask; + int i = toFind.w1 & msk; final V[] tbl = table; - final int end = tbl.length; V obj; while ((obj = tbl[i]) != null) { if (AnyObjectId.equals(obj, toFind)) return obj; - if (++i == end) - i = 0; + i = (i + 1) & msk; } return null; } @@ -157,16 +156,15 @@ public class ObjectIdSubclassMap implements Iterable { * type of instance to store. */ public V addIfAbsent(final Q newValue) { - int i = newValue.w1 & mask; + final int msk = mask; + int i = newValue.w1 & msk; final V[] tbl = table; - final int end = tbl.length; V obj; while ((obj = tbl[i]) != null) { if (AnyObjectId.equals(obj, newValue)) return obj; - if (++i == end) - i = 0; + i = (i + 1) & msk; } if (++size == grow) { @@ -218,13 +216,11 @@ public class ObjectIdSubclassMap implements Iterable { } private void insert(final V newValue) { - int j = newValue.w1 & mask; + final int msk = mask; + int j = newValue.w1 & msk; final V[] tbl = table; - final int end = tbl.length; - while (tbl[j] != null) { - if (++j == end) - j = 0; - } + while (tbl[j] != null) + j = (j + 1) & msk; tbl[j] = newValue; }