SlideShare a Scribd company logo
Diving into Byte-code
    optimization in Python


            SciPy India,
    IIT Bombay Dec 05th 2011
Chetan Giridhar and Vishal Kanaujia
Fundamentals of Bytecode
• Python source code compiled into Python
  byte code by the CPython interpreter
• “.pyc”?
  – Automatic compilation : importing a module
  – Explicit compilation :
    py_compile.compile(“module.py”) – generates
    ‘module.pyc’
• The module ‘compileall’{}


                 Attribution-NonCommercial CC BY-NC
Fundamentals | more
• A program doesn't run any faster when it is
  read from a ‘.pyc’ file. But, why?
• “.pyc” for a script executed on the command
  line
• “.pyc” files – good enough to distribute your
  code, but with a caveat!



                 Attribution-NonCommercial CC BY-NC
Compilation phases
• Uses a ‘lexer’ to prepare tokens of Python
  code
• A parser arranges token according to the
  language grammar and prepares concrete
  syntax tree
• Concrete syntax tree is transformed in to AST
• AST is compiled to produce Byte-codes


                 Attribution-NonCommercial CC BY-NC
Python                                             Abstract
                      Parse Tree
source                                              Syntax
                      generated
 code                                                Tree
            Pgen.c
                                     ast.c


                                       compile.c




Executed
                      Optimized                    Bytecode
by Python
                      bytecode                     generated
   VM
            ceval.c                peephole.c
Python “ast” module
$ cat myast.py                          $python myast.py
                                        <generator object walk at 0xb784c8ec>
import ast
                                        Module(body=[Expr(value=BinOp
nod = ast.parse('a +2')                 (left=Name(id='a', ctx=Load()),
print ast.walk(nod)                     op=Add(),
                                         right=Num(n=2)))])
print ast.dump(nod)




         •Convenient for analysis, code transformations and generation
         •ASTs are compiled to code objects




                             Attribution-NonCommercial CC BY-NC
A peek into Bytecodes
  $ cat scipy.py                           $ python scipy.py
   1 import dis                             4    0 LOAD_CONST       1 (10)
                                                 3 STORE_FAST       0 (i)
   2
   3 def foo():                             5     6 LOAD_FAST       0 (i)
                                                 9 PRINT_ITEM
   4 i = 10                                      10 PRINT_NEWLINE
   5 print i                                     11 LOAD_CONST      0 (None)
                                                 14 RETURN_VALUE
   6
   7 print dis.dis(foo)


• Bytecode stream: An opcode mix
• Defined in “Python-2.7.2/Include/opcode.h”
                          Attribution-NonCommercial CC BY-NC
Python VM
• Engine to execute Bytecodes
• CPython VM is a C implementation
• Stack based process VM
  – PUSH/ POP operations
• Implementation of Python VM?




                Attribution-NonCommercial CC BY-NC
Python VM: Implementation
                                                          Python/ceval.c
                                                          --> PyEval_EvalFrameEx()
for(; ;) {
       /* Extract opcode and argument */
            opcode = NEXTOP();
            if (HAS_ARG(opcode))
                oparg = NEXTARG();
                switch(opcode) {
                     case LOAD_CONST:
                            ……
                 }
    }
                     Attribution-NonCommercial CC BY-NC
Optimizations


                                Tools
    Getting in to the Problem Space!!




             Attribution-NonCommercial CC BY-NC
Pyrex
• Python like language to create C module for
  Python
• Create your “pyx” files and compile them in “.c”
  files
• Import them as modules in your applications
• Pyrex used as:
  – speed up the execution of Python code
  – Python interface to existing C modules/libraries
• Lot of work for developer
  – .py to .pyx?
  – thinking in C

                    Attribution-NonCommercial CC BY-NC
Psyco
• An intelligent option – JIT compilation
• Profiles dynamically an application for hot-
  spots
• “in-memory” prepares C extension and hook
  them appropriately
• Solves “duck” typing
• Memory footprint?
• Support till CPython 2.5

                Attribution-NonCommercial CC BY-NC
Psyco| Intelligent use
                                        Iterations    Without      With
                                                     Pysco(ms)   Pysco(ms)

                                           1000        125         151

                                         100000       12900       12570




• from psyco.classes import *
• pysco.bind(func)

                Attribution-NonCommercial CC BY-NC
Optimizations


                          Bytecode level
    Getting in to the Problem Space!!




            Attribution-NonCommercial CC BY-NC
Why optimize Bytecode?
                                        def foo():
• Python optimization                                 i=0
                                                      i =i+1
  are ineffective,                                    print i

  sometime
                                    4      0 LOAD_CONST          1 (0)
• Duck typing                              3 STORE_FAST          0 (i)

                                    5      6 LOAD_FAST          0 (i)
  – Run-time types                        9 LOAD_CONST          2 (1)
                                          12 BINARY_ADD
  – Optimizer misses                      13 STORE_FAST         0 (i)

    many opportunities              6     16 LOAD_FAST          0 (i)
                                          19 PRINT_ITEM
  – TCO                                   20 PRINT_NEWLINE
                                          21 LOAD_CONST          0 (None)




                 Attribution-NonCommercial CC BY-NC
Optimizations: Tail Recursive Calls
$ cat runbytecode3.py                     $ python runbytecode3.py

import dis                                 5      1 LOAD_CONST           10
                                                  2 STORE_FAST           x

                                           6       4 LOAD_FAST                  x
def foo():                                        5 LOAD_CONST                 10
  x = 10                                          6 COMPARE_OP                 ==
                                                  7 POP_JUMP_IF_FALSE          to 17
  if x == 10:
                                           7      9 LOAD_CONST       0
     x=0                                         10 STORE_FAST       x
     foo()
                                           8   12 LOAD_GLOBAL            foo
                                               13 CALL_FUNCTION           0
                                               14 POP_TOP
print dis.dis(foo)                             15 JUMP_FORWARD            to 17
                                             >> 17 LOAD_CONST            None
                                               18 RETURN_VALUE
                                          None



                        Attribution-NonCommercial CC BY-NC
BytePlay: Do it yourself!
• Experiment with Bytecodes
• Generates, recompiles and runs code on-the-
  fly
• Very useful to evaluate different code
  optimizations
• Example



                Attribution-NonCommercial CC BY-NC
Playing with Bytecode
$cat runbytecode2.py                               $ python runbytecode2.py
                                                   reassembled ‘byteplay.py' imported.
from byteplay import *
from pprint import pprint                           5     1 LOAD_CONST            10 <<<-------------------------
                                                          2 STORE_FAST        x
def foo():
  x = 10                                            6     4 LOAD_FAST     x
                                                          5 PRINT_ITEM
  print x
                                                          6 PRINT_NEWLINE
                                                          7 LOAD_CONST      None
c = Code.from_code(foo.func_code)                         8 RETURN_VALUE
print printcodelist(c.code)                        None

# Modify the byte code                              5     1 LOAD_CONST            1000 <<<---------------------------
c.code[0] = (LOAD_CONST, 1000)                            2 STORE_FAST        x

foo.func_code = c.to_code()                         6     4 LOAD_FAST     x
                                                          5 PRINT_ITEM
print printcodelist(c.code)                               6 PRINT_NEWLINE
                                                          7 LOAD_CONST      None
                                                          8 RETURN_VALUE
# Call the modified function
                                                   None
foo()
                                                   1000




                                    Attribution-NonCommercial CC BY-NC
Going forward


                                PyPy
    Getting in to the Problem Space!!




             Attribution-NonCommercial CC BY-NC
Does PyPy help?
• Python interpreter in Python itself
• A target function needs to be written with
  subset of Python (Restricted Python)
• PyPy can translate this function to runtime of
  your choice ☺
  – Options: C/POSIX, CLI/.NET and Java/JVM
• Seamless and transparent


                 Attribution-NonCommercial CC BY-NC
PyPy | more
from pypy.translator.interactive import                   @compdec
    Translation
                                                          def fact(n):
class compdec:
                                                            if 0 == n:
  def __init__(self, func):                                    return 1
     self.func = func                                       return n * fact(n -1)
     self.argtypes = None                                 fact(10)

  def __call__(self, *args):
    argtypes = tuple(type(arg) for arg in
    args)
    if argtypes != self.argtypes:
       self.argtypes = argtypes
       t = Translation(self.func)
       t.annotate(argtypes)
       self.cfunc = t.compile_c()               What PyPy does?
                                                •The translation of the RPython function to C.
                                                •Invoking the C compiler to create a C extension module.
    return self.cfunc(*args)
                                                •Importing the compiled function back into the Python interpreter.




                                     Attribution-NonCommercial CC BY-NC
Recommendations
• Always profile your applications
• 90/10 rule of performance gain
• Performance critical portion could be written
  as C extensions
  – Wrappers like SWIG could be used to bridge
    Python/C applications
  – Pyrex, Psyco, and PyPy
  – Tuning Bytecodes manually
• Evaluate and use ☺

                  Attribution-NonCommercial CC BY-NC
References
• http://docs.python.org/release/2.5.2/lib/module-dis.html
• http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyre
  x/
• http://www.dalkescientific.com/writings/diary/archive/201
  0/02/22/instrumenting_the_ast.html
• http://cs.utsa.edu/~danlo/teaching/cs4713/lecture/node7.
  html
• http://www.devshed.com/c/a/Python/How-Python-Runs-
  Programs/4/
• http://www.enthought.com/~ischnell/paper.html
• http://codespeak.net/pypy/dist/pypy/doc/translation.html
• http://effbot.org/pyref/type-code.htm

                     Attribution-NonCommercial CC BY-NC
Questions



  Thank you for your time and attention ☺



• Please share your feedback/ comments/ suggestions to us at:
• cjgiridhar@gmail.com ,        http://technobeans.com
• vishalkanaujia@gmail.com,     http://freethreads.wordpress.com

                         Attribution-NonCommercial CC BY-NC
Backup slides




 Attribution-NonCommercial CC BY-NC
Bytecodes | more
• Atomic unit of execution (thread safe)
• A Python module is compiled and saved in
  form of a “pyc” file
• An optimization; avoid compilation phase for
  future uses of a module
• If source file is changed, pyc is recompiled
• “pyc” file is coded in bytecode!


                Attribution-NonCommercial CC BY-NC
Code Objects
• Bytecode representation
• Immutable objects (No ref to mutable objects)
• Function object (Code objects that reference
  global variables)
• “co_code” = Bytecodes




                Attribution-NonCommercial CC BY-NC
Benefits of using Bytecode
• Architecture neutral code
• Portability
• Simple optimizations




                Attribution-NonCommercial CC BY-NC

More Related Content

What's hot

Python легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачиPython легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачи
Maxim Kulsha
 
.NET 2015: Будущее рядом
.NET 2015: Будущее рядом.NET 2015: Будущее рядом
.NET 2015: Будущее рядом
Andrey Akinshin
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and Go
Eleanor McHugh
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
Svet Ivantchev
 
Go a crash course
Go   a crash courseGo   a crash course
Go a crash course
Eleanor McHugh
 
«Отладка в Python 3.6: Быстрее, Выше, Сильнее» Елизавета Шашкова, JetBrains
«Отладка в Python 3.6: Быстрее, Выше, Сильнее» Елизавета Шашкова, JetBrains«Отладка в Python 3.6: Быстрее, Выше, Сильнее» Елизавета Шашкова, JetBrains
«Отладка в Python 3.6: Быстрее, Выше, Сильнее» Елизавета Шашкова, JetBrains
it-people
 
Faster Python, FOSDEM
Faster Python, FOSDEMFaster Python, FOSDEM
Faster Python, FOSDEM
Victor Stinner
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
Eleanor McHugh
 
Functional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonFunctional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with Python
Carlos V.
 
Phil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonPhil Bartie QGIS PLPython
Phil Bartie QGIS PLPython
Ross McDonald
 
Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?
Andrey Akinshin
 
Let's golang
Let's golangLet's golang
Let's golang
SuHyun Jeon
 
Welcome to python
Welcome to pythonWelcome to python
Welcome to python
Kyunghoon Kim
 
C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)
Yuki Tamura
 
Python profiling
Python profilingPython profiling
Python profiling
dreampuf
 
Exploiting Memory Overflows
Exploiting Memory OverflowsExploiting Memory Overflows
Exploiting Memory Overflows
Ankur Tyagi
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
Abed Bukhari
 
Metarhia KievJS 22-Feb-2018
Metarhia KievJS 22-Feb-2018Metarhia KievJS 22-Feb-2018
Metarhia KievJS 22-Feb-2018
Timur Shemsedinov
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 redux
Eleanor McHugh
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
Adam Dudczak
 

What's hot (20)

Python легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачиPython легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачи
 
.NET 2015: Будущее рядом
.NET 2015: Будущее рядом.NET 2015: Будущее рядом
.NET 2015: Будущее рядом
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and Go
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Go a crash course
Go   a crash courseGo   a crash course
Go a crash course
 
«Отладка в Python 3.6: Быстрее, Выше, Сильнее» Елизавета Шашкова, JetBrains
«Отладка в Python 3.6: Быстрее, Выше, Сильнее» Елизавета Шашкова, JetBrains«Отладка в Python 3.6: Быстрее, Выше, Сильнее» Елизавета Шашкова, JetBrains
«Отладка в Python 3.6: Быстрее, Выше, Сильнее» Елизавета Шашкова, JetBrains
 
Faster Python, FOSDEM
Faster Python, FOSDEMFaster Python, FOSDEM
Faster Python, FOSDEM
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
 
Functional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonFunctional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with Python
 
Phil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonPhil Bartie QGIS PLPython
Phil Bartie QGIS PLPython
 
Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?
 
Let's golang
Let's golangLet's golang
Let's golang
 
Welcome to python
Welcome to pythonWelcome to python
Welcome to python
 
C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)
 
Python profiling
Python profilingPython profiling
Python profiling
 
Exploiting Memory Overflows
Exploiting Memory OverflowsExploiting Memory Overflows
Exploiting Memory Overflows
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Metarhia KievJS 22-Feb-2018
Metarhia KievJS 22-Feb-2018Metarhia KievJS 22-Feb-2018
Metarhia KievJS 22-Feb-2018
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 redux
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
 

Similar to Diving into byte code optimization in python

JIT compilation for CPython
JIT compilation for CPythonJIT compilation for CPython
JIT compilation for CPython
delimitry
 
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Romain Dorgueil
 
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain DorgueilSimple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Pôle Systematic Paris-Region
 
Pysense: wireless sensor computing in Python?
Pysense: wireless sensor computing in Python?Pysense: wireless sensor computing in Python?
Pysense: wireless sensor computing in Python?
Davide Carboni
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
GlobalLogic Ukraine
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行
kao kuo-tung
 
Find your own iOS kernel bug
Find your own iOS kernel bugFind your own iOS kernel bug
Find your own iOS kernel bug
Gustavo Martinez
 
Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020
Yung-Yu Chen
 
12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine
ChengHui Weng
 
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
Wang Hao Lee
 
Writing a Python C extension
Writing a Python C extensionWriting a Python C extension
Writing a Python C extension
Sqreen
 
PythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummiesPythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummies
Tatiana Al-Chueyr
 
mpi4py.pdf
mpi4py.pdfmpi4py.pdf
mpi4py.pdf
A Jorge Garcia
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
Henry Schreiner
 
OpenGurukul : Language : Python
OpenGurukul : Language : PythonOpenGurukul : Language : Python
OpenGurukul : Language : Python
Open Gurukul
 
PyCon KR 2019 sprint - RustPython by example
PyCon KR 2019 sprint  - RustPython by examplePyCon KR 2019 sprint  - RustPython by example
PyCon KR 2019 sprint - RustPython by example
YunWon Jeong
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
Henry Schreiner
 
Cleanup and new optimizations in WPython 1.1
Cleanup and new optimizations in WPython 1.1Cleanup and new optimizations in WPython 1.1
Cleanup and new optimizations in WPython 1.1
PyCon Italia
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
John(Qiang) Zhang
 
CorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. ProgrammingCorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. Programming
Slide_N
 

Similar to Diving into byte code optimization in python (20)

JIT compilation for CPython
JIT compilation for CPythonJIT compilation for CPython
JIT compilation for CPython
 
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
 
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain DorgueilSimple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
 
Pysense: wireless sensor computing in Python?
Pysense: wireless sensor computing in Python?Pysense: wireless sensor computing in Python?
Pysense: wireless sensor computing in Python?
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行
 
Find your own iOS kernel bug
Find your own iOS kernel bugFind your own iOS kernel bug
Find your own iOS kernel bug
 
Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020
 
12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine
 
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
英文【Xu hao chen xiaobo】find your_own_ios_kernel_bug
 
Writing a Python C extension
Writing a Python C extensionWriting a Python C extension
Writing a Python C extension
 
PythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummiesPythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummies
 
mpi4py.pdf
mpi4py.pdfmpi4py.pdf
mpi4py.pdf
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
OpenGurukul : Language : Python
OpenGurukul : Language : PythonOpenGurukul : Language : Python
OpenGurukul : Language : Python
 
PyCon KR 2019 sprint - RustPython by example
PyCon KR 2019 sprint  - RustPython by examplePyCon KR 2019 sprint  - RustPython by example
PyCon KR 2019 sprint - RustPython by example
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
Cleanup and new optimizations in WPython 1.1
Cleanup and new optimizations in WPython 1.1Cleanup and new optimizations in WPython 1.1
Cleanup and new optimizations in WPython 1.1
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
 
CorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. ProgrammingCorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. Programming
 

More from Chetan Giridhar

Rapid development & integration of real time communication in websites
Rapid development & integration of real time communication in websitesRapid development & integration of real time communication in websites
Rapid development & integration of real time communication in websites
Chetan Giridhar
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
Chetan Giridhar
 
PyCon India 2012: Rapid development of website search in python
PyCon India 2012: Rapid development of website search in pythonPyCon India 2012: Rapid development of website search in python
PyCon India 2012: Rapid development of website search in python
Chetan Giridhar
 
Fuse'ing python for rapid development of storage efficient FS
Fuse'ing python for rapid development of storage efficient FSFuse'ing python for rapid development of storage efficient FS
Fuse'ing python for rapid development of storage efficient FS
Chetan Giridhar
 
Testers in product development code review phase
Testers in product development   code review phaseTesters in product development   code review phase
Testers in product development code review phase
Chetan Giridhar
 
Design patterns in python v0.1
Design patterns in python v0.1Design patterns in python v0.1
Design patterns in python v0.1
Chetan Giridhar
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
Chetan Giridhar
 
PyCon India 2011: Python Threads: Dive into GIL!
PyCon India 2011: Python Threads: Dive into GIL!PyCon India 2011: Python Threads: Dive into GIL!
PyCon India 2011: Python Threads: Dive into GIL!
Chetan Giridhar
 
Pycon11: Python threads: Dive into GIL!
Pycon11: Python threads: Dive into GIL!Pycon11: Python threads: Dive into GIL!
Pycon11: Python threads: Dive into GIL!
Chetan Giridhar
 

More from Chetan Giridhar (9)

Rapid development & integration of real time communication in websites
Rapid development & integration of real time communication in websitesRapid development & integration of real time communication in websites
Rapid development & integration of real time communication in websites
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
 
PyCon India 2012: Rapid development of website search in python
PyCon India 2012: Rapid development of website search in pythonPyCon India 2012: Rapid development of website search in python
PyCon India 2012: Rapid development of website search in python
 
Fuse'ing python for rapid development of storage efficient FS
Fuse'ing python for rapid development of storage efficient FSFuse'ing python for rapid development of storage efficient FS
Fuse'ing python for rapid development of storage efficient FS
 
Testers in product development code review phase
Testers in product development   code review phaseTesters in product development   code review phase
Testers in product development code review phase
 
Design patterns in python v0.1
Design patterns in python v0.1Design patterns in python v0.1
Design patterns in python v0.1
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
 
PyCon India 2011: Python Threads: Dive into GIL!
PyCon India 2011: Python Threads: Dive into GIL!PyCon India 2011: Python Threads: Dive into GIL!
PyCon India 2011: Python Threads: Dive into GIL!
 
Pycon11: Python threads: Dive into GIL!
Pycon11: Python threads: Dive into GIL!Pycon11: Python threads: Dive into GIL!
Pycon11: Python threads: Dive into GIL!
 

Recently uploaded

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 

Recently uploaded (20)

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 

Diving into byte code optimization in python

  • 1. Diving into Byte-code optimization in Python SciPy India, IIT Bombay Dec 05th 2011 Chetan Giridhar and Vishal Kanaujia
  • 2. Fundamentals of Bytecode • Python source code compiled into Python byte code by the CPython interpreter • “.pyc”? – Automatic compilation : importing a module – Explicit compilation : py_compile.compile(“module.py”) – generates ‘module.pyc’ • The module ‘compileall’{} Attribution-NonCommercial CC BY-NC
  • 3. Fundamentals | more • A program doesn't run any faster when it is read from a ‘.pyc’ file. But, why? • “.pyc” for a script executed on the command line • “.pyc” files – good enough to distribute your code, but with a caveat! Attribution-NonCommercial CC BY-NC
  • 4. Compilation phases • Uses a ‘lexer’ to prepare tokens of Python code • A parser arranges token according to the language grammar and prepares concrete syntax tree • Concrete syntax tree is transformed in to AST • AST is compiled to produce Byte-codes Attribution-NonCommercial CC BY-NC
  • 5. Python Abstract Parse Tree source Syntax generated code Tree Pgen.c ast.c compile.c Executed Optimized Bytecode by Python bytecode generated VM ceval.c peephole.c
  • 6. Python “ast” module $ cat myast.py $python myast.py <generator object walk at 0xb784c8ec> import ast Module(body=[Expr(value=BinOp nod = ast.parse('a +2') (left=Name(id='a', ctx=Load()), print ast.walk(nod) op=Add(), right=Num(n=2)))]) print ast.dump(nod) •Convenient for analysis, code transformations and generation •ASTs are compiled to code objects Attribution-NonCommercial CC BY-NC
  • 7. A peek into Bytecodes $ cat scipy.py $ python scipy.py 1 import dis 4 0 LOAD_CONST 1 (10) 3 STORE_FAST 0 (i) 2 3 def foo(): 5 6 LOAD_FAST 0 (i) 9 PRINT_ITEM 4 i = 10 10 PRINT_NEWLINE 5 print i 11 LOAD_CONST 0 (None) 14 RETURN_VALUE 6 7 print dis.dis(foo) • Bytecode stream: An opcode mix • Defined in “Python-2.7.2/Include/opcode.h” Attribution-NonCommercial CC BY-NC
  • 8. Python VM • Engine to execute Bytecodes • CPython VM is a C implementation • Stack based process VM – PUSH/ POP operations • Implementation of Python VM? Attribution-NonCommercial CC BY-NC
  • 9. Python VM: Implementation Python/ceval.c --> PyEval_EvalFrameEx() for(; ;) { /* Extract opcode and argument */ opcode = NEXTOP(); if (HAS_ARG(opcode)) oparg = NEXTARG(); switch(opcode) { case LOAD_CONST: …… } } Attribution-NonCommercial CC BY-NC
  • 10. Optimizations Tools Getting in to the Problem Space!! Attribution-NonCommercial CC BY-NC
  • 11. Pyrex • Python like language to create C module for Python • Create your “pyx” files and compile them in “.c” files • Import them as modules in your applications • Pyrex used as: – speed up the execution of Python code – Python interface to existing C modules/libraries • Lot of work for developer – .py to .pyx? – thinking in C Attribution-NonCommercial CC BY-NC
  • 12. Psyco • An intelligent option – JIT compilation • Profiles dynamically an application for hot- spots • “in-memory” prepares C extension and hook them appropriately • Solves “duck” typing • Memory footprint? • Support till CPython 2.5 Attribution-NonCommercial CC BY-NC
  • 13. Psyco| Intelligent use Iterations Without With Pysco(ms) Pysco(ms) 1000 125 151 100000 12900 12570 • from psyco.classes import * • pysco.bind(func) Attribution-NonCommercial CC BY-NC
  • 14. Optimizations Bytecode level Getting in to the Problem Space!! Attribution-NonCommercial CC BY-NC
  • 15. Why optimize Bytecode? def foo(): • Python optimization i=0 i =i+1 are ineffective, print i sometime 4 0 LOAD_CONST 1 (0) • Duck typing 3 STORE_FAST 0 (i) 5 6 LOAD_FAST 0 (i) – Run-time types 9 LOAD_CONST 2 (1) 12 BINARY_ADD – Optimizer misses 13 STORE_FAST 0 (i) many opportunities 6 16 LOAD_FAST 0 (i) 19 PRINT_ITEM – TCO 20 PRINT_NEWLINE 21 LOAD_CONST 0 (None) Attribution-NonCommercial CC BY-NC
  • 16. Optimizations: Tail Recursive Calls $ cat runbytecode3.py $ python runbytecode3.py import dis 5 1 LOAD_CONST 10 2 STORE_FAST x 6 4 LOAD_FAST x def foo(): 5 LOAD_CONST 10 x = 10 6 COMPARE_OP == 7 POP_JUMP_IF_FALSE to 17 if x == 10: 7 9 LOAD_CONST 0 x=0 10 STORE_FAST x foo() 8 12 LOAD_GLOBAL foo 13 CALL_FUNCTION 0 14 POP_TOP print dis.dis(foo) 15 JUMP_FORWARD to 17 >> 17 LOAD_CONST None 18 RETURN_VALUE None Attribution-NonCommercial CC BY-NC
  • 17. BytePlay: Do it yourself! • Experiment with Bytecodes • Generates, recompiles and runs code on-the- fly • Very useful to evaluate different code optimizations • Example Attribution-NonCommercial CC BY-NC
  • 18. Playing with Bytecode $cat runbytecode2.py $ python runbytecode2.py reassembled ‘byteplay.py' imported. from byteplay import * from pprint import pprint 5 1 LOAD_CONST 10 <<<------------------------- 2 STORE_FAST x def foo(): x = 10 6 4 LOAD_FAST x 5 PRINT_ITEM print x 6 PRINT_NEWLINE 7 LOAD_CONST None c = Code.from_code(foo.func_code) 8 RETURN_VALUE print printcodelist(c.code) None # Modify the byte code 5 1 LOAD_CONST 1000 <<<--------------------------- c.code[0] = (LOAD_CONST, 1000) 2 STORE_FAST x foo.func_code = c.to_code() 6 4 LOAD_FAST x 5 PRINT_ITEM print printcodelist(c.code) 6 PRINT_NEWLINE 7 LOAD_CONST None 8 RETURN_VALUE # Call the modified function None foo() 1000 Attribution-NonCommercial CC BY-NC
  • 19. Going forward PyPy Getting in to the Problem Space!! Attribution-NonCommercial CC BY-NC
  • 20. Does PyPy help? • Python interpreter in Python itself • A target function needs to be written with subset of Python (Restricted Python) • PyPy can translate this function to runtime of your choice ☺ – Options: C/POSIX, CLI/.NET and Java/JVM • Seamless and transparent Attribution-NonCommercial CC BY-NC
  • 21. PyPy | more from pypy.translator.interactive import @compdec Translation def fact(n): class compdec: if 0 == n: def __init__(self, func): return 1 self.func = func return n * fact(n -1) self.argtypes = None fact(10) def __call__(self, *args): argtypes = tuple(type(arg) for arg in args) if argtypes != self.argtypes: self.argtypes = argtypes t = Translation(self.func) t.annotate(argtypes) self.cfunc = t.compile_c() What PyPy does? •The translation of the RPython function to C. •Invoking the C compiler to create a C extension module. return self.cfunc(*args) •Importing the compiled function back into the Python interpreter. Attribution-NonCommercial CC BY-NC
  • 22. Recommendations • Always profile your applications • 90/10 rule of performance gain • Performance critical portion could be written as C extensions – Wrappers like SWIG could be used to bridge Python/C applications – Pyrex, Psyco, and PyPy – Tuning Bytecodes manually • Evaluate and use ☺ Attribution-NonCommercial CC BY-NC
  • 23. References • http://docs.python.org/release/2.5.2/lib/module-dis.html • http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyre x/ • http://www.dalkescientific.com/writings/diary/archive/201 0/02/22/instrumenting_the_ast.html • http://cs.utsa.edu/~danlo/teaching/cs4713/lecture/node7. html • http://www.devshed.com/c/a/Python/How-Python-Runs- Programs/4/ • http://www.enthought.com/~ischnell/paper.html • http://codespeak.net/pypy/dist/pypy/doc/translation.html • http://effbot.org/pyref/type-code.htm Attribution-NonCommercial CC BY-NC
  • 24. Questions Thank you for your time and attention ☺ • Please share your feedback/ comments/ suggestions to us at: • cjgiridhar@gmail.com , http://technobeans.com • vishalkanaujia@gmail.com, http://freethreads.wordpress.com Attribution-NonCommercial CC BY-NC
  • 26. Bytecodes | more • Atomic unit of execution (thread safe) • A Python module is compiled and saved in form of a “pyc” file • An optimization; avoid compilation phase for future uses of a module • If source file is changed, pyc is recompiled • “pyc” file is coded in bytecode! Attribution-NonCommercial CC BY-NC
  • 27. Code Objects • Bytecode representation • Immutable objects (No ref to mutable objects) • Function object (Code objects that reference global variables) • “co_code” = Bytecodes Attribution-NonCommercial CC BY-NC
  • 28. Benefits of using Bytecode • Architecture neutral code • Portability • Simple optimizations Attribution-NonCommercial CC BY-NC