Skip to content

Optimizations: String handling #45

Closed
grimen opened this Issue Sep 6, 2009 · 7 comments

2 participants

@grimen
grimen commented Sep 6, 2009

I noticed two trivial and failsafe ways of optimizing Formtastic, and there are plenty of places in the current code where this applies right now:

  • Concat strings with << (append) instead of +/+= (concat)...or even better (but sometimes less readable) using interpolations. Reasons: Signficiant speed improvements, and no new unnessacy String instances.
  • Use immutable Strings, i.e. Symbol (to_sym) whenever possible (especially for: send, I18.t, etc.)

Formtastic doing a lot of String-manipulation so... Just putting this as a note on the wall - haven't done any benchmarks on Formtastic yet but might do soon for the fun of it. 8) 1.0?

Motivation background:
http://blog.purepistos.net/index.php/2008/07/14/benchmarking-ruby-string-interpolation-concatenation-and-appending/

@justinfrench
Owner

We should definitely get this in for 1.0 -- optimizations and code clean-up are top of my list for the 0.9–1.0 gap.

@grimen
grimen commented Oct 22, 2009

Another possible optimization:

  • Replace gsub() with tr() whenever possible. I know I bench-marked these, and tr() was way faster in those benchmarks. Formtastic use gsub() quite a lot.

Here are some recent benchmarks showing tr() is ~2 times faster. Peep the other benchmarks, might be useful as well.
http://dominikh.fork-bomb.de/2009/10/12/ruby-benchmarks/

@grimen
grimen commented Oct 22, 2009

Actually when looking more into these, in some cases 10 times faster (!) =o

@justinfrench
Owner

Sure, PDI :)

@grimen
grimen commented Nov 22, 2009

Quite obvious changes, and belongs to 1.0 for sure.

@grimen
grimen commented Nov 22, 2009

Using interned strings whenever possible and where it makes sense (String => Symbol), and concat (<<) instead of appending (+). Closed by e1a4a41.

@zuf zuf pushed a commit to zuf/formtastic that referenced this issue Sep 7, 2012
@grimen grimen Using interned strings whenever possible and where it makes sense (St…
…ring => Symbol), and concat (<<) instead of appending (+). Closes #45.
e1a4a41
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.