SlideShare uma empresa Scribd logo
1 de 59
Baixar para ler offline
Use Case / Experiment
TsundereChen
Test Environment
• Vagrant, Ubuntu/16.04
• The benchmark result on Host OS and Guest OS
is really close, so I use VM to get result

(BTW, it's really easy to get your VM dirty
Test Version
• CPython2 2.7.12
• CPython3 3.5.2
• PyPy2 5.1.2 (Installed from apt-get)
• PyPy2 5.6.0 (Compiled from source)
• PyPy3 5.7.1 (Compiled from source)
Why only CPython & PyPy
• Cython
• You'll need to learn Cython's syntax, it's mixing
C and Python.
• Jython
• The latest version of Jython 2.7.0 is released in
May 2015, so it's outdated
Some notice
• PyPy3 is still in beta, so if it's slower than
CPython 3, no surprise
• And not every module can run faster in PyPy
than CPython, there will be samples later
Why PyPy3 is beta ?
• The way CPython develop and the way PyPy develop
is different
• CPython
• Focus on Python3, only maintain Python2 when
security issue pops up
• PyPy
• Focus on PyPy2, also updating PyPy3, but it's not
their main development
How to run PyPy
PyPy Installation
• pypy.org/download.html
• If you download binary
• run bin/pypy
PyPy Installation
• If you want to compile PyPy from scratch
• First, install dependencies
• http://doc.pypy.org/en/latest/build.html
• Then, cd to pypy/goal
PyPy Installation
• No-JIT
• <Python/PyPy> ../../rpython/bin/rpython --
opt=2
• JIT-Enabled
• <Python/PyPy> ../../rpython/bin/rpython --
opt=jit
PyPy Installation
• Notice
• Compile PyPy takes lots of time, and compile it
with JIT-Enabled takes even more.
• Usually takes 30min up
• And you need at least 4G RAM to compile it on
64-Bit Machine, make sure you have enough
RAM for this, or it may be killed by system
Mandelbrot — For Fun
Benchmark result
gcbench
json_bench
django_template
nqueens
regex_v8
richards
scimark
sqlalchemy_declarative
sqlalchemy_imperative
So, why do we still need
CPython?
Not every case should use PyPy
• For example, when it comes to the code below,
CPython is faster than PyPy

myStr = “”

for x in xrange(1, 10**6):

myStr += str(myStr[x])
Can <package> run on
PyPy?
http://packages.pypy.org/
Enough benchmark, let's get
to DSL
Example
• Language: Brainf*ck
• 8 commands
• + mem[ptr] += 1 - mem[ptr] -= 1

< ptr -= 1 > ptr += 1

, input() . print()

[ while(mem[ptr]){ ] }
Repo for Brainf*ck experiment
• https://github.com/TsundereChen/bf_to_py
Our Goal
• Build a Brainf*ck Interpreter
• Build a Brainf*ck to Python translator, and
compile it with PyPy
Interpreter
• Just read in the file, and execute the command
• But, we can add JIT here
What to do to add JIT
• We need to find "Reds" and "Greens"
• Greens -> Define instructions
• Reds -> What's being manipulated
What to do to add JIT
• from rpython.rlib.jit import JitDriver
• jitdriver = JitDriver(greens=[], reds=[])
• and add jit_merge_point to your main loop
Difference
Hm....Not very good, right ?
Notice the second
It’s 2.73… v.s 2.61…
JIT is not enough...

How about some opts
Optimize
• Speed up loop
• Because every loop needs to look up address in
dictionary, but the dictionary is static, so we can
use @elidable decorator and add a function to
speed up
Difference
Hmm.... Better
Difference
Hmm.... Better
Difference
Hmm.... Better
Difference
Hmm.... Better
Okay...enough interpreter

Let's talk about compiler
Basic Knowledge
• It reads in Brainf*ck file, then turn into IR
• Then you can choose to do Optimize in IR
• Finally, turn your IR into Python Code, and
compile it with PyPy to generate a binary le
Brainf*ck
Code
IR
Python
Code
Binary
File
Architecture
• ir.py -> For Brainf*ck to IR and IR to Python
• trans.py -> Main program
• python trans.py <input> <output> <optmode>
• optmode 1 to open optimization, 0 to not to
• opt.py -> Optimize tricks
Optimizations
• opt_contract ( Contract)
• Operation like " +++++ ", means that we have
to do "mem[p] += 1" ve times
• But because we have IR, so we can change
the instruction to "mem[p] += 5"
• When it comes to “+ - > <“, this trick can apply
Optimizations
• opt_clearloop (Clear Loop)
• Command like [-], it means when(mem[p]), do
mem[p] -= 1
• We know what the result is, so we can set
mem[p] to zero directly

mem[p] = 0
Optimizations
• opt_multiloop & opt_copyloop (Multiplication and
Copy)
• Command like [->+>+<<] is copy mem[p]'s
value to mem[p+1] and mem[p+2], and set
mem[p] to zero
• If we know what this is doing, we can make it
short
Optimizations
• opt_multiloop & opt_copyloop (Multiplication and
Copy)
• Same trick can apply to [->++<], make

mem[p+1] = 2 * mem[p] and set mem[p] = 0
• Which is multiplication
Optimizations
• opt_offsetops (Operation Offsets)
• In Brainf*ck, we know that we have a pointer
indicating where we are now, and pointer
usually move a lot
• What if we can calculate offset for Instructions
directly, so we don't need to move the pointer
around
Optimizations
• opt_cancel (Cancel Instructions)
• ++++-->>+-<<< do the same thing as ++<
• Then, why waste all the time on these
Instructons ?
Can it run faster ?
Yes — JIT
Result
Result
Result
Result
Great! So I'll use JIT from now on
Wait a sec...
• Not every case can use JIT
• Because JIT needs to warm-up and Analysis

Maybe warm-up can take more time than your
code actually run
• And it's import to avoid to record the warm-up
time when you want to do some benchmarking
Wait a sec...
• And do you really need JIT ?
• It may cost a lot for one to import JIT to a
project
• Sometimes, maybe buy more server is a better
choice than import JIT into your project
Wait a sec...
• But if you analyzed your project, know how
difcult it is for you to import PyPy and JIT into
your project, then you're good to go!
• BTW, file size of executable with JIT Enabled is
bigger than the one with No-JIT
Question?
References
• Tutorial: Writing an Interpreter with PyPy, part 1
• https://morepypy.blogspot.tw/2011/04/tutorial-writing-interpreter-with-
pypy.html
• PyPy - Tutorial for Brainf*ck Interpreter
• http://wdv4758h.github.io/posts/2015/01/pypy-tutorial-for-brainfuck-
interpreter/
• matslina/bfoptimization
• https://github.com/matslina/bfoptimization/
• Virtual Machine Constructions for Dummies
• https://www.slideshare.net/jserv/vm-construct

Mais conteĂşdo relacionado

Mais procurados

Runtime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in JavaRuntime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
Juan Fumero
 
Profiling and optimizing go programs
Profiling and optimizing go programsProfiling and optimizing go programs
Profiling and optimizing go programs
Badoo Development
 

Mais procurados (20)

Async await in C++
Async await in C++Async await in C++
Async await in C++
 
TensorFlow XLA RPC
TensorFlow XLA RPCTensorFlow XLA RPC
TensorFlow XLA RPC
 
asyncio internals
asyncio internalsasyncio internals
asyncio internals
 
Bridge TensorFlow to run on Intel nGraph backends (v0.4)
Bridge TensorFlow to run on Intel nGraph backends (v0.4)Bridge TensorFlow to run on Intel nGraph backends (v0.4)
Bridge TensorFlow to run on Intel nGraph backends (v0.4)
 
Golang concurrency design
Golang concurrency designGolang concurrency design
Golang concurrency design
 
Metaprogramming and Reflection in Common Lisp
Metaprogramming and Reflection in Common LispMetaprogramming and Reflection in Common Lisp
Metaprogramming and Reflection in Common Lisp
 
Adam Sitnik "State of the .NET Performance"
Adam Sitnik "State of the .NET Performance"Adam Sitnik "State of the .NET Performance"
Adam Sitnik "State of the .NET Performance"
 
C++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingC++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogramming
 
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in JavaRuntime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in Golang
 
Python Async IO Horizon
Python Async IO HorizonPython Async IO Horizon
Python Async IO Horizon
 
用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構
 
Profiling and optimizing go programs
Profiling and optimizing go programsProfiling and optimizing go programs
Profiling and optimizing go programs
 
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
 
TensorFlow Lite (r1.5) & Android 8.1 Neural Network API
TensorFlow Lite (r1.5) & Android 8.1 Neural Network APITensorFlow Lite (r1.5) & Android 8.1 Neural Network API
TensorFlow Lite (r1.5) & Android 8.1 Neural Network API
 
PyHEP 2018: Tools to bind to Python
PyHEP 2018:  Tools to bind to PythonPyHEP 2018:  Tools to bind to Python
PyHEP 2018: Tools to bind to Python
 
PyParis2017 / Function-as-a-service - a pythonic perspective on severless com...
PyParis2017 / Function-as-a-service - a pythonic perspective on severless com...PyParis2017 / Function-as-a-service - a pythonic perspective on severless com...
PyParis2017 / Function-as-a-service - a pythonic perspective on severless com...
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleiner
 
Concurrency in go
Concurrency in goConcurrency in go
Concurrency in go
 
History & Practices for UniRx(EN)
History & Practices for UniRx(EN)History & Practices for UniRx(EN)
History & Practices for UniRx(EN)
 

Semelhante a PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime -Part 2

Cooking a rabbit pie
Cooking a rabbit pieCooking a rabbit pie
Cooking a rabbit pie
Tomas Doran
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
gabriellekuruvilla
 

Semelhante a PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime -Part 2 (20)

PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
An Introduction to PyPy
An Introduction to PyPyAn Introduction to PyPy
An Introduction to PyPy
 
Elasticwulf Pycon Talk
Elasticwulf Pycon TalkElasticwulf Pycon Talk
Elasticwulf Pycon Talk
 
Rusty Python
Rusty PythonRusty Python
Rusty Python
 
from ai.backend import python @ pycontw2018
from ai.backend import python @ pycontw2018from ai.backend import python @ pycontw2018
from ai.backend import python @ pycontw2018
 
SMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgiSMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgi
 
Euro python2011 High Performance Python
Euro python2011 High Performance PythonEuro python2011 High Performance Python
Euro python2011 High Performance Python
 
High-Performance Python
High-Performance PythonHigh-Performance Python
High-Performance Python
 
Performance Enhancement Tips
Performance Enhancement TipsPerformance Enhancement Tips
Performance Enhancement Tips
 
Numba Overview
Numba OverviewNumba Overview
Numba Overview
 
Building SciPy kernels with Pythran
Building SciPy kernels with PythranBuilding SciPy kernels with Pythran
Building SciPy kernels with Pythran
 
Gpgpu intro
Gpgpu introGpgpu intro
Gpgpu intro
 
Cooking a rabbit pie
Cooking a rabbit pieCooking a rabbit pie
Cooking a rabbit pie
 
Fast Python: Master the Basics to Write Faster Code
Fast Python: Master the Basics to Write Faster CodeFast Python: Master the Basics to Write Faster Code
Fast Python: Master the Basics to Write Faster Code
 
Why a new CPAN client cpm is fast
Why a new CPAN client cpm is fastWhy a new CPAN client cpm is fast
Why a new CPAN client cpm is fast
 
Pythonic doesn't mean slow!
Pythonic doesn't mean slow!Pythonic doesn't mean slow!
Pythonic doesn't mean slow!
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
 
GPU Computing for Data Science
GPU Computing for Data Science GPU Computing for Data Science
GPU Computing for Data Science
 
Writing Fast Code - PyCon HK 2015
Writing Fast Code - PyCon HK 2015Writing Fast Code - PyCon HK 2015
Writing Fast Code - PyCon HK 2015
 
Puppet Development Workflow
Puppet Development WorkflowPuppet Development Workflow
Puppet Development Workflow
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime -Part 2

  • 1. Use Case / Experiment TsundereChen
  • 2. Test Environment • Vagrant, Ubuntu/16.04 • The benchmark result on Host OS and Guest OS is really close, so I use VM to get result
 (BTW, it's really easy to get your VM dirty
  • 3. Test Version • CPython2 2.7.12 • CPython3 3.5.2 • PyPy2 5.1.2 (Installed from apt-get) • PyPy2 5.6.0 (Compiled from source) • PyPy3 5.7.1 (Compiled from source)
  • 4. Why only CPython & PyPy • Cython • You'll need to learn Cython's syntax, it's mixing C and Python. • Jython • The latest version of Jython 2.7.0 is released in May 2015, so it's outdated
  • 5. Some notice • PyPy3 is still in beta, so if it's slower than CPython 3, no surprise • And not every module can run faster in PyPy than CPython, there will be samples later
  • 6. Why PyPy3 is beta ? • The way CPython develop and the way PyPy develop is different • CPython • Focus on Python3, only maintain Python2 when security issue pops up • PyPy • Focus on PyPy2, also updating PyPy3, but it's not their main development
  • 7. How to run PyPy
  • 8. PyPy Installation • pypy.org/download.html • If you download binary • run bin/pypy
  • 9. PyPy Installation • If you want to compile PyPy from scratch • First, install dependencies • http://doc.pypy.org/en/latest/build.html • Then, cd to pypy/goal
  • 10. PyPy Installation • No-JIT • <Python/PyPy> ../../rpython/bin/rpython -- opt=2 • JIT-Enabled • <Python/PyPy> ../../rpython/bin/rpython -- opt=jit
  • 11. PyPy Installation • Notice • Compile PyPy takes lots of time, and compile it with JIT-Enabled takes even more. • Usually takes 30min up • And you need at least 4G RAM to compile it on 64-Bit Machine, make sure you have enough RAM for this, or it may be killed by system
  • 23. So, why do we still need CPython?
  • 24. Not every case should use PyPy • For example, when it comes to the code below, CPython is faster than PyPy
 myStr = “”
 for x in xrange(1, 10**6):
 myStr += str(myStr[x])
  • 25. Can <package> run on PyPy? http://packages.pypy.org/
  • 27. Example • Language: Brainf*ck • 8 commands • + mem[ptr] += 1 - mem[ptr] -= 1
 < ptr -= 1 > ptr += 1
 , input() . print()
 [ while(mem[ptr]){ ] }
  • 28. Repo for Brainf*ck experiment • https://github.com/TsundereChen/bf_to_py
  • 29. Our Goal • Build a Brainf*ck Interpreter • Build a Brainf*ck to Python translator, and compile it with PyPy
  • 30. Interpreter • Just read in the le, and execute the command • But, we can add JIT here
  • 31. What to do to add JIT • We need to nd "Reds" and "Greens" • Greens -> Dene instructions • Reds -> What's being manipulated
  • 32. What to do to add JIT • from rpython.rlib.jit import JitDriver • jitdriver = JitDriver(greens=[], reds=[]) • and add jit_merge_point to your main loop
  • 33. Difference Hm....Not very good, right ? Notice the second It’s 2.73… v.s 2.61…
  • 34. JIT is not enough...
 How about some opts
  • 35. Optimize • Speed up loop • Because every loop needs to look up address in dictionary, but the dictionary is static, so we can use @elidable decorator and add a function to speed up
  • 41. Basic Knowledge • It reads in Brainf*ck le, then turn into IR • Then you can choose to do Optimize in IR • Finally, turn your IR into Python Code, and compile it with PyPy to generate a binary le Brainf*ck Code IR Python Code Binary File
  • 42. Architecture • ir.py -> For Brainf*ck to IR and IR to Python • trans.py -> Main program • python trans.py <input> <output> <optmode> • optmode 1 to open optimization, 0 to not to • opt.py -> Optimize tricks
  • 43. Optimizations • opt_contract ( Contract) • Operation like " +++++ ", means that we have to do "mem[p] += 1" ve times • But because we have IR, so we can change the instruction to "mem[p] += 5" • When it comes to “+ - > <“, this trick can apply
  • 44. Optimizations • opt_clearloop (Clear Loop) • Command like [-], it means when(mem[p]), do mem[p] -= 1 • We know what the result is, so we can set mem[p] to zero directly
 mem[p] = 0
  • 45. Optimizations • opt_multiloop & opt_copyloop (Multiplication and Copy) • Command like [->+>+<<] is copy mem[p]'s value to mem[p+1] and mem[p+2], and set mem[p] to zero • If we know what this is doing, we can make it short
  • 46. Optimizations • opt_multiloop & opt_copyloop (Multiplication and Copy) • Same trick can apply to [->++<], make
 mem[p+1] = 2 * mem[p] and set mem[p] = 0 • Which is multiplication
  • 47. Optimizations • opt_offsetops (Operation Offsets) • In Brainf*ck, we know that we have a pointer indicating where we are now, and pointer usually move a lot • What if we can calculate offset for Instructions directly, so we don't need to move the pointer around
  • 48. Optimizations • opt_cancel (Cancel Instructions) • ++++-->>+-<<< do the same thing as ++< • Then, why waste all the time on these Instructons ?
  • 49. Can it run faster ? Yes — JIT
  • 54. Great! So I'll use JIT from now on
  • 55. Wait a sec... • Not every case can use JIT • Because JIT needs to warm-up and Analysis
 Maybe warm-up can take more time than your code actually run • And it's import to avoid to record the warm-up time when you want to do some benchmarking
  • 56. Wait a sec... • And do you really need JIT ? • It may cost a lot for one to import JIT to a project • Sometimes, maybe buy more server is a better choice than import JIT into your project
  • 57. Wait a sec... • But if you analyzed your project, know how difcult it is for you to import PyPy and JIT into your project, then you're good to go! • BTW, le size of executable with JIT Enabled is bigger than the one with No-JIT
  • 59. References • Tutorial: Writing an Interpreter with PyPy, part 1 • https://morepypy.blogspot.tw/2011/04/tutorial-writing-interpreter-with- pypy.html • PyPy - Tutorial for Brainf*ck Interpreter • http://wdv4758h.github.io/posts/2015/01/pypy-tutorial-for-brainfuck- interpreter/ • matslina/bfoptimization • https://github.com/matslina/bfoptimization/ • Virtual Machine Constructions for Dummies • https://www.slideshare.net/jserv/vm-construct