Slide 1

Slide 1 text

Rubinius Tales from the trenches

Slide 2

Slide 2 text

Dirkjan Bussink @dbussink

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Huh?

Slide 5

Slide 5 text

Class.stub!(:new).and_return(MyClass)

Slide 6

Slide 6 text

Beyond imagination

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

class Foo class << self class << self def foo; end end end end class Bar < Foo; end p Bar.singleton_class.foo

Slide 10

Slide 10 text

Liskov Substitution Principle

Slide 11

Slide 11 text

class SomeList < Array def push(value) value = Proxy.new(value) if value.is_a?(String) ... end end alias_method :<<, :push end Unmarshal uses Array#<<

Slide 12

Slide 12 text

StackError

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

class PropertySet < Array alias_method :superclass_slice, :[]= private :superclass_slice def []=(name, property) self << property end def <<(property) found = named?(property.name) add_property(property) if found superclass_slice(index(property), property) else super end end end WUT? Ugh Oh, let’s change the meaning!

Slide 15

Slide 15 text

class PropertySet def []=(name, property) add_property(property) end def <<(property) add_property(property) end def add_property(property) clear_cache @order << property unless @order.include?(@property) @properties[property.name] = property end end

Slide 16

Slide 16 text

IDIOT

Slide 17

Slide 17 text

GCC...

Slide 18

Slide 18 text

vm/builtin/regexp.cpp:312: warning: control may reach end of non-void function

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

GCC is fucking stupid. Avoid inane gcc type punning warning Work around GCC's broken strict aliasing checks. gcc 4.2.1 on freebsd 9 wants a newline at the end Make the gcc gods happy Make the stupid compiler gods happy Appease GCC 4.3

Slide 21

Slide 21 text

Ruby is weird

Slide 22

Slide 22 text

class MyClass def to_ary puts "we're here" [1] end end def my_method arg yield arg end my_method(MyClass.new) do |*arg| p arg end we're here [#] we're here [1] we're here [[1]] [#] A B C D Call to_ary and ignore

Slide 23

Slide 23 text

can't define singleton method "method" for Float obj = 2.0 def obj.method puts "method on float instance" end obj.method No singleton methods on Numeric

Slide 24

Slide 24 text

class Numeric def singleton_method_added(name) end end obj = 2.0 def obj.method puts "method on float instance" end obj.method method on float instance Singleton method on a Numeric!

Slide 25

Slide 25 text

3 4 5 6 Becomes true on first condition, false on second 10.times do |i| if (i == 3) .. (i == 6) puts i end end

Slide 26

Slide 26 text

Ruby’s C-API

Slide 27

Slide 27 text

There is no C-API just internals of MRI that people use

Slide 28

Slide 28 text

VALUE rb_data_object_alloc(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree) { NEWOBJ(data, struct RData); if (klass) Check_Type(klass, T_CLASS); OBJSETUP(data, klass, T_DATA); data->data = datap; data->dfree = dfree; data->dmark = dmark; return (VALUE)data; } MRI internals

Slide 29

Slide 29 text

NotImplementedError: method `class' called on terminated object (0xXXXXXXXX) Inspect object created with rb_data_object_alloc

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

GC can be hard

Slide 32

Slide 32 text

C Stack Object* Object* Heap 1 Object Object Heap 2 Object Object GC moves objects

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Old Young String Array Object String String Hash

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Concurrency is tricky

Slide 37

Slide 37 text

Out of order execution

Slide 38

Slide 38 text

a = 0 b = 0 c = 0 d = 0 Core 1 Core 2 a = 1 c = 1 b = 2 d = 2 Statements can be executed in different order

Slide 39

Slide 39 text

addr = Address.new addr.street = "Somestreet" addr.number = 1 shared_addr = addr shared_addr => nil shared_addr => # shared_addr => # shared_addr => # shared_addr => # shared_addr = addr executed before other assignments

Slide 40

Slide 40 text

addr = Address.new addr.street = "Somestreet" addr.number = 1 Rubinius.memory_barrier shared_addr = addr

Slide 41

Slide 41 text

Try your code We secretly love weird stuff

Slide 42

Slide 42 text

спасибо