Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby 2.6 JIT / RubyConf 2018
Search
Takashi Kokubun
November 15, 2018
Programming
15
13k
Ruby 2.6 JIT / RubyConf 2018
RubyConf 2018 Los Angeles
Takashi Kokubun
November 15, 2018
Tweet
Share
More Decks by Takashi Kokubun
See All by Takashi Kokubun
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
12k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.4k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
1
2k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
11k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
400
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
1.8k
数時間かかる週一リリースを毎日何度も爆速でできるようにするまで / CI/CD Conference 2021
k0kubun
21
14k
Ruby 3 JIT's roadmap / RubyConf China 2020
k0kubun
0
760
Ruby 3.0 JIT on Rails
k0kubun
9
9.1k
Other Decks in Programming
See All in Programming
現場で役立つモデリング 超入門
masuda220
PRO
15
3.1k
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
200
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
110
C++でシェーダを書く
fadis
6
4k
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
200
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
350
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
160
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
470
CSC509 Lecture 12
javiergs
PRO
0
140
RubyLSPのマルチバイト文字対応
notfounds
0
100
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
340
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Done Done
chrislema
181
16k
Designing the Hi-DPI Web
ddemaree
280
34k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Typedesign – Prime Four
hannesfritz
40
2.4k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
We Have a Design System, Now What?
morganepeng
50
7.2k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Fireside Chat
paigeccino
33
3k
Agile that works and the tools we love
rasmusluckow
327
21k
YesSQL, Process and Tooling at Scale
rocio
168
14k
BBQ
matthewcrist
85
9.3k
Transcript
Takashi Kokubun @k0kubun Ruby 2.6 JIT
@k0kubun Takashi Kokubun / Arm Ruby Committer for JIT, ERB
None
None
None
None
Ruby 2.6
None
What's "JIT"?
JIT: Just In Time Compiler
None
History of Ruby implementation
def plus(a, b) a + b end + a b
Parse Ruby 1.8 Traverse
def plus(a, b) a + b end + a b
Compile Ruby 1.9~2.5 Interpret getlocal :a getlocal :b send :+
def plus(a, b) a + b end + a b
JIT Ruby 2.6 (New!) Execute getlocal :a getlocal :b send :+ mov %rdi,%rdx mov %rsi,%rax add %rdx,%rax
How can we use it?
or ruby --jit ... RUBYOPT="--jit" rails s ...
Let's benchmark Ruby 2.6 JIT
None
https://gist.github.com/k0kubun/887b9567afa86b326556389ef00e4200
https://gist.github.com/k0kubun/887b9567afa86b326556389ef00e4200 Ruby 2.0: 34.3 Ruby 2.6: 86.7 2.53x faster
https://gist.github.com/k0kubun/887b9567afa86b326556389ef00e4200 Ruby 2.5: 48.3 Ruby 2.6: 86.7 1.80x faster
Achieved Ruby 3x2.5 !!!
How about other benchmarks?
None
None
JIT makes things slower?
Today's topic JIT performance characteristics
When does Ruby become slow on JIT?
1. When there are many JIT-ed methods
--jit-max-cache (default: 1000)
JIT by C compiler & dynamic loading
unused space code readonly data 1 method Heap
unused space code readonly data 1 method Heap 2MB (big)
about 4KB ?KB
unused space code readonly data unused space code readonly data
unused space code readonly data 3 methods Heap
unused space code readonly data unused space code readonly data
unused space code readonly data L2, ... cache L1 cache Heap
unused space code readonly data unused space code readonly data
unused space code readonly data L2, ... cache L1 cache Heap
unused space Heap code readonly data unused space code readonly
data unused space code readonly data L2, ... cache L1 cache
2. When there are still methods to be JIT-ed
"JIT compaction"
unused space code readonly data unused space code readonly data
unused space code readonly data When it reaches --jit-max-cache, it fires "JIT compaction"
unused space code readonly data unused space code readonly data
unused space code readonly data unused space code code code readonly data readonly data readonly data
unused space code readonly data unused space code readonly data
unused space code readonly data unused space code code code readonly data readonly data readonly data L2, ... cache
CPU/memory resources for C compiler
Locks on GC, waitpid, ...
3. When TracePoint is enabled (to be fixed after Ruby
2.6)
What's TracePoint? When is it enabled? • gem 'web-console' on
your Rails application • byebug or binding.pry (with pry-byebug) • Measuring coverage
Summary: When JIT makes Ruby slow Optcarrot Rails 1. When
there are many JIT-ed methods ✓ 2. When there are still methods to be JIT-ed (Is it a short benchmark?) ✓ 3. When TracePoint is enabled
What is made faster by JIT?
1. Almost all methods
def plus(a, b) a + b end Virtual Machine getlocal
:a getlocal :b send :+ Program Counter Stack Pointer Computer Registers Instruction Pointer
def plus(a, b) a + b end Virtual Machine getlocal
:a getlocal :b send :+ Program Counter Stack Pointer Computer JIT Instruction Pointer Registers mov %rdi,%rdx mov %rsi,%rax add %rdx,%rax
2. Basic operators on core classes
VM-optimized methods Integer, Float +, -, *, /, % Array
+, <<, [], []=, empty?, size, length, min, max Hash [], []=, empty?, size, length String +, <<, =~, empty?, size, length, succ common !, !=, ==, <, >, <=, >=
def three 1 + 2 end putobject 1 putobject 2
send :+ JIT mov $0x3,%eax retq Method inlining on JIT
3. Calling Ruby method
Method dispatch on Ruby VM foo.bar
Method dispatch on Ruby VM foo.bar Method search (slow)
Method dispatch on Ruby VM foo.bar Method search (slow) Ruby
method C function attr_reader alias Method entry Foo#bar
Method dispatch on Ruby VM foo.bar Method search (slow) Verify
cache Ruby method C function attr_reader alias Method entry Foo#bar has cache
Method dispatch on Ruby VM foo.bar Method search (slow) Verify
cache Ruby method C function attr_reader alias Method entry Foo#bar has cache redefined?
Method dispatch on Ruby VM foo.bar Method search (slow) Verify
cache Ruby method C function attr_reader alias Method entry Foo#bar has cache redefined?
Method dispatch on JIT foo.bar Verify cache Ruby method Method
entry Foo#bar has cache Cancel JIT! Less branches Less memory access redefined? some inlining
4. Instance variable access
Reading instance variable on VM @foo
Reading instance variable on VM @foo Search index (many branches,
slow)
Reading instance variable on VM @foo Search index (many branches,
slow) self.class. instance_variables[index = 2]
Reading instance variable on VM Search index (many branches, slow)
self.class. instance_variables[index = 2] @foo has cache Verify cache
Reading instance variable on VM Search index (many branches, slow)
self.class. instance_variables[index = 2] @foo different class? has cache Verify cache
Reading instance variable on VM Search index (many branches, slow)
self.class. instance_variables[index = 2] @foo different class? has cache Verify cache
Reading instance variable on JIT self.class. instance_variables[2] @foo different class?
has cache Verify cache Cancel JIT! inlined index Less branches Less memory access
Summary: When JIT makes Ruby fast Optcarrot Rails 1. Almost
all methods ✓ ✓ 2. Basic operators on core classes ✓ 3. Calling Ruby method ✓ ✓ 4. Instance variable access ✓ ?
Future of Ruby's JIT
Will Ruby 2.6 be fast only on Optcarrot?
Future idea (not for 2.6) • Stack allocation of objects
- That requires "escape analysis" (not easy) - We haven't estimated its possible impact yet
Some rooms for improvements in 2.6 (!?) • Change heuristics
to trigger/compact JIT • Profile-guided optimization • Method inlining
How can we experiment them? Benchmark!
None
None
Create a benchmark for Ruby 3x3?
benchmark_driver.gem
None
None
None
None
Conclusion • Ruby 2.6.0-preview3 JIT is still early days -
Small --jit-max-cache might be an option for now • We still have chance to make Ruby 2.6.0 better - Benchmarks are wanted!!!