Home arrow Ruby-on-Rails arrow Common Performance Problems in Rails

Common Performance Problems in Rails

In this conclusion to a six-part article series on optimizing the performance of a Ruby on Rails program, you'll learn about the most common performance problems you might encounter in Rails, and how to avoid them. This article is excerpted from chapter 13 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
December 29, 2010
  1. · Common Performance Problems in Rails
  2. · Database Access

print this article

Common Performance Problems in Rails
(Page 1 of 2 )

Common Performance Problems in Rails

The performance of any web application can be severely crippled by small issues in the source code, and a Rails application is no different. Here, we’ll look at some of the most common performance problems in Rails applications and how to avoid them.

Rendering Speed

Rendering a page to a user should probably be the fastest and best optimized thing in a web application. Therefore, all really expensive calculations should be made at some other point. One example of this is textilizing content. In Chapter 3, we used the textilizemethod to transform the book blurb stored in Textile format to HTML. However, this formatting can just as well be done whenever the blurb is changed; that is, when the book is saved.

Let’s add a field for the blurb in HTML format to the database:

$ script/generate migration add_blurb_html_to_books

  exists  db/migrate
  create db/migrate/013_add_blurb_html_to_books.rb

Next, we open up the migration file we just created and add the new column to it:

class AddBlurbHtmlToBooks < ActiveRecord::Migration
  def self.up
add_column :books, :blurb_html, :text

  def self.down
remove_column :books, :blurb_html

Then run the migration:

$ rake db:migrate

Now we have a column in the database where we can store the textilized blurb. We will do the textilization in abefore_savefilter in theBook class. Add the following toapp/models/book.rb:

class Book < CachedModel
before_save :textilize_blurb


  [a lot of lines omitted]

  def tagged_with
    tags.collect{|tag| tag.name }.join(", ") if not tags.nil?


    def textilize_blurb
self.blurb ||= ""
textilized = RedCloth.new(self.blurb)
self.blurb_html = textilized.to_html


Now, whenever a book is saved, its blurb is also transformed to HTML.

We should find all thetextilizecalls in the application and replace them withblurb_html. For example, inapp/views/admin/book/show.rhtml, replace this line:

<dd><%= textilize @book.blurb %></dd>

with this one:

<dd><%= @book.blurb_html %></dd>

The result is one less expensive calculation to do in the page-rendering phase.

Caution  Althoughtextilizeis a cool quick-and-dirty helper, as a rule of thumb, it should never be used in a production setting.

Another thing that can slow down rendering is building the URLs in link_to and form helpers. If your profiling shows that a url_for call is taking a lot of time, you might consider replacing the helper with a handwritten HTML tag. So, for example <%= link_to "Home",
home_url %> would be replaced by <a href="/">Home</a>. However, remember to measure and profile. It serves no purpose to optimize these helpers unless you see that they are guilty of slowing down your application.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials