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>tags/v0.12.1
private int size; | private int size; | ||||
private int grow; | |||||
private int mask; | private int mask; | ||||
private V[] table; | private V[] table; | ||||
* 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++; | |||||
} | } | ||||
/** | /** | ||||
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; | ||||
} | } | ||||
} | } | ||||
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); | ||||
} | } |