Convert using ClassCastException to checking instanceof.

Performance tests show this approach to be faster.
Patch by Douglas R. Miles. I modified the patch to
remove tabs, so indentation may be slightly off in places.
That's something that we need to handle separately, abcl
doesn't have a clear indentation policy.
parent c3286ced
......@@ -113,7 +113,7 @@ public abstract class AbstractBitVector extends AbstractVector
@Override
public void fill(LispObject obj) throws ConditionThrowable
{
try {
if (obj instanceof Fixnum) {
switch (((Fixnum)obj).value) {
case 0:
if (bits != null) {
......@@ -134,8 +134,6 @@ public abstract class AbstractBitVector extends AbstractVector
}
return;
}
}
catch (ClassCastException e) {
// Fall through...
}
error(new TypeError(obj, Symbol.BIT));
......@@ -201,12 +199,7 @@ public abstract class AbstractBitVector extends AbstractVector
@Override
public LispObject AREF(LispObject index) throws ConditionThrowable
{
try {
return AREF(((Fixnum)index).value);
}
catch (ClassCastException e) {
return error(new TypeError(index, Symbol.FIXNUM));
}
return AREF(Fixnum.getValue(index));
}
@Override
......
......@@ -127,10 +127,10 @@ public class ArithmeticError extends LispError
@Override
public LispObject execute(LispObject arg) throws ConditionThrowable
{
try {
if (arg instanceof ArithmeticError) {
return ((ArithmeticError)arg).getOperation();
}
catch (ClassCastException e) {
else {
return error(new TypeError(arg, Symbol.ARITHMETIC_ERROR));
}
}
......@@ -142,10 +142,10 @@ public class ArithmeticError extends LispError
@Override
public LispObject execute(LispObject arg) throws ConditionThrowable
{
try {
if (arg instanceof ArithmeticError) {
return ((ArithmeticError)arg).getOperands();
}
catch (ClassCastException e) {
else {
return error(new TypeError(arg, Symbol.ARITHMETIC_ERROR));
}
}
......
......@@ -158,10 +158,7 @@ public final class BasicVector_UnsignedByte16 extends AbstractVector
public LispObject AREF(LispObject index) throws ConditionThrowable
{
try {
return Fixnum.getInstance(elements[((Fixnum)index).value]);
}
catch (ClassCastException e) {
return error(new TypeError(index, Symbol.FIXNUM));
return Fixnum.getInstance(elements[Fixnum.getValue(index)]);
}
catch (ArrayIndexOutOfBoundsException e) {
badIndex(Fixnum.getValue(index), elements.length);
......@@ -183,13 +180,15 @@ public final class BasicVector_UnsignedByte16 extends AbstractVector
@Override
public void aset(int index, LispObject obj) throws ConditionThrowable
{
try {
if (obj instanceof Fixnum) {
try {
elements[index] = ((Fixnum)obj).value;
}
catch (ArrayIndexOutOfBoundsException e) {
badIndex(index, capacity);
}
catch (ClassCastException e) {
}
else {
error(new TypeError(obj, UNSIGNED_BYTE_16));
}
}
......
......@@ -174,17 +174,14 @@ public final class BasicVector_UnsignedByte32 extends AbstractVector
@Override
public LispObject AREF(LispObject index) throws ConditionThrowable
{
final int idx = Fixnum.getValue(index);
try
{
return number(elements[((Fixnum)index).value]);
}
catch (ClassCastException e)
{
return type_error(index, Symbol.FIXNUM);
return number(elements[idx]);
}
catch (ArrayIndexOutOfBoundsException e)
{
badIndex(Fixnum.getValue(index), elements.length);
badIndex(idx, elements.length);
return NIL; // Not reached.
}
}
......
......@@ -159,17 +159,14 @@ public final class BasicVector_UnsignedByte8 extends AbstractVector
@Override
public LispObject AREF(LispObject index) throws ConditionThrowable
{
int idx = Fixnum.getValue(index);
try
{
return coerceJavaByteToLispObject(elements[((Fixnum)index).value]);
}
catch (ClassCastException e)
{
return error(new TypeError(index, Symbol.FIXNUM));
return coerceJavaByteToLispObject(elements[idx]);
}
catch (ArrayIndexOutOfBoundsException e)
{
badIndex(Fixnum.getValue(index), elements.length);
badIndex(idx, elements.length);
return NIL; // Not reached.
}
}
......
......@@ -316,16 +316,14 @@ public final class Bignum extends LispInteger
public static BigInteger getValue(LispObject obj) throws ConditionThrowable
{
try
if (obj instanceof Bignum)
{
return ((Bignum)obj).value;
}
catch (ClassCastException e)
{
type_error(obj, Symbol.BIGNUM);
// Not reached.
return null;
}
}
@Override
......
......@@ -257,7 +257,7 @@ public final class BroadcastStream extends Stream
@Override
public LispObject execute(LispObject arg) throws ConditionThrowable
{
try {
if (arg instanceof BroadcastStream) {
BroadcastStream stream = (BroadcastStream) arg;
Stream[] streams = stream.streams;
LispObject result = NIL;
......@@ -265,9 +265,7 @@ public final class BroadcastStream extends Stream
result = new Cons(streams[i], result);
return result;
}
catch (ClassCastException e) {
return error(new TypeError(arg, Symbol.BROADCAST_STREAM));
}
return error(new TypeError(arg, Symbol.BROADCAST_STREAM));
}
};
}
......@@ -117,12 +117,10 @@ public final class ByteArrayOutputStream extends Stream
@Override
public LispObject execute(LispObject arg) throws ConditionThrowable
{
try {
if (arg instanceof ByteArrayOutputStream) {
return JavaObject.getInstance(((ByteArrayOutputStream)arg).getByteArray());
}
catch (ClassCastException e) {
return error(new TypeError(this, Symbol.STREAM)); //TODO
}
return error(new TypeError(this, Symbol.STREAM)); //TODO
}
};
}
......@@ -55,17 +55,7 @@ public final class CharacterFunctions extends Lisp
public LispObject execute(LispObject first, LispObject second)
throws ConditionThrowable
{
try {
return ((LispCharacter)first).value == ((LispCharacter)second).value ? T : NIL;
}
catch (ClassCastException e) {
LispObject datum;
if (first instanceof LispCharacter)
datum = second;
else
datum = first;
return type_error(datum, Symbol.CHARACTER);
}
return LispCharacter.getValue(first) == LispCharacter.getValue(second) ? T : NIL;
}
@Override
public LispObject execute(LispObject[] array) throws ConditionThrowable
......@@ -101,18 +91,8 @@ public final class CharacterFunctions extends Lisp
throws ConditionThrowable
{
final char c1, c2;
try {
c1 = ((LispCharacter)first).value;
}
catch (ClassCastException e) {
return type_error(first, Symbol.CHARACTER);
}
try {
c2 = ((LispCharacter)second).value;
}
catch (ClassCastException e) {
return type_error(second, Symbol.CHARACTER);
}
c1 = LispCharacter.getValue(first);
c2 = LispCharacter.getValue(second);
if (c1 == c2)
return T;
if (LispCharacter.toUpperCase(c1) == LispCharacter.toUpperCase(c2))
......@@ -238,30 +218,15 @@ public final class CharacterFunctions extends Lisp
public LispObject execute(LispObject first, LispObject second)
throws ConditionThrowable
{
try {
return ((LispCharacter)first).value < ((LispCharacter)second).value ? T : NIL;
}
catch (ClassCastException e) {
LispObject datum;
if (first instanceof LispCharacter)
datum = second;
else
datum = first;
return type_error(datum, Symbol.CHARACTER);
}
}
return LispCharacter.getValue(first) < LispCharacter.getValue(second) ? T : NIL;
}
@Override
public LispObject execute(LispObject[] args) throws ConditionThrowable
{
final int length = args.length;
char[] chars = new char[length];
for (int i = 0; i < length; i++) {
try {
chars[i] = ((LispCharacter)args[i]).value;
}
catch (ClassCastException e) {
return type_error(args[i], Symbol.CHARACTER);
}
chars[i] = LispCharacter.getValue(args[i]);
}
for (int i = 1; i < length; i++) {
if (chars[i-1] >= chars[i])
......@@ -291,40 +256,18 @@ public final class CharacterFunctions extends Lisp
public LispObject execute(LispObject first, LispObject second)
throws ConditionThrowable
{
try {
return ((LispCharacter)first).value <= ((LispCharacter)second).value ? T : NIL;
}
catch (ClassCastException e) {
LispObject datum;
if (first instanceof LispCharacter)
datum = second;
else
datum = first;
return type_error(datum, Symbol.CHARACTER);
}
return LispCharacter.getValue(first) <= LispCharacter.getValue(second) ? T : NIL;
}
@Override
public LispObject execute(LispObject first, LispObject second,
LispObject third)
throws ConditionThrowable
{
try {
if (((LispCharacter)first).value > ((LispCharacter)second).value)
return NIL;
if (((LispCharacter)second).value > ((LispCharacter)third).value)
return NIL;
return T;
}
catch (ClassCastException e) {
LispObject datum;
if (!(first instanceof LispCharacter))
datum = first;
else if (!(second instanceof LispCharacter))
datum = second;
else
datum = third;
return type_error(datum, Symbol.CHARACTER);
}
if (LispCharacter.getValue(first) > LispCharacter.getValue(second))
return NIL;
if (LispCharacter.getValue(second) > LispCharacter.getValue(third))
return NIL;
return T;
}
@Override
public LispObject execute(LispObject[] args) throws ConditionThrowable
......@@ -332,12 +275,7 @@ public final class CharacterFunctions extends Lisp
final int length = args.length;
char[] chars = new char[length];
for (int i = 0; i < length; i++) {
try {
chars[i] = ((LispCharacter)args[i]).value;
}
catch (ClassCastException e) {
return type_error(args[i], Symbol.CHARACTER);
}
chars[i] = LispCharacter.getValue(args[i]);
}
for (int i = 1; i < length; i++) {
if (chars[i-1] > chars[i])
......
......@@ -137,11 +137,12 @@ public class Closure extends Function
"&REST/&BODY must be followed by a variable."));
}
Debug.assertTrue(restVar == null);
try
final LispObject remainingcar = remaining.car();
if (remainingcar instanceof Symbol)
{
restVar = (Symbol) remaining.car();
restVar = (Symbol) remainingcar;
}
catch (ClassCastException e)
else
{
error(new LispError(
"&REST/&BODY must be followed by a variable."));
......
......@@ -164,7 +164,7 @@ public final class ComplexBitVector extends AbstractBitVector
{
if (index < 0 || index >= capacity)
badIndex(index, capacity);
try {
if (newValue instanceof Fixnum) {
switch (((Fixnum)newValue).value) {
case 0:
if (bits != null) {
......@@ -182,9 +182,7 @@ public final class ComplexBitVector extends AbstractBitVector
return;
}
}
catch (ClassCastException e) {
// Fall through...
}
type_error(newValue, Symbol.BIT);
}
......
......@@ -157,14 +157,7 @@ public final class ComplexString extends AbstractString
for (int i = 0; i < capacity; i++)
{
LispObject obj = array.AREF(displacement + i);
try
{
copy[i] = ((LispCharacter)obj).value;
}
catch (ClassCastException e)
{
type_error(obj, Symbol.CHARACTER);
}
copy[i] = LispCharacter.getValue(obj);
}
}
else
......@@ -426,14 +419,7 @@ public final class ComplexString extends AbstractString
@Override
public void aset(int index, LispObject newValue) throws ConditionThrowable
{
try
{
setCharAt(index, ((LispCharacter)newValue).value);
}
catch (ClassCastException e)
{
type_error(newValue, Symbol.CHARACTER);
}
setCharAt(index, LispCharacter.getValue(newValue));
}
@Override
......@@ -449,14 +435,7 @@ public final class ComplexString extends AbstractString
}
if (chars != null)
{
try
{
chars[fillPointer] = ((LispCharacter)element).value;
}
catch (ClassCastException e)
{
type_error(element, Symbol.CHARACTER);
}
chars[fillPointer] = LispCharacter.getValue(element);
}
else
array.aset(fillPointer + displacement, element);
......@@ -486,14 +465,7 @@ public final class ComplexString extends AbstractString
}
if (chars != null)
{
try
{
chars[fillPointer] = ((LispCharacter)element).value;
}
catch (ClassCastException e)
{
type_error(element, Symbol.CHARACTER);
}
chars[fillPointer] = LispCharacter.getValue(element);
}
else
array.aset(fillPointer + displacement, element);
......
......@@ -273,12 +273,9 @@ public final class ConcatenatedStream extends Stream
@Override
public LispObject execute(LispObject arg) throws ConditionThrowable
{
try {
if (arg instanceof ConcatenatedStream)
return ((ConcatenatedStream)arg).streams;
}
catch (ClassCastException e) {
return error(new TypeError(arg, Symbol.CONCATENATED_STREAM));
}
return error(new TypeError(arg, Symbol.CONCATENATED_STREAM));
}
};
}
......@@ -281,20 +281,15 @@ public final class Cons extends LispObject
@Override
public final int length() throws ConditionThrowable
{
int length = 0;
LispObject obj = this;
try
{
int length = 1;
LispObject obj = cdr;
while (obj != NIL)
{
++length;
obj = ((Cons)obj).cdr;
}
}
catch (ClassCastException e)
{
type_error(obj, Symbol.LIST);
}
if (obj instanceof Cons) {
obj = ((Cons)obj).cdr;
} else type_error(obj, Symbol.LIST);
}
return length;
}
......@@ -320,12 +315,12 @@ public final class Cons extends LispObject
public LispObject NTH(LispObject arg) throws ConditionThrowable
{
int index;
try
if (arg instanceof Fixnum)
{
index = ((Fixnum)arg).value;
}
catch (ClassCastException e)
{
else
{
if (arg instanceof Bignum)
{
// FIXME (when machines have enough memory for it to matter)
......@@ -361,13 +356,14 @@ public final class Cons extends LispObject
{
if (i == index)
return cons.car;
try
LispObject conscdr = cons.cdr;
if (conscdr instanceof Cons)
{
cons = (Cons) cons.cdr;
cons = (Cons) conscdr;
}
catch (ClassCastException e)
else
{
if (cons.cdr == NIL)
if (conscdr == NIL)
{
// Index too large.
type_error(Fixnum.getInstance(index),
......@@ -377,7 +373,7 @@ public final class Cons extends LispObject
else
{
// Dotted list.
type_error(cons.cdr, Symbol.LIST);
type_error(conscdr, Symbol.LIST);
}
// Not reached.
return NIL;
......
......@@ -225,14 +225,11 @@ public final class DoubleFloat extends LispObject
public static double getValue(LispObject obj) throws ConditionThrowable
{
try {
if (obj instanceof DoubleFloat)
return ((DoubleFloat)obj).value;
}
catch (ClassCastException e) {
type_error(obj, Symbol.FLOAT);
// Not reached.
return 0;
}
}
public final double getValue()
......
......@@ -336,14 +336,7 @@ public final class Environment extends LispObject
@Override
public LispObject execute(LispObject arg) throws ConditionThrowable
{
try
{
return ((Environment)arg).isEmpty() ? T : NIL;
}
catch (ClassCastException e)
{
return type_error(arg, Symbol.ENVIRONMENT);
}
return checkEnvironment(arg).isEmpty() ? T : NIL;
}
};
......@@ -354,19 +347,12 @@ public final class Environment extends LispObject
@Override
public LispObject execute(LispObject arg) throws ConditionThrowable
{
try
{
Environment env = (Environment) arg;
Environment env = checkEnvironment(arg);
LispObject result = NIL;
for (Binding binding = env.vars; binding != null; binding = binding.next)
if (!binding.specialp)
result = result.push(new Cons(binding.symbol, binding.value));
return result.nreverse();
}
catch (ClassCastException e)
{
return type_error(arg, Symbol.ENVIRONMENT);
}
}
};
}
......@@ -274,12 +274,10 @@ public final class Extensions extends Lisp
public LispObject execute(LispObject arg) throws ConditionThrowable
{
AbstractString string;
try {
if (arg instanceof AbstractString) {
string = (AbstractString) arg;
}
catch (ClassCastException e) {
} else
return type_error(arg, Symbol.STRING);
}
String result = System.getenv(string.getStringValue());
if (result != null)
return new SimpleString(result);
......
......@@ -285,19 +285,13 @@ public final class FileStream extends Stream
throws ConditionThrowable
{
final Pathname pathname;
try {
if(first instanceof Pathname) {
pathname = (Pathname) first;
}
catch (ClassCastException e) {
else {
return type_error(first, Symbol.PATHNAME);
}
final LispObject namestring;
try {
namestring = (AbstractString) second;
}
catch (ClassCastException e) {
return type_error(second, Symbol.STRING);
}
final LispObject namestring = checkString(second);
LispObject elementType = third;
LispObject direction = fourth;
LispObject ifExists = fifth;
......
......@@ -299,16 +299,10 @@ public final class Fixnum extends LispInteger
public static int getValue(LispObject obj) throws ConditionThrowable
{
try
{
return ((Fixnum)obj).value;
}
catch (ClassCastException e)
{
type_error(obj, Symbol.FIXNUM);
// Not reached.
return 0;
}
if (obj instanceof Fixnum) return ((Fixnum)obj).value;
type_error(obj, Symbol.FIXNUM);
// Not reached.
return 0;
}
@Override
......@@ -323,30 +317,18 @@ public final class Fixnum extends LispInteger
public static int getInt(LispObject obj) throws ConditionThrowable
{
try
{
return (int) ((Fixnum)obj).value;
}
catch (ClassCastException e)
{
type_error(obj, Symbol.FIXNUM);
// Not reached.
return 0;
}
if (obj instanceof Fixnum) return ((Fixnum)obj).value;
type_error(obj, Symbol.FIXNUM);
// Not reached.
return 0;
}
public static BigInteger getBigInteger(LispObject obj) throws ConditionThrowable
{
try
{
return BigInteger.valueOf(((Fixnum)obj).value);
}
catch (ClassCastException e)
{
type_error(obj, Symbol.FIXNUM);
// Not reached.
return null;
}
if (obj instanceof Fixnum) return BigInteger.valueOf(((Fixnum)obj).value);
type_error(obj, Symbol.FIXNUM);
// Not reached.
return null;
}
@Override
......
......@@ -82,16 +82,14 @@ public class ForwardReferencedClass extends LispClass
public LispObject execute(LispObject arg)
throws ConditionThrowable
{
try {
if (arg instanceof Symbol) {
Symbol name = (Symbol) arg;
ForwardReferencedClass c = new ForwardReferencedClass(name);
LispClass.addClass(name, c);
return c;
}
catch (ClassCastException e) {
return error(new TypeError(arg.writeToString() +
" is not a valid class name."));
}
}
};
}
......@@ -53,15 +53,7 @@ public final class HashTableFunctions extends Lisp
LispObject rehashSize, LispObject rehashThreshold)
throws ConditionThrowable
{
final int n;
try
{
n = ((Fixnum)size).value;
}
catch (ClassCastException e)
{
return type_error(size, Symbol.FIXNUM);
}