Wednesday, June 25, 2008

Living on the Edge (or what's new in Edge Rails) #2 - Performance improvements

Posted by Chu Yeow

The first Living on the Edge covered some of the API changes since Rails 2.1, and this time round, I’m going to cover the performance improvements as promised.

Jumping right in…

Faster Erb templates

Jeremy Kemper has made the Erb processing more efficient, especially the concat and capture helper methods.

The “special” Erb _erbout has been replaced with an instance variable, which allows for:

  • better (memory) performance because bindings are no longer being passed around,
  • fewer evals which are usually expensive,
  • there’s no need to slice the _erbout variable when you can swap in a new (string) buffer, and
  • the buffer is actually available via a output_buffer reader and writer methods (so you can override it if you want).

Relevant changesets: 933697a
- 0bdb7d3
- 4d4c8e2

Faster partials and JavaScript helpers

Partial template initialization and JavaScript helpers have been refactored and optimized for speed and efficiency by Jeremy Kemper. These are but a few of the optimizations Jeremy has been committing recently. Be sure to check out some of the commits to Rails (or for that matter, any quality Open Source project) – you could learn something!

Relevant changesets: partialsJavaScript helpers

RecordIdentifier methods speedup

The RecordIdentifier has been sped up by some simple use of memo-ization, thus reducing the number of inflections performed, among other things. The RecordIdentifier is used widely in cache keys, partial template paths, and in most places where you identify an ActiveRecord model without caring about its actual id.

Relevant changesets by Jeremy Kemper: c1a9820566d717

Lazy load cache and session stores

Update: Oops my bad, this change was later reverted in 6573f6a.

The various cache stores in the ActiveSupport::Cache module are now lazy loaded – this means that they are only required when you actually start using them.

Changeset by Pratik Naik