Skip to content

Commit

Permalink
Merge pull request #311 from mouse0w0/master
Browse files Browse the repository at this point in the history
Cache class field ref into local
  • Loading branch information
vigna authored Feb 7, 2024
2 parents e0ef03b + c64b26d commit df4402a
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 44 deletions.
3 changes: 2 additions & 1 deletion drv/ArrayFIFOQueue.drv
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC implements PRIORITY_QUEUE KEY_GENERIC,
s.defaultWriteObject();
int size = size();
s.writeInt(size);
final KEY_GENERIC_TYPE[] array = this.array;
for(int i = start; size-- != 0;) {
s.WRITE_KEY(array[i++]);
if (i == length) i = 0;
Expand All @@ -219,7 +220,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC implements PRIORITY_QUEUE KEY_GENERIC,
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
end = s.readInt();
array = KEY_GENERIC_ARRAY_CAST new KEY_TYPE[length = HashCommon.nextPowerOfTwo(end + 1)];
final KEY_GENERIC_TYPE[] array = this.array = KEY_GENERIC_ARRAY_CAST new KEY_TYPE[length = HashCommon.nextPowerOfTwo(end + 1)];
for(int i = 0; i < end; i++) array[i] = KEY_GENERIC_CAST s.READ_KEY();
}
}
47 changes: 28 additions & 19 deletions drv/ArrayList.drv
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
#endif

/** The backing array. */
protected transient KEY_GENERIC_TYPE a[];
protected transient KEY_GENERIC_TYPE[] a;

/** The current actual size of the list (never greater than the backing-array length). */
protected int size;
Expand Down Expand Up @@ -137,7 +137,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
* @param a the array that will be used to back this array list.
*/

protected ARRAY_LIST(final KEY_GENERIC_TYPE a[], @SuppressWarnings("unused") boolean wrapped) {
protected ARRAY_LIST(final KEY_GENERIC_TYPE[] a, @SuppressWarnings("unused") boolean wrapped) {
this.a = a;
#if ! KEYS_PRIMITIVE
this.wrapped = wrapped;
Expand Down Expand Up @@ -243,7 +243,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
* @param a an array whose elements will be used to fill the array list.
*/

public ARRAY_LIST(final KEY_GENERIC_TYPE a[]) {
public ARRAY_LIST(final KEY_GENERIC_TYPE[] a) {
this(a, 0, a.length);
}

Expand All @@ -254,7 +254,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
* @param length the number of elements to use.
*/

public ARRAY_LIST(final KEY_GENERIC_TYPE a[], final int offset, final int length) {
public ARRAY_LIST(final KEY_GENERIC_TYPE[] a, final int offset, final int length) {
this(length);
System.arraycopy(a, offset, this.a, 0, length);
size = length;
Expand Down Expand Up @@ -320,7 +320,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
* @return a new array list of the given size, wrapping the given array.
*/

public static KEY_GENERIC ARRAY_LIST KEY_GENERIC wrap(final KEY_GENERIC_TYPE a[], final int length) {
public static KEY_GENERIC ARRAY_LIST KEY_GENERIC wrap(final KEY_GENERIC_TYPE[] a, final int length) {
if (length > a.length) throw new IllegalArgumentException("The specified length (" + length + ") is greater than the array size (" + a.length + ")");
final ARRAY_LIST KEY_GENERIC l = new ARRAY_LIST KEY_GENERIC_DIAMOND(a, true);
l.size = length;
Expand All @@ -337,7 +337,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
* @return a new array list wrapping the given array.
*/

public static KEY_GENERIC ARRAY_LIST KEY_GENERIC wrap(final KEY_GENERIC_TYPE a[]) {
public static KEY_GENERIC ARRAY_LIST KEY_GENERIC wrap(final KEY_GENERIC_TYPE[] a) {
return wrap(a, a.length);
}

Expand Down Expand Up @@ -451,7 +451,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
if (wrapped) a = ARRAYS.ensureCapacity(a, capacity, size);
else {
if (capacity > a.length) {
final Object t[] = new Object[capacity];
final Object[] t = new Object[capacity];
System.arraycopy(a, 0, t, 0, size);
a = (KEY_GENERIC_TYPE[])t;
}
Expand All @@ -476,7 +476,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
#else
if (wrapped) a = ARRAYS.forceCapacity(a, capacity, size);
else {
final Object t[] = new Object[capacity];
final Object[] t = new Object[capacity];
System.arraycopy(a, 0, t, 0, size);
a = (KEY_GENERIC_TYPE[])t;
}
Expand Down Expand Up @@ -510,20 +510,23 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements

@Override
public int indexOf(final KEY_TYPE k) {
final KEY_TYPE[] a = this.a;
for(int i = 0; i < size; i++) if (KEY_EQUALS(k, a[i])) return i;
return -1;
}


@Override
public int lastIndexOf(final KEY_TYPE k) {
final KEY_TYPE[] a = this.a;
for(int i = size; i-- != 0;) if (KEY_EQUALS(k, a[i])) return i;
return -1;
}

@Override
public KEY_GENERIC_TYPE REMOVE_KEY(final int index) {
if (index >= size) throw new IndexOutOfBoundsException("Index (" + index + ") is greater than or equal to list size (" + size + ")");
final KEY_GENERIC_TYPE[] a = this.a;
final KEY_GENERIC_TYPE old = a[index];
size--;
if (index != size) System.arraycopy(a, index + 1, a, index, size - index);
Expand Down Expand Up @@ -658,6 +661,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements

@Override
public void forEachRemaining(final METHOD_ARG_KEY_CONSUMER action) {
final KEY_GENERIC_TYPE[] a = ARRAY_LIST.this.a;
final int max = to - from;
while(pos < max) {
action.accept(a[from + (lastReturned = pos++)]);
Expand Down Expand Up @@ -698,6 +702,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
}
@Override
public void forEachRemaining(final METHOD_ARG_KEY_CONSUMER action) {
final KEY_GENERIC_TYPE[] a = ARRAY_LIST.this.a;
final int max = getMaxPos();
while(pos < max) {
action.accept(a[pos++]);
Expand Down Expand Up @@ -831,7 +836,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
* @param length the number of elements to add.
*/
@Override
public void addElements(final int index, final KEY_GENERIC_TYPE a[], final int offset, final int length) {
public void addElements(final int index, final KEY_GENERIC_TYPE[] a, final int offset, final int length) {
ensureIndex(index);
ARRAYS.ensureOffsetLength(a, offset, length);
grow(size + length);
Expand All @@ -848,7 +853,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
* @param length the number of elements to add.
*/
@Override
public void setElements(final int index, final KEY_GENERIC_TYPE a[], final int offset, final int length) {
public void setElements(final int index, final KEY_GENERIC_TYPE[] a, final int offset, final int length) {
ensureIndex(index);
ARRAYS.ensureOffsetLength(a, offset, length);
if (index + length > size) throw new IndexOutOfBoundsException("End index (" + (index + length) + ") is greater than list size (" + size + ")");
Expand All @@ -857,6 +862,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements

@Override
public void forEach(final METHOD_ARG_KEY_CONSUMER action) {
final KEY_GENERIC_TYPE[] a = this.a;
for (int i = 0; i < size; ++i) {
action.accept(a[i]);
}
Expand Down Expand Up @@ -994,6 +1000,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
}
@Override
public void forEachRemaining(final METHOD_ARG_KEY_CONSUMER action) {
final KEY_GENERIC_TYPE[] a = ARRAY_LIST.this.a;
while (pos < size) {
action.accept(a[last = pos++]);
}
Expand Down Expand Up @@ -1071,6 +1078,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements

@Override
public void forEachRemaining(final METHOD_ARG_KEY_CONSUMER action) {
final KEY_GENERIC_TYPE[] a = ARRAY_LIST.this.a;
for (final int max = getWorkingMax(); pos < max; ++pos) {
action.accept(a[pos]);
}
Expand Down Expand Up @@ -1229,7 +1237,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
SUPPRESS_WARNINGS_KEY_UNCHECKED
public int compareTo(final ARRAY_LIST KEY_EXTENDS_GENERIC l) {
final int s1 = size(), s2 = l.size();
final KEY_GENERIC_TYPE a1[] = a, a2[] = l.a;
final KEY_GENERIC_TYPE[] a1 = a, a2 = l.a;
#if KEYS_PRIMITIVE // Can't make this assumption for reference types in case we have a goofy Comparable that doesn't compare itself equal
if (a1 == a2 && s1 == s2) return 0;
#endif
Expand Down Expand Up @@ -1263,13 +1271,14 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
final KEY_GENERIC_TYPE[] a = this.a;
for(int i = 0; i < size; i++) s.WRITE_KEY(a[i]);
}

SUPPRESS_WARNINGS_KEY_UNCHECKED
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
a = KEY_GENERIC_ARRAY_CAST new KEY_TYPE[size];
final KEY_GENERIC_TYPE[] a = this.a = KEY_GENERIC_ARRAY_CAST new KEY_TYPE[size];
for(int i = 0; i < size; i++) a[i] = KEY_GENERIC_CAST s.READ_KEY();
}

Expand Down Expand Up @@ -1321,9 +1330,9 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements

int i;
int j;
KEY_TYPE k[] = new KEY_TYPE[n];
KEY_TYPE nk[] = new KEY_TYPE[n];
int randIndexes[] = new int[n];
KEY_TYPE[] k = new KEY_TYPE[n];
KEY_TYPE[] nk = new KEY_TYPE[n];
int[] randIndexes = new int[n];
long ns;

for(i = 0; i < n; i++) {
Expand Down Expand Up @@ -1514,8 +1523,8 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
}

private static Object[] k, v, nk;
private static KEY_TYPE kt[];
private static KEY_TYPE nkt[];
private static KEY_TYPE[] kt;
private static KEY_TYPE[] nkt;
private static ARRAY_LIST topList;

protected static void testLists(LIST m, java.util.List t, int n, int level) throws Exception {
Expand Down Expand Up @@ -1786,7 +1795,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
int p = r.nextInt() % (2 * n + 1);
Collection t1 = new java.util.ArrayList();
int s = r.nextInt(n / 2 + 1);
KEY_TYPE a[] = new KEY_TYPE [s];
KEY_TYPE[] a = new KEY_TYPE [s];
for(int j = 0; j < s; j++) {
KEY_TYPE x = genKey();
t1.add(KEY2OBJ(x));
Expand Down Expand Up @@ -2131,7 +2140,7 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
}


public static void main(String args[]) throws Exception {
public static void main(String[] args) throws Exception {
int n = Integer.parseInt(args[1]);
if (args.length > 2) r = new java.util.Random(seed = Long.parseLong(args[2]));

Expand Down
37 changes: 25 additions & 12 deletions drv/ArrayMap.drv
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC
@Override
SUPPRESS_WARNINGS_KEY_VALUE_UNCHECKED
public void forEachRemaining(final Consumer<? super MAP.Entry KEY_VALUE_GENERIC> action) {
// Hoist containing class field ref into local
final KEY_TYPE[] key = ARRAY_MAP.this.key;
final VALUE_TYPE[] value = ARRAY_MAP.this.value;
final int max = size;
while (next < max) {
action.accept(new ABSTRACT_MAP.BasicEntry KEY_VALUE_GENERIC_DIAMOND(KEY_GENERIC_CAST key[curr = next], VALUE_GENERIC_CAST value[next++]));
Expand Down Expand Up @@ -222,7 +223,8 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC
@Override
SUPPRESS_WARNINGS_KEY_VALUE_UNCHECKED
public void forEachRemaining(final Consumer<? super MAP.Entry KEY_VALUE_GENERIC> action) {
// Hoist containing class field ref into local
final KEY_TYPE[] key = ARRAY_MAP.this.key;
final VALUE_TYPE[] value = ARRAY_MAP.this.value;
final int max = size;
while (next < max) {
entry.key = KEY_GENERIC_CAST key[curr = next];
Expand Down Expand Up @@ -268,7 +270,8 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC
@Override
SUPPRESS_WARNINGS_KEY_VALUE_UNCHECKED
public void forEach(final Consumer<? super MAP.Entry KEY_VALUE_GENERIC> action) {
// Hoist containing class field ref into local
final KEY_TYPE[] key = ARRAY_MAP.this.key;
final VALUE_TYPE[] value = ARRAY_MAP.this.value;
for (int i = 0, max = size; i < max; ++i) {
action.accept(new ABSTRACT_MAP.BasicEntry KEY_VALUE_GENERIC_DIAMOND(KEY_GENERIC_CAST key[i], VALUE_GENERIC_CAST value[i]));
}
Expand All @@ -278,8 +281,9 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC
@Override
SUPPRESS_WARNINGS_KEY_VALUE_UNCHECKED
public void fastForEach(final Consumer<? super MAP.Entry KEY_VALUE_GENERIC> action) {
final KEY_TYPE[] key = ARRAY_MAP.this.key;
final VALUE_TYPE[] value = ARRAY_MAP.this.value;
final BasicEntry KEY_VALUE_GENERIC entry = new BasicEntry KEY_VALUE_GENERIC_DIAMOND ();
// Hoist containing class field ref into local
for (int i = 0, max = size; i < max; ++i) {
entry.key = KEY_GENERIC_CAST key[i];
entry.value = VALUE_GENERIC_CAST value[i];
Expand Down Expand Up @@ -360,6 +364,12 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC

@Override
public void clear() {
#if KEYS_REFERENCE
final KEY_TYPE[] key = this.key;
#endif
#if VALUES_REFERENCE
final VALUE_TYPE[] value = this.value;
#endif
#if KEYS_REFERENCE || VALUES_REFERENCE
for(int i = size; i-- != 0;) {
#if KEYS_REFERENCE
Expand All @@ -378,6 +388,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC

@Override
public boolean containsValue(VALUE_TYPE v) {
final VALUE_TYPE[] value = this.value;
for(int i = size; i-- != 0;) if (VALUE_EQUALS(value[i], v)) return true;
return false;
}
Expand Down Expand Up @@ -487,7 +498,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC
@Override
SUPPRESS_WARNINGS_KEY_UNCHECKED
public void forEachRemaining(final METHOD_ARG_KEY_CONSUMER action) {
// Hoist containing class field ref into local
final KEY_TYPE[] key = ARRAY_MAP.this.key;
final int max = size;
while (pos < max) {
action.accept(KEY_GENERIC_CAST key[pos++]);
Expand Down Expand Up @@ -522,7 +533,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC
@Override
SUPPRESS_WARNINGS_KEY_UNCHECKED
public void forEachRemaining(final METHOD_ARG_KEY_CONSUMER action) {
// Hoist containing class field ref into local
final KEY_TYPE[] key = ARRAY_MAP.this.key;
final int max = size;
while (pos < max) {
action.accept(KEY_GENERIC_CAST key[pos++]);
Expand All @@ -538,7 +549,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC
@Override
SUPPRESS_WARNINGS_KEY_UNCHECKED
public void forEach(METHOD_ARG_KEY_CONSUMER action) {
// Hoist containing class field ref into local
final KEY_TYPE[] key = ARRAY_MAP.this.key;
for (int i = 0, max = size; i < max; ++i) {
action.accept(KEY_GENERIC_CAST key[i]);
}
Expand Down Expand Up @@ -602,7 +613,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC
@Override
SUPPRESS_WARNINGS_VALUE_UNCHECKED
public void forEachRemaining(final METHOD_ARG_VALUE_CONSUMER action) {
// Hoist containing class field ref into local
final VALUE_TYPE[] value = ARRAY_MAP.this.value;
final int max = size;
while (pos < max) {
action.accept(VALUE_GENERIC_CAST value[pos++]);
Expand Down Expand Up @@ -638,7 +649,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC
@Override
SUPPRESS_WARNINGS_VALUE_UNCHECKED
public void forEachRemaining(final METHOD_ARG_VALUE_CONSUMER action) {
// Hoist containing class field ref into local
final VALUE_TYPE[] value = ARRAY_MAP.this.value;
final int max = size;
while (pos < max) {
action.accept(VALUE_GENERIC_CAST value[pos++]);
Expand All @@ -654,7 +665,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC
@Override
SUPPRESS_WARNINGS_VALUE_UNCHECKED
public void forEach(METHOD_ARG_VALUE_CONSUMER action) {
// Hoist containing class field ref into local
final VALUE_TYPE[] value = ARRAY_MAP.this.value;
for (int i = 0, max = size; i < max; ++i) {
action.accept(VALUE_GENERIC_CAST value[i]);
}
Expand Down Expand Up @@ -704,6 +715,8 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
final KEY_TYPE[] key = this.key;
final VALUE_TYPE[] value = this.value;
for(int i = 0, max = size; i < max; i++) {
s.WRITE_KEY(key[i]);
s.WRITE_VALUE(value[i]);
Expand All @@ -712,8 +725,8 @@ public class ARRAY_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENERIC

private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
key = new KEY_TYPE[size];
value = new VALUE_TYPE[size];
final KEY_TYPE[] key = this.key = new KEY_TYPE[size];
final VALUE_TYPE[] value = this.value = new VALUE_TYPE[size];
for(int i = 0; i < size; i++) {
key[i] = s.READ_KEY();
value[i] = s.READ_VALUE();
Expand Down
3 changes: 2 additions & 1 deletion drv/ArrayPriorityQueue.drv
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,14 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC implements PRIORITY_QUEUE KEY_GENE
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
s.writeInt(array.length);
final KEY_GENERIC_TYPE[] array = this.array;
for(int i = 0; i < size; i++) s.WRITE_KEY(array[i]);
}

SUPPRESS_WARNINGS_KEY_UNCHECKED
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
array = KEY_GENERIC_ARRAY_CAST new KEY_TYPE[s.readInt()];
final KEY_GENERIC_TYPE[] array = this.array = KEY_GENERIC_ARRAY_CAST new KEY_TYPE[s.readInt()];
for(int i = 0; i < size; i++) array[i] = KEY_GENERIC_CAST s.READ_KEY();
}
}
Loading

0 comments on commit df4402a

Please sign in to comment.