is Ruby

slow

?

TDC2012 São Paulo

whoami

@qmx

dynjs
JRuby

is

Ruby

slow

?

disclaimer

why?

powerful!

deep dive

Object Orientation

:)

Object Orientation

:(

OH: sometimes, if you really care about performance, you need to write C in Java

what about

Ruby?

Ruby <3

						2.times { puts "yay!"}
					

Ruby <3

						2.send(:times) { puts "yay!"}
					

extra

late

dispatch

dispatch?

what is a

Ruby

class?

						{:methods => {:times => ...}}
					

hashmap!

calling a

method

calling a method

  1. get the object
  2. lookup the method
  3. call it
  4. profit

lookup a method?

seems legit

lookup a method?

						class A
  def lol; puts "lol"; end
end
class B < A; end
class C < B; end
					

lookup a method?

						C.new.lol
					

easy!

lookup the method

						C.new.methods.include? :lol
 => true
					

what sorcery is this?

making things

worse

modules

modules

						module X
  def beep
    puts "beep beep"
  end
end
					

modules

						class C
  include X
end
					

goodbye

simple

lookup

making things

even

worse

open

classes

open classes

						class A
  def lol
    puts "wut" 
  end
end
					

open classes

						> C.new.lol
wut
=> nil 
						
					

this is

amazing

and dangerous

classes are

mutable

Objects

innocuous code

						my_hash = {}
class << my_hash
  include X
end
					

innocuous code?

						10.times do
  Thread.new do
    my_hash = {}
    class << my_hash
      include X
    end
  end
end
					

guilty

or

innocent?

concurrency

should I

care?

double dispatch

hard to

optimize

double dispatch

(almost)

kills

inlining

Car.find_by_name

method_missing

method_missing

also known as

fallback or slow path

eval

considered harmful?

what's wrong with

eval?

absolutely

no guarantees

binding

binding

						x = "hello"
eval("puts x")
hello
=> nil

					

Ruby

inconstants

WTF?

inconstants

						A_CONSTANT = 3
A_CONSTANT = 4
warning: already initialized constant A_CONSTANT
>> A_CONSTANT
=> 4
					

WAT?

what about

Garbage Collection?

what about

Threading?

(and GIL)

implementation-specific

JRuby YARV Rubinius MagLev MRI

pick your poison

censored

biased speaker

JRuby wins ;)

what

JRuby

does?

late dispatch?

open classes?

inlining

ALL THE THINGS

kidding

JRuby makes the method calls

inlinable

let the

JVM

do the dirty work

open classes?

metaclasses

method_missing?

TANSTAAFL

okay :(

inconstants?

invokedynamic

wrapping up

it's all about

trade-offs

Ruby's goal is to make programmers

happy

with great power comes great responsability

thanks!

?

thanks!