JPyInterpreter Introduction

1. What is JPyInterpreter

JPyInterpreter is a Python interpreter that runs in the JVM that can be embedded in CPython to remove overhead of calling Python code frequently from Java. This is mostly relevant for small functions, where the overhead decreases performance by as much as 100 times.

It accomplishes this by creating Java class files from the CPython bytecode, accessible via the dis module. Both CPython Virtual Machine and the JVM are stack machines, meaning each opcode reads it parameters and writes its results to a stack. As such, JPyInterpreter can also be thought of as a translator of CPython bytecode to Java bytecode.

2. JPyInterpreter’s Structure

JPyInterpreter’s structure can be broken into several components:

  • The Python frontend, which is given Python function and classes and returns their corresponding Java versions. The Python frontend has minimal code, and is mostly responsible for passing the Python bytecode to Java and converting Python constants into Java constants.

  • The Java opcode parser, which is given Python bytecode instructions and needs to convert them to intermediary opcodes.

  • A typeflow analyzer, which is given the Python function signature and the intermediary opcodes, and returns the stack metadata associated with each opcode. The stack metadata is useful for optimizing opcodes, such as GET_ATTR (which typically need to go through a complicated dict lookup process, but if it is a known field, it can be implemented by a single getfield java bytecode instruction)

  • Implementors, which write the java bytecode instructions to implement a given intermediary opcode with the given stack metadata and function metadata.