Use Node subclass to represent number literals to avoid creation of Double object.

This commit is contained in:
igor%mir2.org 2002-09-03 12:34:56 +00:00
Родитель 17ffc2f7c8
Коммит 2647231ce4
3 изменённых файлов: 31 добавлений и 37 удалений

Просмотреть файл

@ -121,7 +121,7 @@ public class IRFactory {
* Number (for literals)
*/
public Object createNumber(double number) {
return new Node(TokenStream.NUMBER, number);
return Node.newNumber(number);
}
/**
@ -596,8 +596,7 @@ public class IRFactory {
continue;
}
Node addelem = new Node(TokenStream.SETELEM, createUseTemp(temp),
new Node(TokenStream.NUMBER, i),
elem);
Node.newNumber(i), elem);
i++;
result = new Node(TokenStream.COMMA, result, addelem);
}
@ -625,11 +624,11 @@ public class IRFactory {
createUseTemp(temp),
new Node(TokenStream.STRING,
"length"),
new Node(TokenStream.NUMBER, i));
Node.newNumber(i));
result = new Node(TokenStream.COMMA, result, setlength);
}
} else {
array.addChildToBack(new Node(TokenStream.NUMBER, i));
array.addChildToBack(Node.newNumber(i));
}
return new Node(TokenStream.COMMA, result, createUseTemp(temp));
}

Просмотреть файл

@ -46,6 +46,16 @@ package org.mozilla.javascript;
public class Node implements Cloneable {
private static class NumberNode extends Node {
NumberNode(double number) {
super(TokenStream.NUMBER);
this.number = number;
}
double number;
}
public Node(int nodeType) {
type = nodeType;
}
@ -78,25 +88,6 @@ public class Node implements Cloneable {
this.datum = new Integer(value);
}
public Node(int nodeType, double value) {
type = nodeType;
int ivalue = (int)value;
if (ivalue == value) {
if ((byte)ivalue == ivalue) {
this.datum = new Byte((byte)ivalue);
}
else if ((short)ivalue == ivalue) {
this.datum = new Short((short)ivalue);
}
else {
this.datum = new Integer(ivalue);
}
}
else {
this.datum = new Double(value);
}
}
public Node(int nodeType, String str) {
type = nodeType;
this.datum = str;
@ -117,6 +108,10 @@ public class Node implements Cloneable {
this.datum = datum;
}
public static Node newNumber(double number) {
return new NumberNode(number);
}
public int getType() {
return type;
}
@ -417,12 +412,9 @@ public class Node implements Cloneable {
return ((Number) datum).intValue();
}
/** Can only be called when <tt>getType() == TokenStream.NUMBER</tt> */
public double getDouble() {
return ((Number) datum).doubleValue();
}
public long getLong() {
return ((Number) datum).longValue();
return ((NumberNode)this).number;
}
public String getString() {
@ -449,6 +441,9 @@ public class Node implements Cloneable {
if (type == TokenStream.TARGET) {
sb.append(' ');
sb.append(hashCode());
} else if (type == TokenStream.NUMBER) {
sb.append(' ');
sb.append(getDouble());
}
if (datum != null) {
sb.append(' ');

Просмотреть файл

@ -787,8 +787,8 @@ public class Optimizer {
// numerical addition and string concatenation
if(lt == TokenStream.NUMBER && rt == TokenStream.NUMBER) {
// num + num
replace = new Node(TokenStream.NUMBER,
lChild.getDouble() + rChild.getDouble());
replace = Node.
newNumber(lChild.getDouble() + rChild.getDouble());
}
else if (lt == TokenStream.STRING && rt == TokenStream.STRING) {
// string + string
@ -816,8 +816,8 @@ public class Optimizer {
// subtraction
if (lt == TokenStream.NUMBER && rt == TokenStream.NUMBER) {
//both numbers
replace = new Node(TokenStream.NUMBER,
lChild.getDouble() - rChild.getDouble());
replace = Node.
newNumber(lChild.getDouble() - rChild.getDouble());
}
else if (lt == TokenStream.NUMBER && lChild.getDouble() == 0) {
// first 0: 0-x -> -x
@ -836,8 +836,8 @@ public class Optimizer {
// multiplication
if (lt == TokenStream.NUMBER && rt == TokenStream.NUMBER) {
// both constants -- just multiply
replace = new Node(TokenStream.NUMBER,
lChild.getDouble() * rChild.getDouble());
replace = Node.
newNumber(lChild.getDouble() * rChild.getDouble());
}
else if (lt == TokenStream.NUMBER && lChild.getDouble() == 1) {
// first 1: 1*x -> +x
@ -858,8 +858,8 @@ public class Optimizer {
// division
if (lt == TokenStream.NUMBER && rt == TokenStream.NUMBER) {
// both constants -- just divide, trust Java to handle x/0
replace = new Node(TokenStream.NUMBER,
lChild.getDouble() / rChild.getDouble());
replace = Node.
newNumber(lChild.getDouble() / rChild.getDouble());
}
else if (rt == TokenStream.NUMBER && rChild.getDouble() == 1) {
// second 1: x/1 -> +x