Dr Nic

[BTS] Magic Models vs ActiveRecords – Efficiency

Dr Nic’s Magic Models are magical, that goes without saying. But are they efficient at being magical? Do you get the same speed in your application as if you explicitly defined your classes and your associations will your application execute faster?

Yes. The cost of accessing a Magic Model and a normal ActiveRecord is exactly the same – once the Magic Model has been initially generated. Its just a normal ActiveRecord like all your other ActiveRecords: they are loaded into memory when you first ask for them. Normal ActiveRecords are loaded into memory from your /app/models/.rb file; Magic Models are generated classes. The Magic Models would in fact be quicker to load as there is no file access required.

The cost of accessing Automatic Associations versus normal Active Record associations is also exactly the same. To understand the inner workings of associations, see err.the_blog’s latest article.

The point to take away from the discussion is that associations are a generated method on your class. Once you’ve generated it once (at the time you call has_many for example), you have it permanently. So there is only a small, one-time cost for the Automatic Associations – to see which association to generate for the incoming method call. But once you’ve generated the association, its yours permanently.

Related posts:

  1. MagicCGI shows OpenID user count In the last 20 days, 43 people have used...
  2. map_by_method now works with ActiveRecord associations I was always annoyed that map_by_method was broken for ActiveRecord...
  3. Magic Wiggly Lines => GuessMethod, by Chris Shea If you ever make time to code just for pleasure,...
  4. Dr Nic’s Magic Show at RejectConf2007 Update: there is a patch available for edge rails to...
  5. An Interview with Dr Nic Do you want answers to questions like: What do you...

One Response to “[BTS] Magic Models vs ActiveRecords – Efficiency”

  1. buy paxil…

    eijohuk ih…