Commits

Anonymous committed f0be1ca Draft

Optimize function calls with 0-3 arguments
(no creation of PyObject[]'s in this case, also simpler arg processing)

  • Participants
  • Parent commits 8592bb7

Comments (0)

Files changed (4)

File org/python/core/PyCode.java

+package org.python.core;
+
+public abstract class PyCode extends PyObject {
+    public PyCode(PyClass c) { super(c); }
+	public String co_name;
+
+	abstract public PyObject call(PyFrame frame);
+	abstract public PyObject call(PyObject args[], String keywords[], 
+	        PyObject globals, PyObject[] defaults);
+	abstract public PyObject call(PyObject self, PyObject args[], String keywords[], 
+	        PyObject globals, PyObject[] defaults);
+	        
+   	abstract public PyObject call(PyObject globals, PyObject[] defaults);
+   	abstract public PyObject call(PyObject arg1, PyObject globals, PyObject[] defaults);
+   	abstract public PyObject call(PyObject arg1, PyObject arg2, PyObject globals, PyObject[] defaults);
+   	abstract public PyObject call(PyObject arg1, PyObject arg2, PyObject arg3, PyObject globals, PyObject[] defaults);
+	        
+}

File org/python/core/PyFunction.java

+package org.python.core;
+
+public class PyFunction extends PyObject {
+	public String __name__;
+	public PyObject __doc__;
+	public PyObject func_globals;
+	public PyObject[] func_defaults;
+	public PyCode func_code;
+
+    public static PyClass __class__;
+	public PyFunction(PyObject globals, PyObject[] defaults, PyCode code, PyObject doc) {
+        super(__class__);
+		func_globals = globals;
+		__name__ = code.co_name;
+        if (doc == null) __doc__ = Py.None;
+        else __doc__ = doc;
+		func_defaults = defaults;
+		func_code = code;
+	}
+	
+	public PyFunction(PyObject globals, PyObject[] defaults, PyCode code) {
+	    this(globals, defaults, code, null);
+	}
+	
+	public PyObject _doget(PyObject container) {
+	    return new PyMethod(container, this);
+	}
+	public PyObject __call__() {
+	    return func_code.call(func_globals, func_defaults);
+	}	
+	public PyObject __call__(PyObject arg) {
+	    return func_code.call(arg, func_globals, func_defaults);
+	}
+	public PyObject __call__(PyObject arg1, PyObject arg2) {
+	    return func_code.call(arg1, arg2, func_globals, func_defaults);
+	}
+	public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) {
+	    return func_code.call(arg1, arg2, arg3, func_globals, func_defaults);
+	}
+	
+	public PyObject __call__(PyObject[] args, String[] keywords) {
+		return func_code.call(args, keywords, func_globals, func_defaults);
+	}
+	public PyObject __call__(PyObject arg1, PyObject[] args, String[] keywords) {
+		return func_code.call(arg1, args, keywords, func_globals, func_defaults);
+	}
+
+	public String toString() {
+		return "<function "+__name__+" at "+hashCode()+">";
+	}
+}

File org/python/core/PyMethod.java

 	    return im_self != null && !(im_self instanceof PyClass);
 	}*/
 	
+	public PyObject __call__(PyObject arg1) {
+	    if (im_self != null) {
+	        return im_func.__call__(im_self, arg1);
+	    } else {
+	        return im_func.__call__(arg1);
+	    }
+	}
+	
 	public PyObject __call__(PyObject[] args, String[] keywords) {
 		if (im_self != null) {
 		    return im_func.__call__(im_self, args, keywords);

File org/python/core/PyTableCode.java

 		this.func_id = func_id;
 	}
 
-	public PyObject call(PyFrame frame) throws PyException {
+	public PyObject call(PyFrame frame) {
 	    //System.out.println("tablecode call: "+co_name);
 	    ThreadState ts = Py.getThreadState();
 
 		try {
 			ret = funcs.call_function(func_id, frame);
 		} catch (Throwable t) {
-
+            //t.printStackTrace();
 			//Convert exceptions that occured in Java code to PyExceptions
 			PyException e = Py.JavaError(t);
 
 	    ts.frame = ts.frame.f_back;
 		return ret;
    	}
-   	
-   	/*public PyObject call(PyObject arg, PyObject globals, PyObject[] defaults) {
-		if (co_argcount != 1) return call(new PyObject[] {arg}, Py.NoKeywords, globals, defaults);
+   	public PyObject call(PyObject globals, PyObject[] defaults) {
+		if (co_argcount != 0 || args || keywords) return call(Py.EmptyObjects, Py.NoKeywords, globals, defaults);
 		PyFrame frame = new PyFrame(this, globals);
-		frame.f_fastlocals[0] = arg;
+		return call(frame);
+	}   	
+   	public PyObject call(PyObject arg1, PyObject globals, PyObject[] defaults) {
+		if (co_argcount != 1 || args || keywords) return call(new PyObject[] {arg1}, Py.NoKeywords, globals, defaults);
+		PyFrame frame = new PyFrame(this, globals);
+		frame.f_fastlocals[0] = arg1;
 		return call(frame);
 	}
    	public PyObject call(PyObject arg1, PyObject arg2, PyObject globals, PyObject[] defaults) {
+		if (co_argcount != 2 || args || keywords) return call(new PyObject[] {arg1, arg2}, Py.NoKeywords, globals, defaults);
+		PyFrame frame = new PyFrame(this, globals);
+		frame.f_fastlocals[0] = arg1;
+		frame.f_fastlocals[1] = arg2;
+		return call(frame);
+	}	
+   	public PyObject call(PyObject arg1, PyObject arg2, PyObject arg3, PyObject globals, PyObject[] defaults) {
+		if (co_argcount != 2 || args || keywords) return call(new PyObject[] {arg1, arg2, arg3}, Py.NoKeywords, globals, defaults);
+		PyFrame frame = new PyFrame(this, globals);
+		frame.f_fastlocals[0] = arg1;
+		frame.f_fastlocals[1] = arg2;
+		frame.f_fastlocals[2] = arg3;
+		return call(frame);
+	}	
+	
+	/*
+   	public PyObject call(PyObject arg1, PyObject arg2, PyObject globals, PyObject[] defaults) {
 		if (co_argcount != 2) return call(new PyObject[] {arg1, arg2}, Py.NoKeywords, globals, defaults);
 		PyFrame frame = new PyFrame(this, globals);
 		frame.f_fastlocals[0] = arg1;