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
2009/11/11 logicmoo@gmail.com:
- It uses Arrays.fill(...) when it can
- throwing badMajorRowIndex from one array (repeated code)
That's a nice cleanup, avoids repetition.
CONS: On a 32bit machine the new represention takes the same ram 64 bit machine takes twice the ram after the refactor.
Are our Fixnums dependent on the machine architecture? Why, how?
Is there any noticeable performance difference?
CONS: On a 32bit machine the new represention takes the same ram 64 bit machine takes twice the ram after the refactor.
Are our Fixnums dependent on the machine architecture? Why, how?
int[] array on both archs are 32bits each Fixnum[] is a java.lang.Object[] which are 32bits on JVM32 and 64bits on JVM64. - So that is where the extra memory comes in
Now that is evaluating uint16s in BasicVector_UnsignedByte16 when it comes to uint32s in BasicVector_UnsignedByte32 .. we store now into long[] which means this: On JVM32 we'd use up only 1/2 the memory than currently to store the same data if boxed (if all the values were the simular numbers, If every value stored in the array is differnt.. bets are off since we allocate their boxes on the java heap)
Is there any noticeable performance difference?
Its really hard to say ..
INT32
;; -*- lisp -*- Armed Bear Common Lisp 0.18.0-dev ;; ;; Implementation *features*: ;; (:X86-64 :JAVA-1.6 :ARMEDBEAR :ABCL :COMMON-LISP :ANSI-CL :WINDOWS :CDR6) ;; ;; Function real user sys consed ;; ---------------------------------------------------------------- ;; 1D-ARRAYS 7.219 7.219 0.0 0 ;; 2D-ARRAYS 20.562 20.562 0.0 0 ;; 3D-ARRAYS 25.25 25.25 0.0 0 ;; BITVECTORS 2.688 2.688 0.0 0 ;; BENCH-STRINGS 11.468 11.468 0.0 0 ;; fill-strings/adjustable 6.391 6.391 0.0 0 ;; STRING-CONCAT 20.515 20.515 0.0 0
("Armed Bear Common Lisp 0.18.0-dev" ("STRING-CONCAT" 4103/200 4103/200 0 0) ("fill-strings/adjustable" 6391/1000 6391/1000 0 0) ("BENCH-STRINGS" 2867/250 2867/250 0 0) ("BITVECTORS" 336/125 336/125 0 0) ("3D-ARRAYS" 101/4 101/4 0 0) ("2D-ARRAYS" 10281/500 10281/500 0 0) ("1D-ARRAYS" 7219/1000 7219/1000 0 0))
FIXNUM ;; -*- lisp -*- Armed Bear Common Lisp 0.18.0-dev ;; ;; Implementation *features*: ;; (:X86-64 :JAVA-1.6 :ARMEDBEAR :ABCL :COMMON-LISP :ANSI-CL :WINDOWS :CDR6) ;; ;; Function real user sys consed ;; ---------------------------------------------------------------- ;; 1D-ARRAYS 7.922 7.922 0.0 0 ;; 2D-ARRAYS 24.031 24.031 0.0 0 ;; 3D-ARRAYS 27.734 27.734 0.0 0 ;; BITVECTORS 3.282 3.282 0.0 0 ;; BENCH-STRINGS 13.859 13.859 0.0 0 ;; fill-strings/adjustable 7.078 7.078 0.0 0 ;; STRING-CONCAT 22.688 22.688 0.0 0
("Armed Bear Common Lisp 0.18.0-dev" ("STRING-CONCAT" 2836/125 2836/125 0 0) ("fill-strings/adjustable" 3539/500 3539/500 0 0) ("BENCH-STRINGS" 13859/1000 13859/1000 0 0) ("BITVECTORS" 1641/500 1641/500 0 0) ("3D-ARRAYS" 13867/500 13867/500 0 0) ("2D-ARRAYS" 24031/1000 24031/1000 0 0) ("1D-ARRAYS" 3961/500 3961/500 0 0))
FIXNUM ;; -*- lisp -*- Armed Bear Common Lisp 0.18.0-dev ;; ;; Implementation *features*: ;; (:X86-64 :JAVA-1.6 :ARMEDBEAR :ABCL :COMMON-LISP :ANSI-CL :WINDOWS :CDR6) ;; ;; Function real user sys consed ;; ---------------------------------------------------------------- ;; 1D-ARRAYS 7.063 7.063 0.0 0 ;; 2D-ARRAYS 21.609 21.609 0.0 0 ;; 3D-ARRAYS 26.063 26.063 0.0 0 ;; BITVECTORS 2.656 2.656 0.0 0 ;; BENCH-STRINGS 11.625 11.625 0.0 0 ;; fill-strings/adjustable 6.422 6.422 0.0 0 ;; STRING-CONCAT 20.015 20.015 0.0 0
("Armed Bear Common Lisp 0.18.0-dev" ("STRING-CONCAT" 4003/200 4003/200 0 0) ("fill-strings/adjustable" 3211/500 3211/500 0 0) ("BENCH-STRINGS" 93/8 93/8 0 0) ("BITVECTORS" 332/125 332/125 0 0) ("3D-ARRAYS" 26063/1000 26063/1000 0 0) ("2D-ARRAYS" 21609/1000 21609/1000 0 0) ("1D-ARRAYS" 7063/1000 7063/1000 0 0))
INT32
;; -*- lisp -*- Armed Bear Common Lisp 0.18.0-dev ;; ;; Implementation *features*: ;; (:X86-64 :JAVA-1.6 :ARMEDBEAR :ABCL :COMMON-LISP :ANSI-CL :WINDOWS :CDR6) ;; ;; Function real user sys consed ;; ---------------------------------------------------------------- ;; 1D-ARRAYS 7.375 7.375 0.0 0 ;; 2D-ARRAYS 21.312 21.312 0.0 0 ;; 3D-ARRAYS 26.094 26.094 0.0 0 ;; BITVECTORS 2.703 2.703 0.0 0 ;; BENCH-STRINGS 12.281 12.281 0.0 0 ;; fill-strings/adjustable 6.266 6.266 0.0 0 ;; STRING-CONCAT 19.859 19.859 0.0 0
("Armed Bear Common Lisp 0.18.0-dev" ("STRING-CONCAT" 19859/1000 19859/1000 0 0) ("fill-strings/adjustable" 3133/500 3133/500 0 0) ("BENCH-STRINGS" 12281/1000 12281/1000 0 0) ("BITVECTORS" 2703/1000 2703/1000 0 0) ("3D-ARRAYS" 13047/500 13047/500 0 0) ("2D-ARRAYS" 2664/125 2664/125 0 0) ("1D-ARRAYS" 59/8 59/8 0 0))
FIXNUM ;; -*- lisp -*- Armed Bear Common Lisp 0.18.0-dev ;; ;; Implementation *features*: ;; (:X86-64 :JAVA-1.6 :ARMEDBEAR :ABCL :COMMON-LISP :ANSI-CL :WINDOWS :CDR6) ;; ;; Function real user sys consed ;; ---------------------------------------------------------------- ;; 1D-ARRAYS 7.125 7.125 0.0 0 ;; 2D-ARRAYS 21.422 21.422 0.0 0 ;; 3D-ARRAYS 26.187 26.187 0.0 0 ;; BITVECTORS 2.703 2.703 0.0 0 ;; BENCH-STRINGS 11.031 11.031 0.0 0 ;; fill-strings/adjustable 6.438 6.438 0.0 0 ;; STRING-CONCAT 19.594 19.594 0.0 0
("Armed Bear Common Lisp 0.18.0-dev" ("STRING-CONCAT" 9797/500 9797/500 0 0) ("fill-strings/adjustable" 3219/500 3219/500 0 0) ("BENCH-STRINGS" 11031/1000 11031/1000 0 0) ("BITVECTORS" 2703/1000 2703/1000 0 0) ("3D-ARRAYS" 26187/1000 26187/1000 0 0) ("2D-ARRAYS" 10711/500 10711/500 0 0) ("1D-ARRAYS" 57/8 57/8 0 0))
INT32 ;; -*- lisp -*- Armed Bear Common Lisp 0.18.0-dev ;; ;; Implementation *features*: ;; (:X86-64 :JAVA-1.6 :ARMEDBEAR :ABCL :COMMON-LISP :ANSI-CL :WINDOWS :CDR6) ;; ;; Function real user sys consed ;; ---------------------------------------------------------------- ;; 1D-ARRAYS 7.235 7.235 0.0 0 ;; 2D-ARRAYS 20.875 20.875 0.0 0 ;; 3D-ARRAYS 25.718 25.718 0.0 0 ;; BITVECTORS 2.672 2.672 0.0 0 ;; BENCH-STRINGS 12.297 12.297 0.0 0 ;; fill-strings/adjustable 6.343 6.343 0.0 0 ;; STRING-CONCAT 20.063 20.063 0.0 0
("Armed Bear Common Lisp 0.18.0-dev" ("STRING-CONCAT" 20063/1000 20063/1000 0 0) ("fill-strings/adjustable" 6343/1000 6343/1000 0 0) ("BENCH-STRINGS" 12297/1000 12297/1000 0 0) ("BITVECTORS" 334/125 334/125 0 0) ("3D-ARRAYS" 12859/500 12859/500 0 0) ("2D-ARRAYS" 167/8 167/8 0 0) ("1D-ARRAYS" 1447/200 1447/200 0 0))
_______________________________________________ armedbear-devel mailing list armedbear-devel@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/armedbear-devel
armedbear-devel@common-lisp.net