ruby-prof shoots down my helper block

written by mark on May 12th, 2008 @ 04:00 PM

Here is the head of my ruby-prof output. I'm surprised to see that one of my fairly simple helper methods (ApplicationHelper#highlight_my) is topping the charts of the expensive procedures list.


 18.67      0.27     0.27     0.00     0.00      237  Proc#binding
 17.61      0.25     0.25     0.00     0.00    10208  WIN32OLE#method_missing
 13.05      0.19     0.19     0.00     0.00       96  ApplicationHelper#highlight_my

All it does is slap a span tag around any task that belongs to the logged in user.


    <% highlight_my(task.assignee) do %>
	   ...
    <% end %>

The span tag has a css class that will highlight those tasks.


  def highlight_my(obj, &block)
    if obj != nil
      mine = obj.id == current_user.id
    end
    concat('<span class="mine">', block.binding) if mine
    block.call
    concat('</span>', block.binding) if mine
  end
			

It appears the block binding might be a bit much overhead for such a simple operation. Perhaps a better use for it would be to wrap something more significant like a table to add rounded corners. You may only need to do this a handful of times rather than invoking the method 96 times like I did here. I still really like the syntax so i my play around with some different kinds of partial caching schemes to see if i can keep this in my bag of tricks.

Post a comment