Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News MacRuby 0.6 With GCD and Threading Improvements, Fast Debugger, AOT

MacRuby 0.6 With GCD and Threading Improvements, Fast Debugger, AOT

This item in japanese

MacRuby 0.6 іs now available (MacRuby 0.6 Download Link).

As MacRuby 0.6 Release Notes say, MacRuby 0.6 is now considered stable for Cocoa development, complete with XCode and Ahead of Time (AOT) support, which means it's possible to compile the Ruby source and only ship the resulting binaries.

As announced previously, the 0.6 release now comes with experimental debugging support. MacRuby's debugging support inserts special instructions in the compiled code to trigger breakpoints, as compared to many other Ruby debuggers which use the tracing support or use Ruby VM hooks.
A note from the release blog post:

An interesting feature of the debugger is that it has been abstracted into a simple Objective-C API, of which macrubyd is just one client. In the future we might see other clients.

Comparable Java and Ruby libraries are available in the Debug-Commons project.

With MacRuby 0.5, MacRuby removed the GIL, allowing threads to run in parallel (unlike Ruby 1.9 where only one Ruby thread can run at a time). Some new improvements in 0.6 make multiple threads more useful, eg. (from the release blog post):

Finally, the Regexp class has been totally rewritten in this release. It is now using the ICU framework instead of Oniguruma for regular expression compilation and pattern matching. Since ICU is thread-safe, MacRuby 0.6 allows multiple threads to utilize regular expressions in a very efficient way, which was not possible previously.

MacRuby 0.5 already contained the beginnings of support for Grand Central Dispatch (GCD), Apple's system wide threadpool and task system available on Mac OS X Snow Leopard and on Phone OS 4.0. MacRuby 0.6 adds new features and abstractions in the 'dispatch' library.

Job offers a similar interface as Thread, and can be used to asynchronously execute code. It can be used synchronously or asynchronously. For instance, here the code to have a Job do some work and wait for the Job to finish (from the docs):

@result = job.value
puts @result.to_int.to_s.size # => 51

The asynchronous way requires to pass in a callback that is called once the Job is finished; in Ruby this is, of course, done with a block:

job.value {|v| p v.to_int.to_s.size } # => 51 (eventually)

Another tool introduced in MacRuby's Dispatch library is  Proxy, which allows to serialize method calls on an object.

Other features are parallel iteration, ie. parallel implementations of iterators, Enumerable#p_each, Enumerable#p_map, Enumerable#p_find, and similar methods which make use of multiple threads to execute all the block invocations.

GCD also features Event Sources, which allow to register interest in certain events and have them handled by blocks scheduled on GCD queues. Event sources can be timers, process signals, input descriptors for I/O, etc. Event Sources are a powerful tool to make use of the OS-wide thread pool by running event handlers on a queue which grabs a thread when an event comes in and releases it back to the system after the event handler was run.
The source code of the dispatch library in MacRuby has a README for a more detailed introduction to all of these GCD features in MacRuby 0.6.

One issue that was found after the release of MacRuby 0.6 seems to cause problems with scheduling a block to run on the main thread from a background thread. Something to watch out for in case of weird threading problems; the bug report contains a workaround and a solution should come to MacRuby's trunk soon.

The big question for every Ruby implementation is of course compatibility; the state in MacRuby 0.6 (from the release blog post):

MacRuby 0.6 provides support for C extensions written for the original implementation of ruby. We were able to successfully use the Nokogiri, SQLite3 and PostgreSQL extensions from MacRuby.

This release also passes about 85% of RubySpecs, is able to run a modified version of Rails 3, and implements better support for Ruby 1.9 encodings.

Finally, Matt Aimonetti is working on a book for O'Reilly; the first few chapters of "MacRuby: The Definite Guide" are already available and can be read online.

Rate this Article