We have an application that displays thousands of currency values on a page, using the "number_to_currency" converter. When profiling the view rendering time, I found that a large amount of time was spent in the number helper classes. One level deeper, a bunch of time was spent calling I18n.translate to retrieve format options that were the same for the entire page.
I think it would be nice to optimize these helpers to allow subclasses of NumberConverter to be reused. I spent 10 minutes modifying the converters to allow them to be called more than once with the same options. After that, I did a quick test using bench.rb from the attached diff and saw roughly a 10x improvement in speed:
$ bundle exec ./bench.rb
Rehearsal ------------------------------------------------------
original delimited 3.110000 0.070000 3.180000 ( 3.262467)
reused delimited 0.270000 0.010000 0.280000 ( 0.284962)
original currency 13.140000 0.100000 13.240000 ( 13.395538)
reused currency 1.240000 0.020000 1.260000 ( 1.276842)
-------------------------------------------- total: 17.960000sec
user system total real
original delimited 2.820000 0.010000 2.830000 ( 2.846727)
reused delimited 0.250000 0.010000 0.260000 ( 0.265952)
original currency 12.870000 0.080000 12.950000 ( 13.020219)
reused currency 1.220000 0.020000 1.240000 ( 1.241132)
Are these changes something the Rails Core team is amenable to pursuing? Should I open a PR with my changes and some additional tests?
Thanks!
-md