Lazy loading ...........

I have learnt lazy loading, and implemented it, in my office project, but non-technically speaking, I have learnt it long ago, in my first semester of engineering. One of my best friend Shrikant taught me that.

In college days we use to make many unrealistic plans, and after making them we use to vote, who is in and who is out? If someone says no, then we used to curse him, that because of you we are canceling our plan. But Shrikant never use to say no, doesn’t matter how unrealistic you make it. Because somewhere in deep, he was knowing, that plan is not going to work. 

At the end when we cancel the plan, he use to pretend that, he was ready but as plan got canceled, so no use of talking about it.

You know what, lazy loading works exactly in same way. Out of many functionality (which your application offers), client use only 20% of them all the time. Yes I am talking about 20-80 rule.

As past experience tells us that, not all functionalities are in use very actively and some of them are not in use at all, so why not to implement lazy loading, to implement those less use functionality.

If I don’t compute or execute the code which is not frequently used, how it will help to improve performance?
  1. First you will not load data into memory, so efficiency will increase.
  2. If client don’t ask for it, you don’t have to execute it at all.

So you have to calmly wait for it, if at all you get request, load and execute it, otherwise, you don’t even have to load it in memory, simple :). Moral of the story is, do it when you have to do.

Example –



/*
This example is taken from "more effective C++ by scott meyers"
*/
// Lazy expression evaluation.
// Think you have a matrix class.
template<class T>
class Matrix
{
    //......
};
// you declare two huge matrix object
Matrix <int>  m1(1000, 1000);
Matrix <int>  m2(1000, 1000);
// now you are caching m1 + m2 value in m3
Matrix <int> m3 = m1 + m2;

/*
Think, why we need sum of m1 and m2? 
    1. To show its sum value in output console?
    2. And use summed value in some calculation? 
But why we are doing it here? be lazy ..
*/

// so if we want m3[4][4] to be shown in output console then use -
cout << m3[4][4];

// or be lazy (and don't create m3 at all) just use expression like mentioned below -
cout << m1[4][4] + m2[4][4];

/*
there is no reason we compute all values if we only looking for m1[4][4] + m2[4][4].
why to create all new variable m3 and calculate all other sums? it is just west of time.

if we really want to have sum of m1 + m2 , and do some operation on top of that and we want 
old values (m1 and m2) to be there, and some kind of business requirement where we have to 
create m3 to store m1 + m2 (and them do some typical operation on summed value), then create it, 
All the design will depend upon the requirement. but don't do it just like that all operations has_denorm
some cost associate with it. 
*/


Thanks for reading it. To read more on design patterns and basic design principles please see my web page. You can also join me on FB or on G++. Please drop comments for any question related to this blog.

Comments

Popular posts from this blog

Non-virtual interface idiom (NVI)

Architectural patterns => Mud to structure => layers.

Architectural style -> Adoptable system -> Reflection.