Mon 26 Nov 2007
Performance tuning in Rails: how to write fast Rails apps
Posted by Julian under Performance, New Ways of Thinking
No Comments
In my view, a good test of how solid a framework is, is what you have to do to get good performance out of it. Does it all go to hell? That is, does the code you wrote before have to be mangled beyond recognition to get things to go fast?
This article on by Stefan Kaes on Common Rails Performance Problems, and Stefan’s blog RailsExpress dedicated to Rails Performance make it clear that Rails is a bit of both:
- The core Rails framework definitely has some pieces that just seem to work pretty slowly and should be avoided on critical pages: URL mapping (’routes’) is one:
“In general, all helpers that take a URL hash will invoke the routing module to generate the shortest URL referencing the underlying controller action. This implies that several routes in the route file need to be examined, which is a costly process, most of the time”
- ActiveRecord is slow in a number of circumstances:
“Retrieving a large number of ActiveRecord objects from the database is relatively slow. Not so much because the actual wire transfer is slow, but the construction of the Ruby objects inside Rails is rather expensive, due to representing row data as hashes”
- Another one of use: the MySQL adapter for Rails has a performance bug in it which makes it degrade significantly when accessing 1000s of rows. Full discussion here on how to make sure your Rails MySQL performance is optimal.
These aren’t surprising in a framework that’s only been out a couple of years now. What I’m particularly interested in low cost tunings that won’t be made redundant by the next rev of Rails. Stay tuned.
How to find the problems: performance tools
Another aspect to consider is tools for Rails performance assessment: Railsbench for performance regression testing, and a commercial Rails performance analysis tool for $US199 which Stefan also recommends.
Whatever happens, memcached is very likely to be part of scalable performant Rails apps for quite some time.