Background

PaperClip is a useful file attachment library for ActiveRecord that allows you to attach files (in our case photos) to an ActiveRecord object. It abstracts a way a good deal of complexity and tedious code and is particularly great for uploading files to Amazon S3.

Problem

Like many Ruby on Rails apps, we have a method for searching this particular set of ActiveRecords and returning a set of results that include URLs for the attached photos. As part of constructing the result set we were calling the url method on the photo attachment. For some reason, calling this url method about three times per record was taking forever (like say upwards of 20 ms per record). Returning just 25 records or so was really introducing a lot of delay.

Solution

In our case, the urls for the photos don't change after they are set and can be easily computed in constant time. So to really speed things up, we stopped using the url method on the PaperClip attachment and simply built the url string manually. As you can imagine the speed up has been awesome as the time to produce these new URL strings is almost nothing. On the above mentioned search, we shaved off about half a second in the controller, which makes the search feel more "instant".