Faster eager loading and funky joins 0

Posted by pratik
on Tuesday, October 30

I was able to spend some time on a flight and at home to work on a very annoying performance pit associated with eager loading association’s instantiation code. So, with changeset 8051, hopefully you should see some performance improvement with eagerloading associations with large data sets ( even 100 rows should be good enough to notice the difference – remember, rails does a big fat assed cartesian join when you eager load multiple associations ) – at the cost of a little extra bit of memory. You can catch the mailing list discussion here.

On a very related side note, please take some time to check changeset 8054 as well. As the changeset contains really well written documentation, I wouldn’t reinvent the wheel here. This would make life a lot easier for those of you who are shit scared of sql and use eager loading in some of the worst ways possible because of that.

In any cases, I’d really encourage you to benchmark your code before choosing any solution. If you have never done it before, and you got scared by looking at the generated output, it’s time to try it again. I’d suggest you start with ruby-prof HTML call graphs which are explained very well here

The sample performance script can look as simple as :

1
2
3
4
5
6
7
8
require 'ruby-prof'
puts "Sanity check..."
puts Person.find(:all, :include => :items).inspect
results = RubyProf.profile { Person.find(:all, :include => :items) }
File.open "#{RAILS_ROOT}/tmp/profile-graph.html", 'w' do |file|
  RubyProf::GraphHtmlPrinter.new(results).print(file)
  `open #{file.path}`
end

And just run the script with script/runner of your rails application. For changeset 8051, you can see my before and after graphs to get a basic idea.

Fixtures go foxy 3

Posted by pratik
on Friday, October 26

With Changeset 8036

Following

1
2
3
4
5
6
7
8
9
10
11
# clients.yml
monkeys: 
  id: 1 
  name: 3 Monkeys Ltd.

# employees.yml 

lifo: 
  id: 1 
  name: Pratik
  client_id: 1

Now can be re-written as

1
2
3
4
5
6
7
8
9
# clients.yml
monkeys: 
  name: 3 Monkeys Ltd.

# employees.yml 

lifo: 
  name: Pratik
  client: monkeys 

There are some more goodies there as well. Please check out documentation and code for more details.

P.S -> Now there is a built-in performance and benchmarking support as well. Just do rake rails:update:scripts to get them in your app running bleeding edge.

Sweet render shortcut

Posted by pratik
on Monday, July 23

I didn’t really know about this shortcut until I came across this ticket

In edge rails, instead of :


<%= render :partial => 'task', :collection => @tasks %>

You can do :


<%= render :partial => @tasks %>