Go deep into lazy --. NET Framework 4.0

Source: Internet
Author: User

. NET framework 4 was finally released in the next hop. At an accidental opportunity, I saw anytao's [what you must know. net] 33rd back, in-depth. net 4.0, lazy <t>.

I have not seen it in. net Framework 4.0 beta2's lazy <t> implementation does not know whether the official version has been improved (changed) with the previous version. I just want to talk about it here. the implementation of lazy <t> in net framework 4.

1. Lazy <t> Overview

We may encounter such a situation where a big guy (large object) needs to be created, so it takes a long time to create this object, at the same time, you also need to allocate more space on the hosting stack.

In. NET Framework 4, the following clever method is provided: lazy <t> (we can call it a lazy object ). Of course, many people have previously made their own implementations.

Here we can sum up the role of lazy <t> as one sentence,On-Demand Loading Delay.

2. Use of lazy <t>

After learning about the role of lazy <t>, let's take a look at how lazy <t> is applied.

 Class  Program
{
Static void Main (String [] ARGs)
{
Lazy < Large > Lazyobject = New Lazy < Large > ();
Console . Writeline (lazyobject. isvaluecreated );
Lazyobject. value. Test ();
Console . Writeline (lazyobject. isvaluecreated );
}
}

[Serializable ]
Class Large
{
Public Large (){}
Public void Test ()
{
Console . Writeline ( "Test" );
}
}

The running result is as follows:

This example is very simple. It is also the most basic and common application method of lazy <t>.

3. Implement your own lazy <t>

Before. NET Framework 4.0, a large object exists. How can we deal with a large object for a large system. In my opinion, there are two points: delayed loading and instant cleaning. The former solves the creation problem, and the latter solves the recycling problem.

Let's take a look at the implementation of the. NET Framework of lazy <t>. Let's first implement a simple lazy <t>.

ClassMylazy<T>WhereT:New()
{
PrivateT value;
Private boolIsloaded;
PublicMylazy ()
{
Isloaded =False;
}
PublicT value
{
Get
{
If(! Isloaded)
{
Value =NewT ();
Isloaded =True;
}
ReturnValue;
}
}
}

This should be the simplest version of lazy <t>. There is no thread security detection, but there is nothing. Only real objects are created during access, however, it may be enough for our general applications.

4. Implement the. NET Framework of lazy <t>

I still want to explainCode, But there are too many explanations ....... Let's write some important things.

In fact, the. NET Framework and the above implementation are similar, there are two major differences:

A. introduced the internal boxed class:

 
[Serializable]
Private classBoxed
{
// Fields
InternalT m_value;

// Methods
[Targetedpatchingoptout ("Performance critical to inline this type of method defined SS ngen image boundaries")]
InternalBoxed (T value)
{
This. M_value = value;
}
}

This internal class replaces the generic constraint in the above implementation to make it more generic.

However, we should also note that if T is a struct, packing and unpacking may be more efficient because T is very large, use lazy with caution for the value type.

B. Thread Security Control

In the thread-safe control options,. NET Framework provides us with the following enumeration options:

 
Public EnumLazythreadsafetymode
{
None,
Publicationonly,
Executionandpublication
}

No extra explanation is required. The specific meanings of the three are clearly stated in msdn. You can refer to this article.

The code in it is quite troublesome.

5. Comprehensive big object Solutions

In anytaoArticleIn the reply, I mentioned the following:

I personally think that lazy + weakreference is the complete solution for implementing a large object. One can be loaded as needed, and the other can be cleaned up .....

It is perfect to add them together.

But Lao Zhao said:

Not all big objects need to be cleared automatically ......

Then I will not repeat the summary again. If these two sentences are added together, I think we can come up with a perfect big object processing solution.

In this case, can we create a separate implementation class: weaklazy <t>.

This is equivalent to having weakreference, lazy <t>, and weaklazy <t>. Can we select an application as needed!

6. Summary

I originally wanted to write something a little deeper, but at the end of the article, I found that it was still fur, sighing .......

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.