methyl
methyl

Reputation: 3312

Rails 3 slow partial rendering

First code:

# matches/index.html.haml
%ul.match_list
- @matches.each do |match|
  %li
    =render match
# matches/_match.html.haml
%li
  =match.id
Completed 200 OK in 665ms (Views: 496.3ms | ActiveRecord: 142.1ms)

Slow as hell.

Second code:

# matches/index.html.haml
%ul.match_list
- @matches.each do |match|
  %li
    =match.id
Completed 200 OK in 196ms (Views: 30.0ms | ActiveRecord: 134.6ms)

Much better.

Why is it so much faster when not using partial?

Upvotes: 3

Views: 4203

Answers (1)

Matchu
Matchu

Reputation: 85792

Are you running in development mode? I don't know the inner workings of Rails, but I do know that it caches view code in production, whereas it re-reads the file in development. It'd be nice if it cached the view for the duration of the request, but it may not, and that may be the issue.

Also, instead of looping over the matches, try giving the following a whirl:

= render @matches

It's definitely more concise, and, if the issue is re-reading the file, it's possible that Rails might optimize that process if it knows you're going to be looping.

Upvotes: 9

Related Questions