Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rubinius — seemingly the same but completely different

Rubinius — seemingly the same but completely different

Slides from a talk I delivered on 13.03.2013 at Munich Ruby Users Group meetup.

Ae7a42fb716793697b1d222f3cc753b8?s=128

Jan Stępień

March 13, 2013
Tweet

Transcript

  1. Rubinius seemingly the same but completely different

  2. My name is Jan Stępień and I come from Warsaw

  3. Rubinius an alternative Ruby VM

  4. Table of contents 1. Philosophy 2. JIT compilation 3. Garbage

    collection 4. Multithreading 5. The real life
  5. Philosophy

  6. MRI The main language is C

  7. My code is too slow ⇒ Rewrite it in C,

    C++, D, etc.
  8. Rubinius The main language is Ruby

  9. Array#insert static VALUE rb_ary_insert(int argc, VALUE *argv, VALUE ary) {

    long pos; if (argc < 1) { rb_raise(rb_eArgError, ”wrong number of arguments”); } rb_ary_modify_check(ary); if (argc == 1) return ary; pos = NUM2LONG(argv[0]); if (pos == -1) { pos = RARRAY_LEN(ary); } if (pos < 0) { pos++; } rb_ary_splice(ary, pos, 0, rb_ary_new4(argc - 1, argv + 1)); return ary; } class Array def insert(idx, *items) Rubinius.check_frozen return self if items.length == 0 idx = Rubinius::Type.coerce_to idx, Fixnum, :to_int idx += (@total + 1) if idx < 0 if idx < 0 raise IndexError, ”#{idx} out of bounds” end self[idx, 0] = items self end end (redacted for the sake of brevity and legibility)
  10. My code is too slow ⇒ Improve the virtual machine

  11. JIT compilation

  12. Just-in-time compilation i.e. compile as late as possible

  13. Compilation according to MRI require ’sinatra’ get ’/ping’ do content_type

    ’text/pong’ ’pong’ end → call self :get '/ping' :args → putself putstring ”sinatra” send :require, 1, nil, 8 pop trace putself putstring ”/ping” send :get, 1, <c>, 8
  14. Compilation according to Rubinius . . . → putself putstring

    ”sinatra” send :require, 1, nil, 8 pop trace putself putstring ”/ping” send :get, 1, <c>, 8 → xor eax, eax test rax, rax je 0xf01dab1e pop rbp mov edi, 0x600d80 jmp rax nop dword ptr [rax+0x0] mov eax, 0x600d80
  15. Compilation according to Rubinius ...what can be summarised as...

  16. Compilation according to Rubinius require ’sinatra’ get ’/ping’ do content_type

    ’text/pong’ ’pong’ end → xor eax, eax test rax, rax je 0xf01dab1e pop rbp mov edi, 0x600d80 jmp rax nop dword ptr [rax+0x0] mov eax, 0x600d80 LLVM
  17. Making Ruby Fast: e Rubinius JIT — Evan Phoenix https://blog.engineyard.com/2010/making-ruby-fast-the-rubinius-

    jit/
  18. Garbage collection

  19. Garbage collection in MRI Allocation ▶ A memory pool divided

    into blocks ▶ A list of unused chunks Freeing 1. Stop the virtual machine 2. Mark all active objects 3. Delete unmarked objects
  20. MRI spends 20% of time on garbage collection

  21. 20%

  22. 20%

  23. Garbage Collection and the Ruby Heap — Joe Damato

  24. Observation Majority of objects has a very short life

  25. Nursery → Young → Mature Large objects’ tank

  26. 5 ings You’ll Love About Rubinius — Brian Ford https://blog.engineyard.com/2009/5-things-youll-love-about-

    rubinius/ Garbage Collector – Dirkjan Bussink https://github.com/rubinius/rubinius/blob/master/web/doc/en/memory- system/garbage-collector.markdown
  27. Multithreading

  28. Concurrency vs. Parallelism

  29. Concurrency thread β thread α time

  30. Parallelism thread β thread α time

  31. threads = [1, 2].map do |x| Thread.new do 100_000_000.times {

    x = -x } end end threads.map &:join
  32. $ time ruby threads.rb 30.73user 0.03system 0:31.45elapsed 97%CPU (0avgtext+0avgdata 5032maxresident)k

    2032inputs+0outputs (1major+1354minor)pagefaults $ time rbx threads.rb 20.53user 0.04system 0:10.92elapsed 188%CPU (0avgtext+0avgdata 34280maxresident)k 0inputs+0outputs (0major+9166minor)pagefaults
  33. mri 31,5 s 5 MB 97% rbx 10,9 s 34

    MB 188%
  34. The real life

  35. A handful of benchmarks Real ▶ rails — 1 000

    requests to a simple Rails 3.2 application ▶ sinatra — 10 000 requests to a simple Sinatra app Unreal ▶ matrices — matrix multiplication (from The Computer Language Benchmarks Game) ▶ threads — as seen on an earlier slide
  36. 0 10 20 30 40 50 rails sinatra threads matrices

    rbx mri Time of execution [s] Rubinius: commit fd80bd compiled with GCC 4.7.2. MRI: Arch Linux x86-64 package, version 1.9.3_p362-1. Rails and Sinatra served using Thin. Rails and Sinatra tests were preceded with sending 1k unmeasured requests to warm up JIT and caches.
  37. Rubinius – Use Ruby — Karol Hosiawa, KRUG 11.2011 http://hosiawak.github.com/rubinius_presentation/

  38. jan@stepien.cc http://stepien.cc/~jan @janstepien Danke schön Dziękuję pięknie

  39. ese slides were typeset with L A TEX. © 2013

    Jan Stępień. Some rights reserved.