开发者

Rails 3 slow partial rendering

First code:

# matches/index.html.haml
%ul.match_list
- @matches.each do |match|
  %li
    =render match
# matches/_m开发者_JAVA技巧atch.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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜