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.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
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
150
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
4
650
我々のデザインシステムは Chakra v3 にアップデートします
shunya078
2
3k
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
230
Discord Bot with AI -for English learners-
xin9le
1
120
CSC509 Lecture 14
javiergs
PRO
0
130
TypeScript でバックもやるって実際どう? 実運用で困ったこと3選
yuichiro_serita
17
7.6k
Recoilを剥がしている話
kirik
4
5.9k
Full stack testing :: basic to basic
up1
1
900
DevTools extensions で 独自の DevTool を開発する | FlutterKaigi 2024
kokiyoshida
0
460
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
140
アニメーションを最深まで理解してパフォーマンスを向上させる
mine2424
0
110
Featured
See All Featured
Docker and Python
trallard
41
3.1k
Making Projects Easy
brettharned
116
5.9k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
420
Designing for Performance
lara
604
68k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Visualization
eitanlees
145
15k
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!!!