Commits

Shashank Bharadwaj  committed c7e35da

version for todays talk - near complete call-site part

  • Participants
  • Parent commits 0bdcbea

Comments (0)

Files changed (1)

File jvmlangsummit-2011/slides.rst

 =======
 
 * What is Jython?
-* What is Invokedyanmic?
 * Call-site specialization
-
-  - Global Function Lookup
-  - Jython Calling Convention
-  - Optimizing Built-in Functions
-
 * Iterator Expression Transformation
 
 
 =======
 
 * **What is Jython?**
-* What is Invokedyanmic?
 * Call-site specialization
-
-  - Global Function Lookup
-  - Jython Calling Convention
-  - Optimizing Built-in Functions
-
 * Iterator Expression Transformation
 
 
        * improve performance by using invokedynamic
 
 
-Outline
-=======
-
-* What is Jython?
-* **What is Invokedyanmic?**
-* Call-site specialization
-
-  - Global Function Lookup
-  - Jython Calling Convention
-  - Optimizing Built-in Functions
-
-* Iterator Expression Transformation
-
-
-
-What is Invokedynamic?
-======================
-
-Two parts:
-
-    * The ``invokedyanmic`` byte code
-    * The package ``java.lang.invoke``
-
-* Developed as per JSR 292
-* Opportunity for the language to be involved in the dispatch process
-
-
 How is Python compiled to Java?
 ===============================
 
       print a + b + c
 
 
-Super-class of all the runtime Python Objects - ``PyObject``
+
+
+.. raw:: pdf
+
+   PageBreak
+
+
 
 Using ``PyObject`` as the root-type
 -----------------------------------
 
    PageBreak
 
-
-
 Using ``PyObject`` as the root-type
 -----------------------------------
 
 Will be compiled to:
 
 .. code-block:: java
-   
-   PyObject a, b, c;
-   PyObject tmp = a._add(b)._add(c);
-   Py.println(tmp);
-
-.. raw:: pdf
-
-   PageBreak
-
-Using ``PyObject`` as the root-type
------------------------------------
-
-The statement:
-
-.. code-block:: python
-   def f(a, b, c):
-      print a + b + c
-
-Will be compiled to:
-
-.. code-block:: java
 
    ... {
       PyObject a, b, c;
 
    PageBreak
 
-..
-Also, global function lookup is costly
---------------------------------------
-
-..
-.. image:: images/getglobal.png
-
-..
-.. raw:: pdf
-
-   PageBreak
-
-
-
-
-Use ``indy`` to put arguments on stack [1]
-------------------------------------------
-
-.. code-block:: java
-
-   public PyObject f(PyFrame frame, ThreadState ts){
-   	  return _f(frame, ts, frame.getlocal(0), 
-	  	      frame.getlocal(1), frame.getlocal(2));
-   }
-
-   public PyObject _f(PyFrame frame, ThreadState ts,
-   	  	       PyObject a, PyObject b, PyObject c){
-       PyObject localPyObj = a._add(b)._add(c);
-       Py.println(localPyObj);
-   }
-
-
-.. raw:: pdf
-
-   PageBreak
-
-
-Use ``indy`` to put arguments on stack [2]
-------------------------------------------
-
-If the call-site has vanilla argument passing:
-
-.. code-block:: python
-
-   f(1, 2, 3)
-
-We can transform that into and indy dispatch:
-
-.. code-block:: java
-
-    public static CallSite bootstrap(Lookup caller, String name, 
-    	   	  	             MethodType mType, Object className){
-        MethodHandle mHandle;
-        try {
-	    Class clazz = Class.forName(className);
-            mHandle = caller.findVirtual(clazz, name, type);
-        } catch (NoSuchMethodException e) {
-            mHandle = caller.findVirtual(SpecializeCallSite.class, "fallback", type);
-        }
-    }
-
-.. XXX: Talk about keyword arguments? 
-        Should probably include it after it's working
-
-.. raw:: pdf
-
-   PageBreak
-
-
-Results
--------
-
-Both the optimizations combined provide:
-
-* **1.5x** performance improvement on micro-benchmarks
-* **xx%** improvement in the benchmark suite
-
-
-Outline
-=======
-
-* What is Jython?
-* What is Invokedyanmic?
-* **Call-site specialization**
-
-  - Global Function Lookup
-  - Jython Calling Convention
-  - Optimizing Built-in Functions
-
-* Iterator Expression Transformation
-
-Call-site Specialization
-========================
-
-.. code-block:: python
-
-   def foo(...):
-       pass
-
-   # callsite
-   foo(...)
-
-
-Outline
-=======
-
-* What is Jython?
-* What is Invokedyanmic?
-* Call-site specialization
-
-  - **Global Function Lookup**
-  - Jython Calling Convention
-  - Optimizing Built-in Functions
-
-* Iterator Expression Transformation
-
-Calls to Global Functions are Slow
-==================================
-
-.. code-block:: python
-
-   def foo(...):
-       pass
-
-   # callsite
-   foo(...)
-
-In Java:
-
-.. code-block:: java
-
-   public PyObject foo(PyFrame frame, ThreadState ts){
-   }
-
-   // callsite
-   frame.getglobal("foo").__call__(...);
-   // After some indirection, foo(..) is called
 
 Jython cannot statically bind, because
 --------------------------------------
    code_string = "print 'Hi'"
    code = compile(code_string, "<string>", "exec")
    
-   foo()	 # prints 'Hello World'
+   foo()         # prints 'Hello World'
    foo.func_code = code
-   foo()	 # prints 'Hi'
+   foo()         # prints 'Hi'
 
 Will produce::
 
      Hi
 
 
+
 Using Invokedynamic provides the solution (1)
 ---------------------------------------------
 
+Create an function which takes in args on stack
+
+.. code-block:: java
+
+    // optimized version 
+    public PyObject _f(PyFrame frame, ThreadState ts, PyObject a1,
+   	  	      PyObject a2, PyObject a3){
+        // implementation
+    }		      
+
+    // wrapper function to support other types of argument passing
+    public PyObject f(PyFrame frame, ThreadState ts) {
+        _f(frame, ts, frame.getlocal(0),
+	              frame.getlocal(1),
+		      frame.getlocal(2));
+    }		      
+
+        
+
+.. raw:: pdf
+
+   PageBreak
+
+
+
+Using Invokedynamic provides the solution (2)
+---------------------------------------------
+
 A ``SwitchPoint`` with:
 
-+---------------------+---------------------------------------------------------+
-| Target	      | .. code-block:: java                                    |
-|                     |                                                         |
-|                     |    invoke_foo(PyFrame prevFrame, ThreadState ts){       |
-|                     |        PyFrame frame = setup(...);                      |
-|                     |        foo(frame, ts);                                  |
-|                     |    }                                                    |
-+---------------------+---------------------------------------------------------+
-| Fallback            | .. code-block:: java                                    |
-|                     |                                                         |
-|                     |    fallback(PyFrame prevFrame, ThreadState ts){         |
-|                     |        prevFrame.getglobal("foo").__call__(...);        |
-|                     |    }                                                    |
-+---------------------+---------------------------------------------------------+
++---------------------+------------------------------------------------------------------------+
+| Target	      | .. code-block:: java                                                   |
+|                     |                                                        		       |
+|                     |    invoke_f(PyFrame prevFrame, ThreadState ts, PyObject a1,            |
+|                     |             PyObject a2, PyObject a3} {                       	       |
+|                     |        PyFrame frame = setup(...);                     	    	       |
+|                     |        _f(frame, ts, a1, a2, a3);				       |
+|                     |    }                                                    	       |
++---------------------+------------------------------------------------------------------------+
+| Fallback            | .. code-block:: java						       |
+|                     |                                                          	       |
+|                     |    fallback(PyFrame prevFrame, ThreadState ts){         	       |
+|                     |        prevFrame.getglobal("foo").__call__(...);        	       |
+|                     |    }                                                    	       |
++---------------------+------------------------------------------------------------------------+
 
 invalidated when ``func_code`` is changed
 
 
    PageBreak
 
-Using Invokedynamic provides the solution (2)
+Using Invokedynamic provides the solution (3)
 ---------------------------------------------
 .. code-block:: python
 
-   # callsite
-   foo(...)
+   f(1, 2, 3)
 
 .. code-block:: java
 
-   public PyObject foo(PyFrame frame, ThreadState ts){ ... }
+   public PyObject _f(PyFrame frame, ThreadState ts, PyObject a1, 
+   	  	      PyObject a2, PyObject a3){ ... }
 
-   // callsite
-   invoke_foo(frame, ts);
+   // call-site
+   indy_dispatch(frame, ts, _1, _2, _3);
 
-   public PyObject invoke_foo(frame, ts){
-	// some setup
-	foo(frame, ts);
+   public PyObject invoke_f(PyFrame prevFrame, ThreadState ts, PyObject a1, 
+   	  	   	    PyObject a2, PyObject a3) {
+	// setup the frame
+	_f(frame, ts, a1, a2, a3);
    }
 
+
+.. raw:: pdf
+
+   PageBreak
+
+
+Results
+-------
+
+* **4x** improvement on micro-benchmarks
+* Up-to xx% improvement on the benchmark suite
+
+.. raw:: pdf
+
+   PageBreak
+
+
+Future
+------
+
+* Extend the calling convention change to keyword arguments
+* Support annotations in Python like ``@frameless``
+
+.. code-block:: python
+
+   @frameless
+   def f(a, b, c):
+      print a, b, c
+
 Outline
 =======
 
 * What is Jython?
-* What is Invokedyanmic?
 * Call-site specialization
-
-  - Global Function Lookup
-  - **Jython Calling Convention**
-  - Optimizing Built-in Functions
-
-* Iterator Expression Transformation
-
-
-Jython Calling Convention
-=========================
-
-* All the arguments are pushed onto the frame
-* For supporting keyword args, star args and var args
-
-Example
--------
-
-.. code-block:: python
-
-   def foo(arg0, arg1, arg2):
-       pass
-
-   # callsite
-   foo(6, 4, 2)
-
-
-.. raw:: pdf
-
-   PageBreak
-
-Current Implementation: works, but slow
----------------------------------------
-
-.. code-block:: java
-
-   public PyObject foo(PyFrame frame, ThreadState ts){...}
-
-At the call-site:
-
-+----------+--------------------+
-| .. code-block:: java          |
-|                               |
-|     frame = new PyFrame();    |
-+==========+====================+
-| arg0     | 6                  |
-+----------+--------------------+
-| arg1     | 4                  |
-+----------+--------------------+
-| arg2     | 2                  |
-+----------+--------------------+
-
-.. code-block:: java
-   
-   foo(frame, threadstate);	// call-site
-
-
-
-Outline
-=======
-
-* What is Jython?
-* What is Invokedyanmic?
-* Call-site specialization
-
-  - Global Function Lookup
-  - Jython Calling Convention
-  - **Optimizing Built-in Functions**
-
-* Iterator Expression Transformation
-
-
-Optimizing Built-in Functions
-=============================
-
-Python provides rich set of built-in functions, implemented in Java in Jython
-
-.. code-block:: python
-
-   print min(4, 2)
-
-   def min(one, two):
-       return one + two
-
-   print min(4, 2)
-
-
-Will produce::
-
-     2
-     6
-
-
-.. raw:: pdf
-
-   PageBreak
-
-
-Current Implementation: works, but ...
---------------------------------------
-
-.. You might want to add that _4 = new PyInteger(4); and _2 = new PyInteger(2);
-
-+-------------------------+------------------------------------------------------+
-| Python                  | Java                                                 |
-+=========================+======================================================+
-| .. code-block:: python  | .. code-block:: java                                 |
-|                         |                                                      |
-|    print min(4, 2)      |    frame.getglobal("min").__call__(4, 2)             |
-|                         |        // the above line will call the following:    |
-|                         |        MinFuction.min(new PyObject[] {arg0, arg1})   |
-|                         |                                                      |
-|    def min(one, two):   |    // define how the function should behave          |
-|        return one + two |    local = new PyFunction(...)                       |
-|                         |    frame.setglobal("min", local)                     |
-|                         |                                                      |
-|    print min(4, 2)      |    frame.getglobal("min").__call__(4, 2)             |
-+-------------------------+------------------------------------------------------+
-
-* But we know ``min`` is part of Python Built-in Functions 
-
-.. raw:: pdf
-
-   PageBreak
-
-
-Using Invokedynamic provides a solution (1)
--------------------------------------------
-
-We setup a ``guardWithTest`` with:
-
-+---------------------+-----------------------------------------------------------------------+
-| Test   	      | .. code-block:: java                                                  |
-|                     |                                                                       |
-|                     |    public static boolean test(PyObject self) {                        |
-|                     |        if (self.getClass() != MinFunction.class) {                    |
-|                     |            return false;                                              |
-|                     |        }                                                              |
-|                     |        return true;                                                   |
-|                     |    }                                                                  |
-+---------------------+-----------------------------------------------------------------------+
-| Target	      | .. code-block:: java                                                  |
-|                     |                                                                       |
-|                     |    public static min(PyObject self, PyObject arg0, PyObject arg1){    |
-|                     |        // implementation                                              |
-|                     |    }                                                                  |
-+---------------------+-----------------------------------------------------------------------+
-
-``fallback`` will call ``__call__`` as before
-
-
-
-.. raw:: pdf
-
-   PageBreak
-
-Using Invokedynamic provides a solution (2)
--------------------------------------------
-
-.. You might want to add that _4 = new PyInteger(4); and _2 = new PyInteger(2);
-
-+-------------------------+------------------------------------------------------+
-| Python                  | Java                                                 |
-+=========================+======================================================+
-| .. code-block:: python  | .. code-block:: java                                 |
-|                         |                                                      |
-|    print min(4, 2)      |    // An indy call with `test` returning `true`:     |
-|                         |    MinFuction.min(arg0, arg1)                        |
-|                         |    // not MinFuction.min(new PyObject[] {arg0, arg1})|
-|                         |                                                      |
-|    def min(one, two):   |    // define how the function should behave          |
-|        return one + two |    local = new PyFunction(...)                       |
-|                         |    frame.setglobal("min", local)                     |
-|                         |                                                      |
-|                         |    // An indy call with `test` returning `false`:    |
-|    print min(4, 2)      |    frame.getglobal("min").__call__(ts, frame, 4, 2)  |
-+-------------------------+------------------------------------------------------+
-
-
-.. raw:: pdf
-
-   PageBreak
-
-
-Results
--------
-
-* **4x** improvement on microbenchmarks
-* Upto xx% improvement on the benchmark suite
-
-.. raw:: pdf
-
-   PageBreak
-
-
-Outline
-=======
-
-* What is Jython?
-* What is Invokedyanmic?
-* Call-site specialization
-
-  - Global Function Lookup
-  - Jython Calling Convention
-  - Optimizing Built-in Functions
-
 * **Iterator Expression Transformation**
 
 
 Results
 -------
 
-* **2x** to **10x** improvement on microbenchmarks
-* Upto 10% improvement on the benchmark suite
+* **2x** to **10x** improvement on micro-benchmarks
+* Up-to 10% improvement on the benchmark suite
 
-Future Plans
-============
-
-* ``@frameless`` annotation
-* add support for keyword arguments in the Jython calling convention	
 
 Questions
 =========