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

Rubinius - Tales from the trenches @ Baruco 2012

Rubinius - Tales from the trenches @ Baruco 2012

Dirkjan Bussink

September 09, 2012
Tweet

More Decks by Dirkjan Bussink

Other Decks in Technology

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. Liskov
    Substitution
    Principle

    View Slide

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

  11. StackError

    View Slide

  12. View Slide

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

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

  15. IDIOT

    View Slide

  16. GCC...

    View Slide

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

    View Slide

  18. View Slide

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

  20. Ruby is weird

    View Slide

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

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

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

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

  25. Ruby’s C-API

    View Slide

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

    View Slide

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

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

    View Slide

  29. View Slide

  30. GC can
    be hard

    View Slide

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

    View Slide

  32. View Slide

  33. Old
    Young
    String Array
    Object
    String
    String Hash

    View Slide

  34. View Slide

  35. Concurrency
    is tricky

    View Slide

  36. Out of order
    execution

    View Slide

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

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

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

    View Slide

  40. Try your code
    We secretly love weird stuff

    View Slide