ActiveRecord attribute-related improvements on Edge Rails
August 14, 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_missingmagic).
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!
Trying to code your own site? If you need some help, you can find custom PHP/Perl script writing services to help you. Whether you're looking for Perl scripts to use on your site or want someone to look at your PHP scripts, just look online for an abundance of resources.





[…] ActiveRecord attribute-related improvements on Edge Rails […]
Posted by: Morning Brew #62 on August 15, 2007 7pm