Slide 1

Slide 1 text

RPython A Step Towards Reconciling Dynamically and Statically Typed OO Languages Antonio Cuni – DISI, Universit` a degli Studi di Genova joint work with D. Ancona, M. Ancona, N. D. Matsakis DLS’07 OOPSLA Montreal CA October 22, 2007 Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 1 / 26

Slide 2

Slide 2 text

Outline 1 Introduction to RPython 2 RPython idioms 3 Implementation notes and benchmarks 4 Conclusions Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 2 / 26

Slide 3

Slide 3 text

Dynamic languages for .NET and JVM .NET and JVM: widespread platforms Designed for static languages Great Python implementations: IronPython, Jython Much slower than e.g. C# and Java Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 3 / 26

Slide 4

Slide 4 text

Dynamic vs. static Dynamic languages Flexibility Rapid development cycle Metaprogramming Static languages Speed Nothing more :-) Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 4 / 26

Slide 5

Slide 5 text

Dynamic vs. static Dynamic languages Flexibility Rapid development cycle Metaprogramming Static languages Speed Nothing more :-) Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 4 / 26

Slide 6

Slide 6 text

RPython Quick Facts Restricted subset of Python Statically typed (type inference) Still allows metaprogramming RPython programs still run under {C,J,Iron}Python Three backends: C, .NET, JVM Almost as fast as C, C#, Java Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 5 / 26

Slide 7

Slide 7 text

Type inference Top-down, starting from an entry point; whole program analysis Assign the most precise type to each variable Fail if you try to mix incompatible types RPython def main (): print add(40, 2) def add(a, b): return a+b Not RPython def fn(flag ): if flag: return 42 else: return ’hello ’ Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 7 / 26

Slide 8

Slide 8 text

Type inference Top-down, starting from an entry point; whole program analysis Assign the most precise type to each variable Fail if you try to mix incompatible types RPython def main (): print add(40, 2) def add(a, b): return a+b Not RPython def fn(flag ): if flag: return 42 else: return ’hello ’ Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 7 / 26

Slide 9

Slide 9 text

Type inference Top-down, starting from an entry point; whole program analysis Assign the most precise type to each variable Fail if you try to mix incompatible types RPython def main (): print add(40, 2) def add(a, b): return a+b Not RPython def fn(flag ): if flag: return 42 else: return ’hello ’ Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 7 / 26

Slide 10

Slide 10 text

Other restrictions Globals are assumed to be constant yield and generators not supported No special methods (except init and del ) No run-time definition of new functions and classes Cannot modify classes at run-time Cannot change the class of an object Single inheritance, with limited support for mixins Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 8 / 26

Slide 11

Slide 11 text

Still pythonic, though No syntactic restriction Functions and classes are first-order values Exceptions work Lists and dictionaries Work, but they must be homogeneous list of int, dict from string to floats, etc. are OK list of int and strings is not Most of methods of list, dict and str are supported Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 9 / 26

Slide 12

Slide 12 text

Still pythonic, though No syntactic restriction Functions and classes are first-order values Exceptions work Lists and dictionaries Work, but they must be homogeneous list of int, dict from string to floats, etc. are OK list of int and strings is not Most of methods of list, dict and str are supported Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 9 / 26

Slide 13

Slide 13 text

Init-time, translation-time, run-time Translation time -- RPython Init time Full Python Python interpreter Live objects Translation toolchain Executable *.py Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 10 / 26

Slide 14

Slide 14 text

Metaprogramming RPython restrictions only apply to live objects No restrictions about how they are created Full Python is allowed at init-time Python as a metaprogramming language for RPython Code generation considered harmful Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 11 / 26

Slide 15

Slide 15 text

Outline 1 Introduction to RPython 2 RPython idioms 3 Implementation notes and benchmarks 4 Conclusions Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 12 / 26

Slide 16

Slide 16 text

Compute complex constants Fibonacci’s numbers def fibo(N): sequence = [] a, b = 1, 1 for i in xrange(N): sequence.append(a) a, b = b, a+b return sequence # computed at init -time fibo_numbers = fibo (100) Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 14 / 26

Slide 17

Slide 17 text

Metaclasses run at init-time extend metaclass class MyClass(object ): def foo(self ): ... class __extend__(MyClass ): def bar(self ): ... def main (): obj = MyClass () obj.bar() Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 16 / 26

Slide 18

Slide 18 text

Dynamic classes/functions at init-time “Static” nested scopes work def make_adder(N): def add(x): return x+N return add add10 = make_adder (10) add20 = make_adder (20) def main (): print add10 (32) print add20 (22) Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 18 / 26

Slide 19

Slide 19 text

Outline 1 Introduction to RPython 2 RPython idioms 3 Implementation notes and benchmarks 4 Conclusions Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 19 / 26

Slide 20

Slide 20 text

The Translation Toolchain CPython: *.py --> Python bytecode FlowObjSpace: bytecode --> flow graphs Annotator: type inference on flow graphs High level Python types (List(Integer)) RTyper: high level types --> low level types lltype for C, ootype for CLI and JVM Backends: code generation C, CLI (.NET), JVM Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 20 / 26

Slide 21

Slide 21 text

Benchmarks Classic Martin Richard’s test Available in Java, C#, RPython Language Result Factor Results on Microsoft CLR C# 6.94 ms 1.00x RPython 7.25 ms 1.04x IronPython 1675.00 ms 241.35x Results on JVM Java 1.77 ms 1.00x RPython 2.10 ms 1.18x Jython 2918.90 ms 1641.80x Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 21 / 26

Slide 22

Slide 22 text

Outline 1 Introduction to RPython 2 RPython idioms 3 Implementation notes and benchmarks 4 Conclusions Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 22 / 26

Slide 23

Slide 23 text

What’s good about RPython Pythonic enough to be usable Very fast Testable under CPython Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 23 / 26

Slide 24

Slide 24 text

Things to improve Originally an implementation detail Not designed to be user-friendly; terse error messages Lack of documentation/reference manual Lack of separate compilation Integration with the hosting platform Good for C/Posix Proof of concept for .NET Doesn’t exist for JVM Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 24 / 26

Slide 25

Slide 25 text

About PyPy Python in (R)Python High level interpreter written in RPython Easy to understand Easy to extend Translation Toolchain Written in full Python Works as a general compiler Especially for interpreters (e.g. Javascript, Prolog) Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 25 / 26

Slide 26

Slide 26 text

About PyPy Python in (R)Python High level interpreter written in RPython Easy to understand Easy to extend Translation Toolchain Written in full Python Works as a general compiler Especially for interpreters (e.g. Javascript, Prolog) Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 25 / 26

Slide 27

Slide 27 text

Acknowledgments The whole PyPy Team RPython is not mine :-) Our contributions: CLI and JVM backends Thanks for the attention Any question? Antonio Cuni (DSL’07 OOPSLA) RPython, a dynamic static language October 22, 2007 26 / 26