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.

Performance Roundup: Heap Stacks Boost Threads in 1.8.x, MacRuby AOT, ZenProfile and EventHooks

Posted by Werner Schuster on May 23, 2009

Sections
Development,
Architecture & Design
Topics
Compilers ,
Ruby ,
Runtimes ,
Performance & Scalability
Tags
Scalability ,
Ruby1.9 ,
Threading ,
Virtual Machines

Ruby 1.9 moved the Ruby world from 1.8.x's userspace thread system to native threads. While 1.9's native threads still suffer from the GVL (Global VM Lock), which allows only one Ruby thread to be executed at a time, the switch to native threads brought other benefits.

Joe Damato explores one problem in Ruby 1.8.x's thread implementation which went away with native threads in 1.9. In short: context switches in 1.8.x are quite expensive, since they cause a thread's complete stack contents to be copied; from the stack to the heap (for the suspended thread) and in the other direction for the scheduled thread. Applications with large stacks or with huge stack frames suffer from this implementation detail.

Native thread implementations avoid this inefficiency by maintaining multiple stacks and switching between them. Joe's post is a very detailed description of his "heap stacks", which bring this approach to Ruby 1.8.x.
The performance improvements are significant, ranging from 2x increases up to ~10x increases, which bring the benchmark results close to the results of 1.9.1.
Patched versions of the code are available on GitHub: for 1.8.6 and for 1.8.7.

The Heap Stacks solution is yet another attempt to eradicate the biggest inefficiencies of Ruby 1.8.x, along with the MBARI patches which fixed some long standing issues with continuations and the GC.

Another path to better Ruby performance is taken by the MacRuby project, which has recently started work on an LLVM based VM. Some of that work has now been used to create an Ahead Of Time (AOT) compiler for Ruby. AOT here is in contrast to Just In Time compilers, ie. instead of compiling at runtime, an AOT compiler run generates an executable out of the source code:

The expression is compiled into LLVM IR, then bitcode, then assembly, then machine code. True compilation :-)

There are many scenarios where this is useful:

It will be useful for 1) code obfuscation 2) use Ruby on environments where dynamic code generation is not allowed


Finally, profilers are a way to figure out bottlenecks in applications. Ryan Davis updated his zenprofile profiler, which uses event hooks in the Ruby runtime as efficient way to track method invocations. Zenprofile has been around for some time, but the updated version now relies on the event_hook gem, which factors out the native code necessary for setting up the hooks. By using event_hook, it's now possible to write pure Ruby event hooks instead of having to write native code to hook into the Ruby interpreter. Zenprofile makes use of that by offering a pure Ruby version of it's profiling logic, and a faster version which uses RubyInline and C for the native code.

A quick look at the zenprofile code shows that using event_hook is as easy as extending the EventHook class, overriding a few methods such as def self.process event, obj, method, klass to capture the events.

Zenprofile also offers the spy_on feature, which can be used to focus on the performance of individual methods. The feature can be configured with Ruby code; eg. to focus on Integer#downto, here an example from misc/factorial.rb:

require 'spy_on'
Integer.spy_on :downto  

No comments

Watch Thread Reply

Educational Content

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

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.