Rails Partial (Rails 3, HAML) 任意慢
我使用的是 Rails 3.0.1、HAML 0.3.22 和 Mongrel 1.1.5(MongoMapper 不是 AR)。我正在渲染一个列表,每个列表项都是它自己的部分。每次页面渲染其中一个列表项部分时,渲染时间都会延长近 100 倍(而且每次都是任意不同的。而且,不用说,每个项目都具有基本相同的数据)。
知道这是怎么回事吗?我应该将逻辑移至一个块而不是部分吗?
Rendered shared/_head.html.haml (5.6ms)
Rendered tasks/_incomplete_task.haml (6.2ms)
Rendered tasks/_incomplete_task.haml (6.4ms)
Rendered tasks/_incomplete_task.haml (6.9ms)
Rendered tasks/_incomplete_task.haml (6.2ms)
Rendered tasks/_incomplete_task.haml (6.0ms)
Rendered tasks/_incomplete_task.haml (6.1ms)
Rendered tasks/_incomplete_task.haml (6.4ms)
Rendered tasks/_incomplete_task.haml (6.2ms)
Rendered tasks/_incomplete_task.haml (7.0ms)
Rendered tasks/_incomplete_task.haml (531.6ms)
Rendered tasks/_incomplete_task.haml (8.0ms)
Rendered tasks/_incomplete_task.haml (6.8ms)
Rendered tasks/_incomplete_task.haml (6.5ms)
Rendered shared/_tasks.html.haml (633.0ms)
I'm using Rails 3.0.1, HAML 0.3.22, and Mongrel 1.1.5 (and MongoMapper not AR). I'm rendering a list, and each list item is it's own partial. Every time the page renders one of the list item partials takes almost 100X longer to render (and it is an arbitrarily different one each time. Also, needless to say, each item has essentially the same data).
Any idea what is going on here? Should I move the logic into a block rather than a partial?
Rendered shared/_head.html.haml (5.6ms)
Rendered tasks/_incomplete_task.haml (6.2ms)
Rendered tasks/_incomplete_task.haml (6.4ms)
Rendered tasks/_incomplete_task.haml (6.9ms)
Rendered tasks/_incomplete_task.haml (6.2ms)
Rendered tasks/_incomplete_task.haml (6.0ms)
Rendered tasks/_incomplete_task.haml (6.1ms)
Rendered tasks/_incomplete_task.haml (6.4ms)
Rendered tasks/_incomplete_task.haml (6.2ms)
Rendered tasks/_incomplete_task.haml (7.0ms)
Rendered tasks/_incomplete_task.haml (531.6ms)
Rendered tasks/_incomplete_task.haml (8.0ms)
Rendered tasks/_incomplete_task.haml (6.8ms)
Rendered tasks/_incomplete_task.haml (6.5ms)
Rendered shared/_tasks.html.haml (633.0ms)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每个部分 6 毫秒似乎太高,但它实际上取决于您在部分中做什么(那里有任何 SQL 查询吗?)
至于 500 毫秒,我注意到我的应用程序中有相同的行为。我投入了一些时间来深入挖掘。好吧(就我而言)它确实是 Ruby 垃圾收集器。
当您可以微调垃圾收集器时,您可以使用 REE(例如此处 http://www.coffeepowered.net/2009/06/13/fine-tuning-your-garbage-collector/)。
您可以使用这些数字,看看您的应用程序的行为是否会改变。如果是这样,那就责怪GC。
6 msec per partial seems too high but it really depends on what are you doing inside the partial (any SQL queries there?)
As for 500 msec thing I've noticed same behavior in my app. And I've invested some time to dig inside. And well (in my case) it really was Ruby garbage collector.
You can use REE when you can fine tune the garbage collector (for example here http://www.coffeepowered.net/2009/06/13/fine-tuning-your-garbage-collector/).
You can play with those numbers and see if the behavior of your application will change. And if so then blame the GC.
如果将其放入一个单独的部分中,也许会更快?
我的意思是把它放入
部分,称为 1 次..而不是 1000 次...
Maybe its faster if you put it into one single partial?
I mean to put it into a
partial, which is called 1 time.. and not 1000 times...
放入
.bashrc
:源代码,然后重新启动服务器
根据您的问题,这可能会使您的部分渲染速度更快(在我的情况下,平均渲染时间加快了 3 倍)。
Put in
.bashrc
:Source, and restart server
Depending on what is your problem, this might make rendering your partials faster (in my case, average rendering time got x3 faster).