Rails' memoization is not always faster

September 29, 2008 at 10:57

Filed under: Computing — Pistos @ 10:57

I was chatting in #ruby-pro (on Freenode) with some folks, and the topic of memoization came up. raggi expressed his sentiment that

[the rails devs] over-memoize waaaay too much. some of these methods will be faster running than looking up from memoize. in the case of certain rails code, it seems to be applied to any method that is stable, regardless of complexity or input

apeiros commented that

it’s often overmedication too since a simple def foo; @foo ||= begin; ...longcalc...; end; end would suffice

I do believe that people should not employ memoization too loosely, since I think that if you have a performance problem, you should first try to address it directly rather than blindly treat the symptom simply by slapping a method with the Memoizable mixin.

I asked raggi for an example of the problem, some code that I could run some benchmarks on. He directed my attention to request.rb of the ActionController module. Strolling through the code, I saw a memoized method which did nothing but return a hash value. apeiros and I thought that it couldn’t possibly be faster to memoize code like that, so I set out to benchmark some code.

Benchmark results

  • Memoizing this method made it 14.5% slower.
  • Rails memoization was 57.8% slower than classic memoization (i.e. @cached_content_length ||= @env['CONTENT_LENGTH'].to_i). Alternatively phrased: classic memoization was more than twice as fast as Rails’.
  • The very act of mixing in Memoizable (without memoizing any methods) incurred an 8.3% speed penalty on method execution.

Here are the benchmarking code and the full benchmark results. The code makes use of better-benchmark, my library for statistically correct benchmarking.

So, employ Memoizable judiciously. Look over your method and consider whether memoization will really improve speed or not. It’s not guaranteed to do so in all cases.

Share

Managing web application errors with Hoptoad and Ramaze

September 22, 2008 at 23:42

Filed under: Computing — Pistos @ 23:42

I came across Hoptoad recently. Hoptoad is basically a webservice with an API that you can post error data to. It then stores the data and presents it to you in a nice web interface, and optionally sends you e-mail alerts in such a fashion that it doesn’t just unintelligently flood your inbox.

It was originally designed to be used with Rails apps, but I saw that there was also a Merb plugin. Not wanting my favourite web framework to be left out, I made a Hoptoad notifier for Ramaze with the help of manveru (the Ramaze lead dev).

Install via Rubygems:

gem install hoptoad-notifier-ramaze --source http://purepistos.net

And usage is dead easy; just two lines:

require 'hoptoad-notifier' Ramaze::Helper::HoptoadNotifier.trait[ :api_key ] = 'your-hoptoad-project-api-key'

I’m already using it with my own Ramaze applications. Enjoy! :)

Share

Thomas – For Thee

September 12, 2008 at 13:50

Filed under: Computing,Music — Pistos @ 13:50

So I was going through my usual whirl of daily Ruby news sites, and eventually my clickpath led me to a blog post by Dave Thomas wherein he unveils his first composition for piano. For those of you who don’t know, Dave Thomas is a well-known figure in the Ruby community, known for co-authoring Programming Ruby, one of the most popular books written about the Ruby programming language.

Well, reading the blog post piqued my curiosity because Ruby is my favourite programming language, and I’m also a pianist. So I examined the PDF of the piece, and thought that it didn’t look too difficult to play. I thought this might make a fitting next entry in my piano recording series.

So here I present my interpretation and rendition of Dave Thomas’ piece, For Thee. I took some liberties with tempo, as you can see by comparison with the recording linked in his blog post.

Share
Powered by WordPress.