ObjectIdSubclassMap: Avoid field loads in inner loops
Ensure the JIT knows the table cannot be changed during the critical inner loop of get() or insert() by loading the field into a final local variable. This shouldn't be necessary, but the instance member is declared non-final (to resizing) and it is not very obvious to the JIT that the table cannot be modified by AnyObjectId.equals(). Simplify the JIT's decision making by making it obvious, these values cannot change during the critical inner loop, allowing for better register allocation. Change-Id: I0d797533fc5327366f1207b0937c406f02cdaab3 Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
parent
df7b192e26
commit
c11756ca4e
|
@ -93,12 +93,14 @@ public void clear() {
|
||||||
*/
|
*/
|
||||||
public V get(final AnyObjectId toFind) {
|
public V get(final AnyObjectId toFind) {
|
||||||
int i = toFind.w1 & mask;
|
int i = toFind.w1 & mask;
|
||||||
|
final V[] tbl = table;
|
||||||
|
final int end = tbl.length;
|
||||||
V obj;
|
V obj;
|
||||||
|
|
||||||
while ((obj = table[i]) != null) {
|
while ((obj = tbl[i]) != null) {
|
||||||
if (AnyObjectId.equals(obj, toFind))
|
if (AnyObjectId.equals(obj, toFind))
|
||||||
return obj;
|
return obj;
|
||||||
if (++i == table.length)
|
if (++i == end)
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -156,12 +158,14 @@ public <Q extends V> void add(final Q newValue) {
|
||||||
*/
|
*/
|
||||||
public <Q extends V> V addIfAbsent(final Q newValue) {
|
public <Q extends V> V addIfAbsent(final Q newValue) {
|
||||||
int i = newValue.w1 & mask;
|
int i = newValue.w1 & mask;
|
||||||
|
final V[] tbl = table;
|
||||||
|
final int end = tbl.length;
|
||||||
V obj;
|
V obj;
|
||||||
|
|
||||||
while ((obj = table[i]) != null) {
|
while ((obj = tbl[i]) != null) {
|
||||||
if (AnyObjectId.equals(obj, newValue))
|
if (AnyObjectId.equals(obj, newValue))
|
||||||
return obj;
|
return obj;
|
||||||
if (++i == table.length)
|
if (++i == end)
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,7 +173,7 @@ public <Q extends V> V addIfAbsent(final Q newValue) {
|
||||||
grow();
|
grow();
|
||||||
insert(newValue);
|
insert(newValue);
|
||||||
} else {
|
} else {
|
||||||
table[i] = newValue;
|
tbl[i] = newValue;
|
||||||
}
|
}
|
||||||
return newValue;
|
return newValue;
|
||||||
}
|
}
|
||||||
|
@ -215,11 +219,13 @@ public void remove() {
|
||||||
|
|
||||||
private void insert(final V newValue) {
|
private void insert(final V newValue) {
|
||||||
int j = newValue.w1 & mask;
|
int j = newValue.w1 & mask;
|
||||||
while (table[j] != null) {
|
final V[] tbl = table;
|
||||||
if (++j >= table.length)
|
final int end = tbl.length;
|
||||||
|
while (tbl[j] != null) {
|
||||||
|
if (++j == end)
|
||||||
j = 0;
|
j = 0;
|
||||||
}
|
}
|
||||||
table[j] = newValue;
|
tbl[j] = newValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void grow() {
|
private void grow() {
|
||||||
|
|
Loading…
Reference in New Issue