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
13k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.5k
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
410
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
770
Ruby 3.0 JIT on Rails
k0kubun
9
9.1k
Other Decks in Programming
See All in Programming
良いユニットテストを書こう
mototakatsu
8
2.8k
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
510
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
140
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
550
Spatial Rendering for Apple Vision Pro
warrenm
0
110
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
0
160
たのしいparse.y
ydah
3
120
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
140
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
480
Amazon S3 NYJavaSIG 2024-12-12
sullis
0
100
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
94
13k
Git: the NoSQL Database
bkeepers
PRO
427
64k
The Cult of Friendly URLs
andyhume
78
6.1k
Side Projects
sachag
452
42k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Music & Morning Musume
bryan
46
6.2k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
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!!!