Rails Partial (Rails 3, HAML) arbitrarily slow

2019-07-19 18:43发布

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)

3条回答
2楼-- · 2019-07-19 18:59

Maybe its faster if you put it into one single partial?

I mean to put it into a

# tasks/_incomplete_tasks.haml instead of tasks/_incomplete_task.haml

partial, which is called 1 time.. and not 1000 times...

查看更多
Fickle 薄情
3楼-- · 2019-07-19 19:04

Put in .bashrc:

export RUBY_GC_HEAP_INIT_SLOTS=1000000
export RUBY_HEAP_SLOTS_INCREMENT=500000
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
export RUBY_GC_MALLOC_LIMIT=50000000

Source, and restart server

$> source ~/.bashrc
$> rails s

Depending on what is your problem, this might make rendering your partials faster (in my case, average rendering time got x3 faster).

查看更多
Anthone
4楼-- · 2019-07-19 19:08

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.

查看更多
登录 后发表回答