5

My Rails 3.0 app on ruby 1.8.7 contains Haml 3.1.3. Most of the views are haml templates, it takes about 0.5-5ms to render them on my machine in production mode.

Having said that, a few partials take much longer. 300ms to 900ms for 30-60 lines of haml. It must be something in the way how I use it, but how could I debug what's wrong? The benchmarks are consistent and reproducible.

I'm not sure about possible sources of the error:

  • deep partial nesting? (3-5 levels)
  • deep haml nesting? (4-8 levels)
  • use of block helpers?
  • lots of translations?
  • using haml with formtastic 2.0?
  • using form builders for nested forms?

Any help is appreciated.

datenimperator
  • 308
  • 2
  • 9

4 Answers4

1

It turned out to be a number of things inside Formtastic 2.0:

  • Lots of object lookups, uncached
  • Lots of translations, uncached
  • Use of try…rescue blocks which slowed it down tremendously

Fixes have been added to Formtastic 2.1 and up, making it a lot faster. Kudos to Sascha Konietzke for providing patches.

Community
  • 1
  • 1
datenimperator
  • 308
  • 2
  • 9
1

A more recent way to fix this is Hamlit, a high-performance Haml implementation with some limitations by design for performance. It claims 8.24x speedup.

Also, not Haml-specific, but look at multi-fetch gem for lists.

mahemoff
  • 44,526
  • 36
  • 160
  • 222
  • Thanks for mentioning hamlit, which indeed is faster. In this particular case uncached translations have been causing this, but adding a little extra speed is nice nonetheless. – datenimperator Aug 12 '16 at 14:56
  • Interesting, I haven't heard of that before. Any info on how to make sure translations are being cached correctly? (I'm always up for anything to improve Rails rendering perf!) – mahemoff Aug 13 '16 at 18:17
0

Most likely it's Garbage Collection. See these posts:

http://www.williambharding.com/blog/uncategorized/rails-3-performance-abysmal-to-good-to-great/comment-page-1/

http://bibwild.wordpress.com/2011/06/28/rails3-unbearably-slow-view-rendering-use-ree-with-gc-tuning/

If you're using REE 1.8.7 (which also fixes a memory leak, so you should be) then you can tune your GC settings. I just did this last week and our response time dropped by 50%.

Raphael
  • 1,701
  • 15
  • 26
0

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

Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236