Simple Rails Performance Tip – Active Record Batch Retrieval

Users.each do |user|
  puts user.name
end

Ever wondered about how expensive a simple “.each” on an ActiveRecord object collection is. This essentially means for every iteration of the loop, there is going to be a database fetch. Imagine looping over a collection that has 10,000 records (forget millions..). Yes, I have made use of this very same thing a bunch of times without realising the kind of performance implications this could have. Not until last week I discovered that there was a very less-expensive way to do this.

I was perusing the Rails guides casually looking for something else when I hit upon this. “.find_each”. What does this do? It makes looping over a collection of Active record objects much much cheaper by performing a batch retreival of the database records and caching them. The batch size can be altered by passing in a batch_size param like this “.find_each(:batch_size => 500”).

Users.find_each(:batch_size => 200) do |user|
  puts user.name
end

I have almost stopped using “.each” these days and let “.find_each” do the job.

iPhone 4

I just got the new iPhone 4. Do I like it? Of course, Anybody who liked the older iPhones is bound to like this one as well. I still want to post my honest opinion on what I think about this phone.

+
Retina display is awesome!
The speed is great, a quantum leap over the iPhone 3G.
Multitasking is cool on the iPhone 4 (it is an iOS 4 feature, I know).
FaceTime should rock – haven’t tried it yet.


Battery life is not very different from the old iPhones.
Not sure how I figure out if my phone has got the dreaded antenna problem but have had a bunch of dropped calls till now.

Bottom-line: I think it is an awesome smart device, but a phone? I would beg to differ. I think Apple still needs to optimize the *phone* in the iPhone. Hope iPhone 6 nails it!

Devise for Rails

Happened to try out Devise in my new rails project. Must say it is a great plugin that has impressed me for the following reasons :

  • A piece of cake to implement. The documentation and help-me references are truly helpful and answer a number of questions that you might have about the plugin.
  • Apart from being a flexible authentication solution, It has got a bunch of useful modules for things like setting up a user registration/confirmation feature for your web app,  forgot/reset password feature etc. The best part is you choose what you want to use.
  • The plugin also provides a number of hooks to let you override certain aspects of the implementation. So, it is extensible as well.
  • The necessary custom routes for the different devise controllers are generated seamlessly by using a devise_for :resource in your routes.  There are other interesting things that you can do here as well.

I worked on a user welcome email feature for the project which I think could be another module that could add value to Devise. Hope to contribute sometime soon.

Jack Baur

Finished watching the first season of 24 on Netflix instant play. Seriously awesome political thriller! Completely fell in love with it. I have heard people rave about it and understood why.

Liked Kiefer Sutherland as the indomitable and revered “Jack Baur”. The concept of 24 episodes each depicting the events for the hour in real-time is a cool idea.

Just getting started with “Season 2”.

jQuery’s Clone() and FireQuery

So there was this need for providing ajax-style add/remove item-rows on the last project I worked on.  What I thought might be pretty straight-forward to achieve using jQuery-fu eventually turned out to be little hairy for me.

The approach that I used to implement this was to use a hidden item(a div with some content in this case) and clone it using the jQuery clone() method to make a new copy of the item and then append it to the collection under a parent div.  The cloning and adding worked like a charm. What bothered me was the remove of the item-row. Each item-row had a remove link/button to delete the item. The remove was supposed to work using the jQuery click event handler on the remove link/button. It did’nt work as expected :-(.  The click event handler was’nt getting hit for some reason and I did not have clue as to why as the same remove handler was working without issues in a different section on the same page.

What I came to my rescue was FireQuery.

FireQuery is a cool little add-on for Firefox that integrates with Firebug. It allows inspection of jQuery expressions, methods, events in the firebug console making it a great debugging assistant for jQuery. Using FireQuery, I was able to confirm that the cloned item-rows did not have the click remove handler attached to them. This made me realise that the clone() was not copying the jQuery event handler after all.  Thanks to Google, I finally figured out clone(true) would fix the problem and it did! Apparently, clone(true) is like a deep copy of the DOM element/object along with its events.

I m a jQuery newbie but I have already started loving it over Prototype and other JS libraries I have used in the past. Hoping to make more use of it (and FireQuery too!) in the future.

JQuery Clone and Append

Grails – Watch out!

My experience with Grails on one of my recent projects has been anywhere from frustrating to annoying!

I can’t deny the fact that Grails is a pretty powerful convention driven framework as advertised by it’s founders. It has done well to bring most of railsy philosophies and techniques into itself. It does help in accelerating your software development cycle and being AGILE to a certain extent. The programming language Groovy is pretty sound I think.

The shortcomings of the framework like lack of an enthusiatic developer community supporting it, lack of a solid testing framework(the one that it currently provides is so undocumented and feature-less), unhelpul stack traces and logging etc hit you really hard sometimes.

I d prefer Rails over Grails anytime! What do you think?

Opera Mini for the iPhone!

So Opera Mini for the iPhonegot approved and is available in the App store since last night.  Idownloaded it this morning and should say I am mighty impressed withit. It is zippy, fast and intuitive.  There was a quite a bit of speculation around whether this would get approved by Apple.  I m happy that Apple approved it.
I did notice a few quirks with the Browser but they are in no waydeal-breakers. From my brief interaction with it, the browser looksawesome to me! I dont deny Safari being a solid mobile browser itselfbut with Opera out now, its “Bye-Bye Safari” for me 🙂

photo
Opera Mini for the iPhone

Vinnaithaandi Varuvaaya

Inspite of the thoroughly enjoyable “Omana penne” which I think is
Benny Dayal’s best for Rahman, the funky “Anbil Avan” which has
stunning orchestration and nice vocals and the rebellious “Aromaale”,
VTV definitely falls short of a Jodha Akbar or a Delhi 6. “Hosanna” is
decent but is more HJ-ish in feel and the way it is constucted. The
other numbers especially the Rahman number fail to create the usual Rahman impact.

The shortcomings?

1. I see this line of thought amongst many in the blog world about
Rahman pushing for a global sound and catering to an international
audience! This is precisely the problem. A Tamil album needs to target
the regional audience first and foremost. For instance, a Rhythm or a
Minsaara Kanavu could impress someone like my grandma which I don’t think is the case with the more recent Rahman albums!

2. The intricate layering and synth orchestration in Rahman’s music
(supposed to be his core strength) off-late seems to be overwhelming.
The tune is what defines a song and that’s being compromised in the
quest for layering.

3. Composers like Yuvan, Harris and others have mastered Rahman’s yesteryear style and they are closing in on him. Not to say that they are any where versatile like Rahman, but they are belting out pretty enjoyable numbers (Vaaranam Aayiram, Goa etc).

Wish Rahman delivers a stunner in Mani’s Raavan or Shankar’s Endhiran.

P.S Congrats on the grammys, Rahman!.