Pass by value, pass by reference and DRbUndumped

dRuby copies objects it can marshal and sends a reference for the rest. How to tell which you get, and how DRb::DRbUndumped forces a reference.

Updated

When an object crosses a dRuby connection as an argument or a return value, one of two things happens. If it can be marshaled, the other side receives a copy (pass by value). If it cannot, or if it includes DRb::DRbUndumped, the other side receives a DRbObject proxy that points back to the original (pass by reference).

Pass by value: you get a copy

# server.rb
require 'drb/drb'

class Notebook
  def initialize
    @lines = ['first line']
  end

  def lines
    @lines
  end

  def add(text)
    @lines << text
  end
end

DRb.start_service('druby://localhost:8787', Notebook.new)
DRb.thread.join
# client.rb
require 'drb/drb'

DRb.start_service
notebook = DRbObject.new_with_uri('druby://localhost:8787')

notebook.lines << 'lost'   # changes a local copy only
p notebook.lines           # => ["first line"]

notebook.add('kept')       # runs on the server
p notebook.lines           # => ["first line", "kept"]

notebook.lines returns an Array, which is marshaled and rebuilt in the client. Appending to that copy does nothing to the server's array. To change server state, call a method that changes it on the server.

Pass by reference: DRbUndumped

Include DRb::DRbUndumped in a class to say "never copy instances of this, send a reference".

class Page
  include DRb::DRbUndumped

  attr_accessor :title

  def initialize(title)
    @title = title
  end
end

class Book
  def initialize
    @page = Page.new('Untitled')
  end

  def page
    @page
  end
end

If Book.new is the front object, book.page in the client is now a DRbObject. Calling book.page.title = 'Chapter 1' sends the call to the server and changes the real Page.

You can also mark a single object with obj.extend(DRb::DRbUndumped).

What dRuby decides for you

Objects that Marshal cannot dump are passed by reference automatically. That includes procs, IO objects such as files and sockets, and threads.

Things that catch people out

  • The class must exist on both sides for a copy. If the receiver does not have the class definition loaded, it gets a DRb::DRbUnknown placeholder instead of the object.
  • A reference needs a server at its origin. Passing a by-reference object from the client only works if the client has called DRb.start_service, because the reference contains the client's URI. See callbacks and blocks.
  • A remote reference does not keep the object alive. The proxy on the other side is not known to the garbage collector in the owning process. Keep your own reference to anything you hand out, for as long as others may call it.
  • Every call on a reference is a network round trip. A copy is cheap to read many times; a reference is not.

Which to choose

You want Use
A snapshot of data Pass by value (the default)
Shared, changing state DRb::DRbUndumped
To hand over a large object without copying it DRb::DRbUndumped