Upload
kai-aras
View
3.915
Download
0
Embed Size (px)
Citation preview
PyPy
Compiling Dynamic Language Implementations
The Python Situation
The Python Situation
• lots of implementationso CPython - Posixo Jython - JVM o IronPython - .NET
• specialized projectso Stackless Pythono unladen-swallowo Psyco
What PyPy is
1.Python Implementation2.Translation Framework
What PyPy is
1. Python implemented in Python
What PyPy is
1. Python implemented in Python
• looks like this:
$> python py.py
What PyPy is
1. Python implemented in Python
• looks like this:
• runs on top of CPython
$> python py.py
What PyPy is
1. Python implemented in Python
• looks like this:
• runs on top of CPython• about 2000 times slower than CPython
$> python py.py
What PyPy is
2. Translation Framework
$> python py.py
$> ./pypy-c $> ./pypy-jvm$> ./pypy-cli
some PyPy facts
• founded in 2003 by Holger Krekel and Armin Rigo• 2004 - 2007 EU Project - Sixth Framework• since 2007 Open Source Project• occasional Funding by Google• Sprint-Driven Development
PyPy Components
1.Architecture2.RPython3.Interpreter 4.Translation Framework
PyPy componentsArchitektur
PyPy componentsRPython• RPython = Restricted/Reduced Python
PyPy componentsRPython• RPython = Restricted/Reduced Python
o Restricted = most possible static subset of Python
PyPy componentsRPython• RPython = Restricted/Reduced Python
o Restricted = most possible static subset of Python o required to perform Type Inference
PyPy componentsRPython• RPython = Restricted/Reduced Python
o Restricted = most possible static subset of Python o required to perform Type Inference o input to the Translation Framework
PyPy componentsRPython• RPython = Restricted/Reduced Python
o Restricted = most possible static subset of Python o required to perform Type Inference o input to the Translation Frameworko no real specification - just some hints
PyPy componentsRPython• RPython = Restricted/Reduced Python
o Restricted = most possible static subset of Python o required to perform Type Inference o input to the Translation Frameworko no real specification - just some hintso used for writing interpreters
PyPy componentsRPython• RPython = Restricted/Reduced Python
o Restricted = most possible static subset of Python o required to perform Type Inference o input to the Translation Frameworko no real specification - just some hintso used for writing interpreterso can be used for writing extensions as well
PyPy componentsInterpreter
PyPy componentsInterpreter• written in RPython
PyPy componentsInterpreter• written in RPython• Internals:
o bytecode compiler -> generates bytecode
PyPy componentsInterpreter• written in RPython• Internals:
o bytecode compiler -> generates bytecodeo bytecode evaluator -> interprets bytecode
PyPy componentsInterpreter• written in RPython• Internals:
o bytecode compiler -> generates bytecodeo bytecode evaluator -> interprets bytecode o object space -> handles operations on objects
Python Interpreter
PyPy componentsTranslation Framework
PyPy componentsTranslation Framework
PyPy componentsTranslation Framework
PyPy componentsTranslation Framework
PyPy > CPython
1.High Level Language Implementation
2.JIT Generation3.Object Spaces4.Stackless
PyPy > CPythonHigh Level Language Implementation• easy to experiment with
PyPy > CPythonHigh Level Language Implementation• easy to experiment with • easy to implement new features
PyPy > CPythonHigh Level Language Implementation• easy to experiment with • easy to implement new features
o lazily computed objects and functions
PyPy > CPythonHigh Level Language Implementation• easy to experiment with • easy to implement new features
o lazily computed objects and functionso plug-able garbage-collection
PyPy > CPythonHigh Level Language Implementation• easy to experiment with • easy to implement new features
o lazily computed objects and functionso plug-able garbage-collection o runtime replacement of live-objects
PyPy > CPythonHigh Level Language Implementation• easy to experiment with • easy to implement new features
o lazily computed objects and functionso plug-able garbage-collection o runtime replacement of live-objectso stackless concurrency
PyPy > CPythonJIT Generation• based on Partial Evaluation via
Yoshihiko Futamura (1971). "Partial Evaluation of Computation Process – An Approach to a Compiler-Compiler"
PyPy > CPythonJIT Generation• based on Partial Evaluation via
Yoshihiko Futamura (1971). "Partial Evaluation of Computation Process – An Approach to a Compiler-Compiler"
• says: it's possible to take an interpreter and transform it into a compiler
PyPy > CPythonJIT Generation• based on Partial Evaluation via
Yoshihiko Futamura (1971). "Partial Evaluation of Computation Process – An Approach to a Compiler-Compiler"
• says: it's possible to take an interpreter and transform it into a compiler
• means:having such a thing is pretty cool because writing interpreters is a lot easier than writing compilers.
PyPy > CPythonJIT Generation
PyPy > CPythonObject Spaces• Flow ObjSpace• Thunk ObjSpace• Taint ObjSpace• Dump ObjSpace• Transparent Proxies
Thunk Object Space
• provides:1.lazily computed objects • lazily computed functions• globally replaceable objects
Thunk Object Space
• provides:1.lazily computed objects • lazily computed functions• globally replaceable objects
• is implemented in about 200 lines of code
Thunk Object Space
• provides:1.lazily computed objects • lazily computed functions• globally replaceable objects
• is implemented in about 200 lines of code
• Example:
>>>> a = "hello">>>> b = "world">>>> a + b'helloworld'
>>>> become(a,b)>>>> a + b'worldworld'
Taint Object Space
• provides protection for:o sensitive data that should not leak
Taint Object Space
• provides protection for:o sensitive data that should not leak
• provides protection from:o untrusted data that needs to be validated
Taint Object Space
• provides protection for:o sensitive data that should not leak
• provides protection from:o untrusted data that needs to be validated
• Example:>>>> password = "secret">>>> password'secret'
>>>> password = taint("secret")>>>> passwordTraceback (application-level): File "<inline>", line 1 in <interactive> passwordTaintError
PyPy > CPythonStackless• infinite Recursion
o CPython limited to depth=1000o regular PyPy limited to depth=5000
PyPy > CPythonStackless• infinite Recursion
o CPython limited to depth=1000o regular PyPy limited to depth=5000
• Microthreadso Coroutineso Tasklets and Channelso Greenlets
PyPy > CPythonStackless• infinite Recursion
o CPython limited to depth=1000o regular PyPy limited to depth=5000
• Microthreadso Coroutineso Tasklets and Channelso Greenlets
• Coroutine Pickling• Coroutine Cloning
Demo
Lazy functions and objectsTainted objectsTranslator Shell