Here is an example of an array cleanup that might be usefull.
* It uses Arrays.fill(...) when it can
* throwing badMajorRowIndex from one array (repeated code)
This one is a bit questionable:
* It started out as int32[] backing
becasue we needed one extra signless bit to represent a 'unit16'
PROS:
Also notice the previous was constantly: store -> unboxing , get -> reboxing
So almost anytime we wanted to store .. we took a submitted valid Fixnum.. then forgot about it.
Then when we want a value: we have to recreate a fixnum to replace the one we had previously lost.
CONS:
On a 32bit machine the new represention takes the same ram
64 bit machine takes twice the ram after the refactor.
Later on we might decide to allow this array to participate in the JavaObject ontology
.. if so, then this is a neat object for storing unsigned shorts..
however would JavaObject ever rerpresent Unsigned numbers?
-----------------------------------------------------------------------------------
Index: src/org/armedbear/lisp/SimpleArray_UnsignedByte16.java
===================================================================
--- src/org/armedbear/lisp/SimpleArray_UnsignedByte16.java (revision 12275)
+++ src/org/armedbear/lisp/SimpleArray_UnsignedByte16.java (working copy)
@@ -33,17 +33,19 @@
package org.armedbear.lisp;
+import java.util.Arrays;
+
public final class SimpleArray_UnsignedByte16 extends AbstractArray
{
private final int[] dimv;
private final int totalSize;
- private final int[] data;
+ private final Fixnum[] data;
public SimpleArray_UnsignedByte16(int[] dimv)
{
this.dimv = dimv;
totalSize = computeTotalSize(dimv);
- data = new int[totalSize];
+ data = new Fixnum[totalSize];
}
public SimpleArray_UnsignedByte16(int[] dimv, LispObject initialContents)
@@ -57,7 +59,7 @@
rest = rest.elt(0);
}
totalSize = computeTotalSize(dimv);
- data = new int[totalSize];
+ data = new Fixnum[totalSize];
setInitialContents(0, dimv, initialContents, 0);
}
@@ -75,7 +77,7 @@
rest = rest.elt(0);
}
totalSize = computeTotalSize(dimv);
- data = new int[totalSize];
+ data = new Fixnum[totalSize];
setInitialContents(0, dimv, initialContents, 0);
}
@@ -85,7 +87,7 @@
{
if (dims.length == 0) {
try {
- data[index] = coerceLispObjectToJavaByte(contents);
+ data[index] = checkValue(contents);
}
catch (ArrayIndexOutOfBoundsException e) {
error(new LispError("Bad initial contents for array."));
@@ -192,10 +194,10 @@
public int aref(int index)
{
try {
- return data[index];
+ return data[index].value;
}
catch (ArrayIndexOutOfBoundsException e) {
- error(new TypeError("Bad row major index " + index + "."));
+ badRowMajorIndex(index);
// Not reached.
return 0;
}
@@ -205,21 +207,33 @@
public LispObject AREF(int index)
{
try {
- return Fixnum.getInstance(data[index]);
+ return data[index];
}
catch (ArrayIndexOutOfBoundsException e) {
- return error(new TypeError("Bad row major index " + index + "."));
+ return badRowMajorIndex(index);
}
}
+
+ private LispObject badRowMajorIndex(int index) {
+ return error(new TypeError("Bad row major index " + index + "."));
+ }
+ public static Fixnum checkValue(LispObject obj)
+ {
+ if (obj instanceof Fixnum) {
+ return (Fixnum)obj;
+ }
+ return (Fixnum)type_error(obj, UNSIGNED_BYTE_16);
+ }
+
@Override
public void aset(int index, LispObject obj)
{
try {
- data[index] = Fixnum.getValue(obj);
+ data[index] = checkValue(obj);
}
catch (ArrayIndexOutOfBoundsException e) {
- error(new TypeError("Bad row major index " + index + "."));
+ badRowMajorIndex(index);
}
}
@@ -259,11 +273,10 @@
public LispObject get(int[] subscripts)
{
try {
- return Fixnum.getInstance(data[getRowMajorIndex(subscripts)]);
+ return data[getRowMajorIndex(subscripts)];
}
catch (ArrayIndexOutOfBoundsException e) {
- return error(new TypeError("Bad row major index " +
- getRowMajorIndex(subscripts) + "."));
+ return badRowMajorIndex(getRowMajorIndex(subscripts));
}
}
@@ -272,20 +285,17 @@
{
try {
- data[getRowMajorIndex(subscripts)] = Fixnum.getValue(obj);
+ data[getRowMajorIndex(subscripts)] = checkValue(obj);
}
catch (ArrayIndexOutOfBoundsException e) {
- error(new TypeError("Bad row major index " +
- getRowMajorIndex(subscripts) + "."));
+ badRowMajorIndex(getRowMajorIndex(subscripts));
}
}
@Override
public void fill(LispObject obj)
{
- int n = Fixnum.getValue(obj);
- for (int i = totalSize; i-- > 0;)
- data[i] = n;
+ Arrays.fill(data, 0, totalSize, checkValue(obj));
}
@Override