Rails 3 部分渲染缓慢
第一个代码:
# 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在开发模式下运行?我不知道 Rails 的内部工作原理,但我确实知道它在生产中缓存视图代码,而在开发中重新读取文件。如果它在请求期间缓存视图,那就太好了,但可能不会,这可能就是问题所在。
另外,不要循环匹配,而是尝试一下以下操作:
它肯定更简洁,并且,如果问题是重新读取文件,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:
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.