render :partial をやめて高速化

Erubisは高速さがウリのeRuby処理系らしい。

るびまを参考に導入してみた。
Erubis の Preprocessing 機能を使って Ruby on Rails の View 層を高速化する

ここにあったpartial展開ヘルパを少し書き換えて以下のようなヘルパを使っている。

# Erubis
# render :partial のかわりに使うと幸せになれます。
#
# render :partial => 'name'
# [%= partial_template_content_with_preprocessing => 'name' %]
#
# render :partial => 'application/name'
# [%= partial_template_content_with_preprocessing => 'application/name' %]
#
# ※デバッグが難しくなるので、後から差し替えたほうがいいかも。
module ErubisHelper
  def partial_template_filepath(path)
    if path.include?('/')
      dirname = File.dirname(path)
      basename = File.basename(path)
      file_path = "#{RAILS_ROOT}/app/views/#{dirname}/_#{basename}.rhtml"
    else
      file_path = "#{RAILS_ROOT}/app/views/#{controller.class.to_s.underscore.sub(/_controller$/, '')}/_#{path}.rhtml"
    end
    return file_path
  end
  
  def partial_template_content(path)
    return File.read(partial_template_filepath(path))
  end
  
  def partial_template_content_with_preprocessing(path)
    klass = Erubis::Helpers::RailsHelper::PreprocessingEruby
    eruby = klass.new(partial_template_content(path))
    return eruby.evaluate(self)
  end
end

render :partial がネストしてたり多用してたりすると、数割の高速化が確認できた。
(Erubis使用、展開使わず。)
これは良さそう。