InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Declarative Concurrency For Ruby With Dataflow

Posted by Sebastien Auvray on May 04, 2009

Sections
Development,
Architecture & Design
Topics
Language Design ,
Ruby ,
Performance & Scalability
Tags
Erlang ,
Functional Programming ,
Concurrency

Part of the Ruby language was influenced by functional programming techniques. Ruby programmers then adopted techniques from Erlang or Haskell with, or created bridges to these languages, eg. Erlectricity.

Larry Diehl brings a declarative concurrent model to Ruby by importing the concept of unification from Oz Language. Oz is a multiparadigm programming language. It is mainly known as a functional (lazy and eager evaluation), distributed, and concurrent programming language, but also supports constraint, logic, imperative and object-oriented programming.

In light of the rising number of processor cores, Larry wanted to take advantage of the declarative concurrency model in Ruby. The advantages are:

  • It is easy to reason about what the program does
  • Simple but powerful concurrency is possible

To accomplish this, Larry uses the concept of Dataflow threading behavior as described in Concepts, Techniques, and Models of Computer Programming:

What happens if an operation tries to use a variable that is not yet bound? From a purely aesthetic point of view, it would be nice if the operation would simply wait. Perhaps some other thread will bind the variable, and then the operationcan continue. This civilized behavior is known as dataflow.

And to realize this the Oz-way, he uses unification:

The idea of unification is to describe values by logical equations which can be resolved automatically by some unification algorithm

Practically this will be transcribed this way (from the project site):

# Local variables
include Dataflow

local do |x, y, z|
  # notice how the order automatically gets resolved
  Thread.new { unify y, x + 2 }
  Thread.new { unify z, y + 3 }
  Thread.new { unify x, 1 }
  z #=> 6
end

You create new variables with local or declare (for Instance variable), and use unify to bind variables.

Examples of Oz Ports (nondeterministic behavior), or Erlang-ish Actors  is provided.

Brian Morearty adresses some interesting debugging, performance, and memory concerns of Dataflow usage. Larry Diehl points out on Brian's blog:

One thing to note is that this library makes JRuby shine over MRI due to its green threads + native thread pool implementation.

No comments

Watch Thread Reply

Educational Content

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.

Beauty Is in the Eye of the Beholder

Alex Papadimoulis discusses ugly code, where it comes from, how to avoid it, and how to get rid of it.

Architecting Visa for Massive Scale and Continuous Innovation

John Davies examines Visa’s architecture and shows how enterprises have architected complex integrations incorporating Hadoop, memcached, Ruby on Rails, and others to deliver innovative solutions.

Max Protect: Scalability and Caching at ESPN.com

Sean Comerford unveils ESPN.com’s architecture, what components are used and why, and the current changes the website goes through.

The Seven Deadly Sins of Enterprise Agile Adoption

Are there repeated patterns of failure on Enterprise Agile Enablement efforts? Sanjiv and Arlen discuss Seven Deadly Sins to avoid when adopting Agile in an enterprise.

Questions for an Enterprise Architect

Erik Dörnenburg answers: What is Enterprise and Evolutionary Architecture?, discussing 4 issues: Turning strategy into execution, Ensuring conformance, Where do the architects sit? Buying or building?

Wrap Your SQL Head Around Riak MapReduce

Sean Cribbs explains what Map-Reduce and Riak are, why and how to use Map-Reduce with Riak, and how to convert SQL queries into their Map-Reduce equivalents.