【问题标题】:Rails 3 slow partial renderingRails 3 缓慢的部分渲染
【发布时间】:2011-07-23 11:24:56
【问题描述】:

第一个代码:

# 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)

慢得要命。

第二个代码:

# 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)

好多了。

为什么不使用 partial 会快很多?

【问题讨论】:

  • 抱歉,我第一次发表评论时看不到结果。
  • 开发还是生产模式?
  • 它的开发模式,在生产中尝试一下
  • 在生产模式下运行是否解决了这个问题?

标签: ruby-on-rails ruby-on-rails-3 views partials


【解决方案1】:

您是否在开发模式下运行?我不知道 Rails 的内部工作原理,但我知道它在生产中缓存视图代码,而在开发中重新读取文件。如果它在请求期间缓存视图会很好,但它可能不会,这可能是问题所在。

另外,不要循环遍历匹配项,而是尝试旋转一下:

= render @matches

它肯定更简洁,而且,如果问题在于重新读取文件,如果 Rails 知道您将要循环,它可能会优化该过程。

【讨论】:

  • render(:partial => 'match', :collection => @matches) 是更正式的指定方式,并且比独立运行一堆render 调用更快。由于您提到的原因,生产模式也比开发模式快得多。
猜你喜欢
  • 1970-01-01
  • 2012-10-16
  • 2023-03-11
  • 1970-01-01
  • 2011-03-22
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多