Lazyinstance, as its name implies, is to delay the creation of class instances.
So how does chrome do it?
For performance, resources, and objects that cannot be used immediately in the program, we do not need to create them immediately. For example, a menu object on the interface is created before you click the menu, it will definitely affect the Startup Performance of the software.
Another example is the database object, or thread..., which may lead to latency. One of the reasons for Chrome's efficiency is that it calls countless lazyinstance objects.
Those who have studied chrome should know that all the threads (except the main thread) of Chrome's thread model of management tasks can be loaded with delay.
What are the features of lazyinstance <type, traits> As a delayed loading template?
1. thread security.
2. Unlike Singleton, lazyinstance supports multiple instances of the same type.
3. Pre-allocate space for the lazyinstance type to reduce heap space fragments.
4. Beautiful Deletion
Usage:
static LazyInstance<MyClass> my_instance(base::LINKER_INITIALIZED); void SomeMethod() { my_instance.Get().SomeMethod(); // MyClass::SomeMethod() MyClass* ptr = my_instance.Pointer(); ptr->DoDoDo(); // MyClass::DoDoDo }
We can see from the above:
Lazyinstance <t>: Get () returns an object reference.
Lazyinstance <t>: pointer () returns the object pointer.
Next we will explain how the features described above are achieved.
1. Thread Security
For thread security, lazyinstance declares three tags:
Enum {
State_empty = 0, // no instance is created
State_creating = 1, // The instance is being created. At this time, another created routine should be held first. The first one should be created first and then return
State_created = 2 // already created. You do not need to create it again.
};
Delayed loading is created when the pointer () method is called for the first time (for the get () method, the pointer method will be called for implementation, and code should be reused as much as possible ).
1 ). the pointer method first does not need to be locked to quickly obtain whether the State _ variable has changed to state_created state. If it has already been created, it will be returned directly. Otherwise, you can check it by adding a multi-thread lock (call the needinstance method ), whether it is being created. If it is being created by another thread, wait until it is created and then return the pointer directly.
Here, lazyinstance tries its best to avoid locking. After an instance is created, no subsequent judgment on the status will involve locking.
2). Call
Traits::New
Chrome provides the default trait for general use. If a new method is declared for a class, the trait class is defined, and the related object application method is defined in the new class. Then, the method is passed in the second lazyinstance template.
3). Return pointer
Type* Pointer() { if (!Traits::kAllowedToAccessOnNonjoinableThread) base::ThreadRestrictions::AssertSingletonAllowed(); if ((base::subtle::Acquire_Load(&state_) != STATE_CREATED) && NeedsInstance()) { instance_ = Traits::New(buf_); void (*dtor)(void*) = Traits::Delete; CompleteInstance(this, (dtor == NULL) ? NULL : OnExit); } ANNOTATE_HAPPENS_AFTER(&state_); return instance_; }
2. Unlike Singleton, lazyinstance supports multiple instances of the same type.
From the above strength, we can see that we can define each delayed loading object globally.
3. Pre-allocate space for the lazyinstance type to reduce heap space fragments.
The member variables are pre-allocated for specific classes as follows:
Int8 Buf _ [sizeof (type)];
In this way, when we call lazyinstance, we use static global variables, which are stored in the static data area rather than the heap area. For stack, stack, static zone introduction, refer to: http://www.cnblogs.com/avril/archive/2011/04/28/2031886.html
Then the pod (what is pod: http://en.wikipedia.org/wiki/Plain_old_data_structure) type is initialized by calling the new method in trait
As follows:
Template <typename type>
Struct defaultlazyinstancetraits {
...
Static type * New (void * instance ){
Return new (Instance) type ();
}
...
}
4. Beautiful Deletion
The delete method also calls the delete method of trait. You can also specify your own Delete method.
When is delete called?
Chrome provides another class atexitmanager to manage global objects. When the entire program ends, atexitmanager calls the Destructor and calls all the callback functions registered in atexitmanager.
After the lazyinstance object is created, register Delete in the completeinstance method above to the atexitmanager instance to display the released memory to avoid leakage, as shown below:
void LazyInstanceHelper::CompleteInstance(void* instance, void (*dtor)(void*)) { base::subtle::Release_Store(&state_, STATE_CREATED); if (dtor) base::AtExitManager::RegisterCallback(dtor, instance);}
Summary:
By analyzing lazyinsance, we can know how chrome optimizes performance.
1. Minimize the lock range.
2. Try to delay loading.
3. Minimize memory fragments.