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 <spearce@spearce.org>
This commit is contained in:
Shawn O. Pearce 2011-03-09 14:32:43 -08:00
parent 47c2a3a98d
commit da548dfd2b
1 changed files with 5 additions and 4 deletions

View File

@ -67,6 +67,8 @@ public class ObjectIdSubclassMap<V extends ObjectId> implements Iterable<V> {
private int size; private int size;
private int grow;
private int mask; private int mask;
private V[] table; private V[] table;
@ -127,10 +129,9 @@ public boolean contains(final AnyObjectId toFind) {
* type of instance to store. * type of instance to store.
*/ */
public <Q extends V> void add(final Q newValue) { public <Q extends V> void add(final Q newValue) {
if (table.length - 1 <= size * 2) if (++size == grow)
grow(); grow();
insert(newValue); insert(newValue);
size++;
} }
/** /**
@ -164,13 +165,12 @@ public <Q extends V> V addIfAbsent(final Q newValue) {
i = 0; i = 0;
} }
if (table.length - 1 <= size * 2) { if (++size == grow) {
grow(); grow();
insert(newValue); insert(newValue);
} else { } else {
table[i] = newValue; table[i] = newValue;
} }
size++;
return newValue; return newValue;
} }
@ -239,6 +239,7 @@ private void grow() {
} }
private void initTable(int sz) { private void initTable(int sz) {
grow = sz >> 1;
mask = sz - 1; mask = sz - 1;
table = createArray(sz); table = createArray(sz);
} }