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
14k
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
13k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.6k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
1
2.1k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
11k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
430
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
1.9k
数時間かかる週一リリースを毎日何度も爆速でできるようにするまで / CI/CD Conference 2021
k0kubun
21
14k
Ruby 3 JIT's roadmap / RubyConf China 2020
k0kubun
0
790
Ruby 3.0 JIT on Rails
k0kubun
9
9.2k
Other Decks in Programming
See All in Programming
『GO』アプリ バックエンドサーバのコスト削減
mot_techtalk
0
140
Introduction to kotlinx.rpc
arawn
0
690
バックエンドのためのアプリ内課金入門 (サブスク編)
qnighy
8
1.8k
第3回関東Kaggler会_AtCoderはKaggleの役に立つ
chettub
3
1k
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
900
さいきょうのレイヤードアーキテクチャについて考えてみた
yahiru
3
750
仕様変更に耐えるための"今の"DRY原則を考える / Rethinking the "Don't repeat yourself" for resilience to specification changes
mkmk884
0
110
Compose でデザインと実装の差異を減らすための取り組み
oidy
1
310
PHPのバージョンアップ時にも役立ったAST
matsuo_atsushi
0
100
Conform を推す - Advocating for Conform
mizoguchicoji
3
690
Formの複雑さに立ち向かう
bmthd
1
840
Honoをフロントエンドで使う 3つのやり方
yusukebe
7
3.2k
Featured
See All Featured
How GitHub (no longer) Works
holman
314
140k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
A designer walks into a library…
pauljervisheath
205
24k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Scaling GitHub
holman
459
140k
Into the Great Unknown - MozCon
thekraken
35
1.6k
GraphQLとの向き合い方2022年版
quramy
44
13k
Typedesign – Prime Four
hannesfritz
40
2.5k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
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!!!