Rails 3 部分渲染缓慢

发布于 2024-10-26 18:28:57 字数 492 浏览 1 评论 0原文

第一个代码:

# 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时速度这么快?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总以为 2024-11-02 18:28:57

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

另外,不要循环匹配,而是尝试一下以下操作:

= render @matches

它肯定更简洁,并且,如果问题是重新读取文件,Rails 可能会优化该过程(如果它知道您将要执行的操作)循环播放。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文