Slide 1

Slide 1 text

EXIT(ing) Through the YJIT

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Eileen M. Uchitelle Twitter / GitHub: @eileencodes Mastadon: @[email protected]

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

EXIT(ing) Through the YJIT

Slide 7

Slide 7 text

What is a JIT?

Slide 8

Slide 8 text

What is a JIT? "Just in time" compiler

Slide 9

Slide 9 text

MJIT "Method-Based" JIT

Slide 10

Slide 10 text

YJIT "Yet Another" JIT

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

def call_method(arg) if arg == "hello" puts arg else puts "bye" end end YJIT only compiles code that is run

Slide 13

Slide 13 text

def call_method(arg) if arg == "hello" puts arg else puts "bye" end end call_method("hello") YJIT only compiles code that is run

Slide 14

Slide 14 text

def call_method(arg) if arg == "hello" puts arg else puts "bye" end end call_method("hello") YJIT only compiles code that is run

Slide 15

Slide 15 text

def call_method able_to_compile not_able_to_compile end call_method YJIT can compile parts of methods

Slide 16

Slide 16 text

def call_method able_to_compile not_able_to_compile end call_method YJIT can compile parts of methods

Slide 17

Slide 17 text

def call_method able_to_compile not_able_to_compile end call_method YJIT can compile parts of methods

Slide 18

Slide 18 text

How is YJIT implemented?

Slide 19

Slide 19 text

MJIT vs YJIT Which to use?

Slide 20

Slide 20 text

How can I use YJIT?

Slide 21

Slide 21 text

$ docker pull rubylang/ruby:master-debug- nightly-focal Docker Images

Slide 22

Slide 22 text

Pre-requisites • autoconf • make • bison • GCC or Clang • libyaml • [email protected] • Rust (for YJIT) • Ruby

Slide 23

Slide 23 text

Run autogen $ cd ruby $ ./autogen.sh

Slide 24

Slide 24 text

Configure Ruby $ ./configure --prefix=~/.rubies/ruby-yjit --with-openssl-dir=$(brew --prefix [email protected]) --disable-install-doc

Slide 25

Slide 25 text

Configure Ruby: Set prefix $ ./configure --prefix=~/.rubies/ruby-yjit --with-openssl-dir=$(brew --prefix [email protected]) --disable-install-doc
 


Slide 26

Slide 26 text

Configure Ruby: openssl $ ./configure --prefix=~/.rubies/ruby-yjit --with-openssl-dir=$(brew --prefix [email protected]) --disable-install-doc

Slide 27

Slide 27 text

Configure Ruby: Disable rdoc $ ./configure --prefix=~/.rubies/ruby-yjit --with-openssl-dir=$(brew --prefix [email protected]) --disable-install-rdoc
 


Slide 28

Slide 28 text

Install Ruby $ make install

Slide 29

Slide 29 text

$ RUBY_YJIT_ENABLE=1 ruby my_script.rb $ ruby --yjit my_script.rb $ ruby --jit my_script.rb Enabling YJIT

Slide 30

Slide 30 text

YJIT dev mode $ ./configure --enable-yjit=dev --prefix=~/.rubies/ruby-yjit --with-openssl-dir=$(brew --prefix [email protected]) $ make install

Slide 31

Slide 31 text

"Exit" When YJIT is unable to compile

Slide 32

Slide 32 text

ar_demo.rb ActiveRecord::Base.establish_connection( adapter: "sqlite3", database: ":memory:") ActiveRecord::Schema.define do create_table :posts, force: true do |t| end end class Post < ActiveRecord::Base end class BugTest < ActiveSupport::TestCase def test_create Post.create! end end

Slide 33

Slide 33 text

Running with stats enabled $ bundle exec ruby --yjit-stats ar_demo.rb

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

But what does the code look like?

Slide 39

Slide 39 text

"We can totally make that work." - Aaron Patterson

Slide 40

Slide 40 text

Tracing YJIT Exits

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Tracing exits $ bundle exec ruby --yjit-trace-exits ar_demo.rb

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

ar_demo.rb [...] class Post < ActiveRecord::Base def call_method(options) one, two, three = options end end class BugTest < ActiveSupport::TestCase def test_create post = Post.create! post.call_method("hello") end end

Slide 48

Slide 48 text

Fixing broken code $ bundle exec ruby --yjit-trace-exits --yjit-call-threshold=1 ar_demo.rb

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

How will we use this?

Slide 53

Slide 53 text

Optimizing YJIT

Slide 54

Slide 54 text

yjit/src/codegen.rs /// Maps a YARV opcode to a code generation function (if supported) fn get_gen_fn(opcode: VALUE) -> Option { let VALUE(opcode) = opcode; let opcode = opcode as ruby_vminsn_type; assert!(opcode < VM_INSTRUCTION_SIZE); match opcode { [...] YARVINSN_send => Some(gen_send), YARVINSN_invokeblock => Some(gen_invokeblock), YARVINSN_invokesuper => Some(gen_invokesuper), YARVINSN_leave => Some(gen_leave), [...]

Slide 55

Slide 55 text

yjit/src/codegen.rs /// Maps a YARV opcode to a code generation function (if supported) fn get_gen_fn(opcode: VALUE) -> Option { let VALUE(opcode) = opcode; let opcode = opcode as ruby_vminsn_type; assert!(opcode < VM_INSTRUCTION_SIZE); match opcode { [...] YARVINSN_send => Some(gen_send), YARVINSN_invokeblock => Some(gen_invokeblock), YARVINSN_invokesuper => Some(gen_invokesuper), YARVINSN_leave => Some(gen_leave), [...]

Slide 56

Slide 56 text

yjit/src/codegen.rs asm.jne( counted_exit!( ocb, side_exit, invokesuper_block ).into() );

Slide 57

Slide 57 text

Optimizing our Ruby Code

Slide 58

Slide 58 text

Try YJIT on your app!

Slide 59

Slide 59 text

Help us improve YJIT

Slide 60

Slide 60 text

Eileen M. Uchitelle Twitter / GitHub: @eileencodes Mastadon: @[email protected]