From da548dfd2b8ef9842cbb0c7da7183f50d93018a4 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 9 Mar 2011 14:32:43 -0800 Subject: [PATCH] ObjectIdSubclassMap: Grow before insertions If the table needs to be grown, do it before the current insertion rather than after. This is a tiny micro-optimization that allows the compiler to reuse the result of "++size" to compare against previously pre-computed size at which the table should rehash itself. Change-Id: Ief6f81b91c10ed433d67e0182f558ca70d58a2b0 Signed-off-by: Shawn O. Pearce --- .../src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 290b53445..6162c221a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java @@ -67,6 +67,8 @@ public class ObjectIdSubclassMap implements Iterable { private int size; + private int grow; + private int mask; private V[] table; @@ -127,10 +129,9 @@ public boolean contains(final AnyObjectId toFind) { * type of instance to store. */ public void add(final Q newValue) { - if (table.length - 1 <= size * 2) + if (++size == grow) grow(); insert(newValue); - size++; } /** @@ -164,13 +165,12 @@ public V addIfAbsent(final Q newValue) { i = 0; } - if (table.length - 1 <= size * 2) { + if (++size == grow) { grow(); insert(newValue); } else { table[i] = newValue; } - size++; return newValue; } @@ -239,6 +239,7 @@ private void grow() { } private void initTable(int sz) { + grow = sz >> 1; mask = sz - 1; table = createArray(sz); }