Commits

Anonymous committed 18f7aaa Draft

Cache small integers (-100->899 for now) and use Py.newInteger to find them
Also use new Options class correctly
Also, fix py2char function for PyBuiltinFunctionSets

Comments (0)

Files changed (2)

org/python/core/Py.java

 
 	public static PyObject MemoryError;
 	public static void MemoryError(OutOfMemoryError t) {
-	    if (showjavaexc) {
+	    if (Options.showJavaExceptions) {
 	        t.printStackTrace();
 	    }
 	    System.err.println("Out of Memory");
     }
 
     /* Convenience methods to create new constants without using "new" */
-    public static PyInteger newInteger(int i) {
-        return new PyInteger(i);
+    private static PyInteger[] integerCache = null;
+    
+    
+    public static final PyInteger newInteger(int i) {
+        if (integerCache == null) {
+            integerCache = new PyInteger[1000];
+            for(int j=-100; j<900; j++) {
+                integerCache[j+100] = new PyInteger(j);
+            }
+        }
+        if (i>=-100 && i < 900) {
+            return integerCache[i+100];
+        } else {
+            return new PyInteger(i);
+        }
     }
     // Very bad behavior...
     public static PyInteger newInteger(long i) {
 		printException(t, f, null);
 	}
 
-    public static boolean showjavaexc=false;
 	public static synchronized void printException(Throwable t, PyFrame f, PyObject file) {
 	    //System.err.println("printingException: "+t+", "+file);
 	    StdoutWrapper stderr = Py.stderr;
 	        stderr = new FixedFileWrapper(file);
 	    }
 	    
-	    if (showjavaexc) {
+	    if (Options.showJavaExceptions) {
 	        stderr.println("Java Traceback:");
 	        java.io.CharArrayWriter buf = new java.io.CharArrayWriter();
 	        if (t instanceof PyException) {
 		if (i == null) throw Py.TypeError("float required");
 		return i.doubleValue();
 	}
+	
+	public static char py2char(PyObject o) {
+	    return py2char(o, "char required");
+	}	
 
-	public static char py2char(PyObject o) {
+	public static char py2char(PyObject o, String msg) {
 		if (o instanceof PyString) {
 			PyString s = (PyString)o;
-			if (s.__len__() != 1) throw Py.TypeError("char expected");
+			if (s.__len__() != 1) throw Py.TypeError(msg);
 			return s.toString().charAt(0);
 		}
 		if (o instanceof PyInteger) {
 		}
 
 		Character i = (Character)o.__tojava__(Character.TYPE);
-		if (i == null) throw Py.TypeError("character required");
+		if (i == null) throw Py.TypeError(msg);
 		return i.charValue();
 	}
 	
     public static PyObject[] unpackSequence(PyObject o, int length) {
         if (o instanceof PyTuple) {
             PyTuple tup = (PyTuple)o;
-            int l = tup.__len__();
+            //System.err.println("unpack tuple");
             if (tup.__len__() == length) return tup.list;
             throw Py.ValueError("unpack tuple of wrong size");
         }

org/python/core/PyInteger.java

 		int x = a + b;
 		if ((x^a) < 0 && (x^b) < 0)
 			throw Py.OverflowError("integer addition: "+this+" + "+right);
-		return new PyInteger(x);
+		return Py.newInteger(x); //new PyInteger(x);
 	}
 
 	public PyObject __sub__(PyObject right) {
 		int x = a - b;
 		if ((x^a) < 0 && (x^~b) < 0)
 			throw Py.OverflowError("integer subtraction: "+this+" - "+right);
-		return new PyInteger(x);
+		return Py.newInteger(x);
 	}
 
 	public PyObject __mul__(PyObject right) {
 		
 		if (x > Integer.MAX_VALUE || x < Integer.MIN_VALUE)
 			throw Py.OverflowError("integer multiplication: "+this+" * "+right);
-		return new PyInteger((int)x);
+		return Py.newInteger((int)x);
 	}
 
     // Getting signs correct for integer division
 	}
 
 	public PyObject __div__(PyObject right) {
-		return new PyInteger(divide(value, ((PyInteger)right).value));
+		return Py.newInteger(divide(value, ((PyInteger)right).value));
 	}
 
 	private int modulo(int x, int y, int xdivy) {
 
 	public PyObject __mod__(PyObject right) {
 		int x = ((PyInteger)right).value;
-		return new PyInteger(modulo(value, x, divide(value, x)));
+		return Py.newInteger(modulo(value, x, divide(value, x)));
 	}
 
 	public PyObject __divmod__(PyObject right) {
 	    if (mod != 0) {
 	        ret = modulo(ret, mod, divide(ret, mod));
 	    }
-	    return new PyInteger(ret);
+	    return Py.newInteger(ret);
 	}
 
 	public PyObject __lshift__(PyObject right) {
         int shift = ((PyInteger)right).value;
         if (shift > 31) return new PyInteger(0);
-		return new PyInteger(value << shift);
+		return Py.newInteger(value << shift);
 	}
 
 	public PyObject __rshift__(PyObject right) {
-		return new PyInteger(value >>> ((PyInteger)right).value);
+		return Py.newInteger(value >>> ((PyInteger)right).value);
 	}
 
 	public PyObject __and__(PyObject right) {
-		return new PyInteger(value & ((PyInteger)right).value);
+		return Py.newInteger(value & ((PyInteger)right).value);
 	}
 
 	public PyObject __xor__(PyObject right) {
-		return new PyInteger(value ^ ((PyInteger)right).value);
+		return Py.newInteger(value ^ ((PyInteger)right).value);
 	}
 
 	public PyObject __or__(PyObject right) {
-		return new PyInteger(value | ((PyInteger)right).value);
+		return Py.newInteger(value | ((PyInteger)right).value);
 	}
 
 	public PyObject __neg__() {
 		int x = -value;
 		if (value < 0 && x < 0)
 			throw Py.OverflowError("integer negation");
-		return new PyInteger(x);
+		return Py.newInteger(x);
 	}
 
 	public PyObject __pos__() {
 	}
 
 	public PyObject __invert__() {
-		return new PyInteger(~value);
+		return Py.newInteger(~value);
 	}
 
 	public PyInteger __int__() {