Developers often neglect the core concepts of ruby when they start working on Rails projects.
" A good rails developer may not be a good ruby developer but a good ruby developer is always a good rails developer."
In my journey to be a good ruby developer, I am demonstrating a simple but powerful core ruby concept that can be used in rails or other ruby based projects.
You might have often seen this kind of code pattern in ruby on rails projects:
class User def format_name do_something_with name do_another_thing_with name end def name @name ||= begin User.first.name end end end
In above code name method is called multiple times and that's why we are using memoization.
Following is another way of memoization that is using some core concepts of ruby:
class User def format_name do_something_with name do_another_thing_with name end def name def self.name @name end @name = User.first.name end end
Following are the concepts behind the above code:
1. Whenever you are going to call
name method
for first time, It is going to create Singleton method - name
for declared object.2. Whenever you will call
Singleton method
for 2nd time, it is going to execute Singleton - name method
instead of main class name method
because Singleton method
have high preference over main class methods.3.
Singleton method
have access to all instance variables of an object.Above methodology can be used when code inside
begin
block is complex or large or may be in different situations to simply code.This way also memoizes `false` and `nil` values as well:
For example:
class Foo < Object def thing1 do_something with: expensive_computation1 do_something_else with: expensive_computation1 end def thing2 do_something with: expensive_computation2 do_something_else with: expensive_computation2 end private def expensive_computation1 @expensive_computation1||= Model.where(id: 1742).first end def expensive_computation2 def self.expensive_computation2 @expensive_computation2 end @expensive_computation2= Model.where(id: 4217).first end end
thing1
and thing2
both are memoized but thing2
also memoizes when the record is not found. thing1
will go hit the db again.Benchmark comparison of different memoization methods:
require "benchmark" class A def name @name ||= begin rand end end end class B def name return(@name) if defined?(@name) @name = rand end end class C def name def self.name @name end @name = rand end end class D def name class << self def name @name end end @name = rand end end n = 20_000 n1 = 2_000 Benchmark.bm(2) do |x| x.report("A:") { n.times { k = A.new; n1.times { k.name } } } x.report("B:") { n.times { k = B.new; n1.times { k.name } } } x.report("C:") { n.times { k = C.new; n1.times { k.name } } } x.report("D:") { n.times { k = D.new; n1.times { k.name } } } endOutput:
user system total real A: 3.810000 0.000000 3.810000 ( 3.817210) B: 4.000000 0.010000 4.010000 ( 4.007852) C: 2.850000 0.010000 2.860000 ( 2.848843) D: 2.850000 0.000000 2.850000 ( 2.854403)
Although there is not much difference but comparatively 'C' is fastest.
Also it's worth noting that initialisation of singleton method is slower than Boolean initialisation but calling singleton method is much faster and that make it usable.
I hope you would like this new way of memoization. Suggestions to improve the code and article are most welcome. :-)
Special Credit: My friend Brian Giaraffa , Kaushik and Philip Hallstrom :-)
Hi Aditya,
ReplyDeletegreat post! Thanks for sharing!
I was wondering whether you could explain the differences in the benchmark results. Why are the singleton method approaches C and D faster? Is evaluating the conditionals in A and B so much more expensive?
Hi Nicolas,
DeleteThanks for appreciating the post.
Following would be the execution order of A - B:
1. call instance method and assign value to instance variable.
2. Do Boolean comparison on every call of method.(Boolean comparison is slow).
Execution order of C - D:
1. Call instance method, initialise singleton method and assign instance variable to it.(This step would be slow as compared to initialisation of A-B).
2. In all subsequent call of instance method it's going to call singleton method instead of instance method because singleton method have high priority over instance method. It won't be required to go through the complete object to find instance method and that make it fast.
Thanks for the explanation! So it's really the Boolean comparison that does it. Very cool trick!
Delete
ReplyDeleteMy brother suggested I would possibly like this web site. He used to be entirely right. This post truly made my day. You can not believe just how so much time I had spent for this information! Thank you! outlook 365 email login
Thanks Oliver, your comment made my day :)
DeleteHi. This post was extremely inspiring, especially since then. The eligibility criteria for Turkey transit visa for US citizens are the same as for other visa types. If you want a visa to enter Turkey for tourism or any purpose, you can apply for a visa transit Turkey.
ReplyDeleteNice post. Thank you for this work. The travelers need to apply kenya e-visa through online visa application. Check the details and read the guidelines before you fill up the application form.
ReplyDeleteThank you . Your articles are very informative . Keep writing. Thankyou for this important information. Ukraine tourist visa express application system is a modern way of applying for a visa which takes just 5 minutes to apply and within 30 minutes you can get your e visa on you Email.
ReplyDeleteNice post. keep up the good work....India tourist visa from USA, Yes US citizens easily can apply for India tourist visa. Apply for the Indian e-Tourist Visa through an online visa application system.
ReplyDeleteI am enjoying every bit of it. It is a good site, as well as the stock is well-chosen. Thank you.emergency visa to India, You can fill your emergency Indian visa application form and get your visa within 1 to 3 business days.
ReplyDeleteHello! Can you suggest any other blogs/websites/forums that go over the same topics? Thank you! What is e visa Indian? India e-Visa is an electronic authorization to travel to India for business, tourism, or medical visits. Apply for an e visa to India through online eta Indian e visa official website.
ReplyDelete