« Make Your Ruby/Rails App Fast: Performance And Memory Profiling Using ruby-prof and KCachegrind | Main | Pluron Rails Optimization Performance @ UA Web »

February 13, 2008

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451c8d369e200e55039acc58833

Listed below are links to weblogs that reference Make Rails Associations Faster by Optimizing Named Blocks and String Callbacks:

Comments

Valdas

I think there is a typo in 1.2 patch for String callbacks for belongs_to_counter_cache_after_destroy_for context: it deals with before_destroy and turns into after_destroy.

Alexander Dymo

Fixed. Thanks Valdas!

Greg

Dude -- you rock. The whole "memory is the problem, not CPU" light bulb is really a breakthrough for speeding stuff up, it's disappointing it's taken so long for someone to come along and realize this, but glad you have!

Lucas Carlson

You have a problem in your first patch... in the associations/has_many_through_association.rb file you forgot to modify the call to super.

Alexander Dymo

Yep, I've missed that in 1.2 patch (2.0 one should be fine). I've uploaded updated 1.2 patch. Thanks, Lucas.

Kwi

Your work is awsome guy ;)
Continue like this !
Thanks a lots

Michael Klishin

You guys are kind of people Rails community waited for several years. Thanks a lot.

Oleg Andreev

I wonder how slow ERB is. It uses bindings for each method like form_for or capture. I don't have any idea how to optimize it. Maybe, markaby is much faster due to it's simplicity. I gotta do some benchmarks.

Paul Kmiec

Thanks for all the info. I am now up and running with all the tools.

Oleg, check out erubis at http://www.kuwata-lab.com/erubis. It is a faster implementation of ERB.

After applying the no_block_args_in_associations patch to rails 1.2.3, I got an "ArgumentError: comparison of Ownership with Ownership failed" which was caused by,

owner.ownerships.sort

where owner has many ownerships. Adding to_a between ownerships and sort fixed the problem. Did anyone else run into this?

Alexander Dymo

I think I know what's wrong. Patched implementations of method_missing in associations add extra block for called function even when you initially didn't pass the block.

For example, call to
owner.ownerships.sort
will be translated by method missing with (very roughly)
owner.ownerships.target.send(:sort) { |*block_args| yield(*block_args) if block_given? }

This is wrong because sort uses the block to actually compare objects and our automatically added block obviously couldn't do that. Hence the "comparison failed" message.

I'll need to fix the patch to not pass blocks when there's no block to the original function call. Fix is coming soon...

Ok, new "no named block arguments" patch for Rails trunk is ready:
http://dev.rubyonrails.org/ticket/11109

Patch for 1.2 is coming soon...

Paul Kmiec

I made the changes you described to Rails 1.2. Everything seems to work. Thanks!

Dave Myron

Alexander, have you considered making these into a plugin for Rails 1.2 and 2.0? It would be much easier to just update a plugin every time you make a blog post regarding Rails performance than to hack the patch into a monkeypatch.

roger matching ties

Thanks for looking into this stuff. I just now realized how expensive named parameters are:

>> Benchmark.measure {10000000.times{go {}}}.real
=> 3.4910409450531

Calling a function with a named block:
>> def go 3; end
>> Benchmark.measure {10000000.times{go {}}}.real
=> 25.6680719852448

Calling a function with a named block which named block is then .call’ed
=> 37.7056579589844

versus yielding:
>> Benchmark.measure {10000000.times{yields_once{}}}.real
=> 5.47482490539551

Also with so much memory saved it will save on the garbage collecting. Rock on.

-R

rogerdpack

Now if we can just figure out how to get AR creation time low we'll be set. Maybe hacking the mysql adapter to create the objects for us? :)
-R

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Buzzletter

  • Hear about our new Ruby on Rails performance improvements, hacks, recipes, plugins & more. Enter your email below