ActiveRecord attribute-related improvements on Edge Rails

In: Ruby|Ruby on Rails

14 Aug 2007

I just noticed a very fresh changeset committed into the Rails trunk that’s pretty damn cool: changeset 7315. Michael Koziarski (aka nzkoz”) has refactored ActiveRecord’s attribute-related methods that, among other things:

  • caches typecasted attribute values in hashes. This avoids expensive typecasting (such as for Time-related attributes) which means access to these fields is significantly faster!
  • moves generation of attribute methods to the class, instead of the instance.
  • generates attribute reader and writer methods (before, only reader methods were generated and writer methods relied on method_missing magic).

The Ruby on Rails: Core Google Group thread on this patch has more info on the changes and some of the rationale behind them.

Here’s a simple (read: totally unscientific) benchmark I ran to access a DateTime attribute 10000 times (I ran it that many times to reduce the effect of the City.find call) with pre-revision 7315 edge Rails (I was using revision 7314):

>> Benchmark.bmbm do |x| 
?>   x.report('test') { c = City.find(:first); 10000.times { c.created_at } }
>> end
Rehearsal ----------------------------------------
test   1.500000   0.010000   1.510000 (  1.568782)
------------------------------- total: 1.510000sec

           user     system      total        real
test   1.500000   0.010000   1.510000 (  1.592264)

Post changeset 7315:

>> Benchmark.bmbm do |x| 
?>   x.report('test') { c = City.find(:first); 10000.times { c.created_at } }
>> end
Rehearsal ----------------------------------------
test   0.100000   0.010000   0.110000 (  0.129514)
------------------------------- total: 0.110000sec

           user     system      total        real
test   0.000000   0.000000   0.000000 (  0.009831)

Quite a bit faster!

1 Response to ActiveRecord attribute-related improvements on Edge Rails

Avatar

Morning Brew #62

August 15th, 2007 at 7pm

[…] ActiveRecord attribute-related improvements on Edge Rails […]