$30 off During Our Annual Pro Sale. View Details »

Rubinius - Tales from the trenches @ Railsclub.ru 2012

Rubinius - Tales from the trenches @ Railsclub.ru 2012

Dirkjan Bussink

September 15, 2012
Tweet

More Decks by Dirkjan Bussink

Other Decks in Programming

Transcript

  1. Rubinius
    Tales from the trenches

    View Slide

  2. Dirkjan Bussink
    @dbussink

    View Slide

  3. View Slide

  4. Huh?

    View Slide

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

    View Slide

  6. Beyond
    imagination

    View Slide

  7. View Slide

  8. View Slide

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

    View Slide

  10. Liskov
    Substitution
    Principle

    View Slide

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

    View Slide

  12. StackError

    View Slide

  13. View Slide

  14. 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!

    View Slide

  15. 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

    View Slide

  16. IDIOT

    View Slide

  17. GCC...

    View Slide

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

    View Slide

  19. View Slide

  20. 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

    View Slide

  21. Ruby is weird

    View Slide

  22. 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

    View Slide

  23. 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

    View Slide

  24. 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!

    View Slide

  25. 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

    View Slide

  26. Ruby’s C-API

    View Slide

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

    View Slide

  28. 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

    View Slide

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

    View Slide

  30. View Slide

  31. GC can
    be hard

    View Slide

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

    View Slide

  33. View Slide

  34. Old
    Young
    String Array
    Object
    String
    String Hash

    View Slide

  35. View Slide

  36. Concurrency
    is tricky

    View Slide

  37. Out of order
    execution

    View Slide

  38. 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

    View Slide

  39. 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

    View Slide

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

    View Slide

  41. Try your code
    We secretly love weird stuff

    View Slide

  42. спасибо

    View Slide