Artem Community
Artem interactive communities
The laziest property lazy-initialization

Today I want to revue the history of property lazy-initialization I used over the years.
Further more, I decided to "measure" if I'm changing for good and is the pattern I use last is the best, the most optimized one.

First, in the old good years I used the "standard" lazy-initialization pattern:

public string First {

    get {

        if (_first == null) {

            _first = "Initialized";

        }

        return _first;

    }

}

Then, at some point I realized I could use by my opinion better pattern:

public string Second {

    get {

        return (_second != null) ? _second : (_second = "Initialized");

    }

}

Last, I found out the good usage of "??" operator on property lazy-initialization and here is my latest pattern:

public string Last {

    get {

        return _last ?? (_last = "Initialized");

    }

}

So, in code the last one looks better, shorter and more optimized like.
But, is that the real case. I decided to check out the generated IL and here are the results:

First :

.method public hidebysig specialname instance string

        get_First() cil managed

{

  // Code size       41 (0x29)

  .maxstack  2

  .locals init ([0] string CS$1$0000,

           [1] bool CS$4$0001)

  IL_0000nop

  IL_0001ldarg.0

  IL_0002ldfld      string Artem.WebSite.Models.Test::_first

  IL_0007ldnull

  IL_0008ceq

  IL_000aldc.i4.0

  IL_000bceq

  IL_000dstloc.1

  IL_000eldloc.1

  IL_000fbrtrue.s   IL_001e

  IL_0011nop

  IL_0012ldarg.0

  IL_0013ldstr      "Initialized"

  IL_0018stfld      string Artem.WebSite.Models.Test::_first

  IL_001dnop

  IL_001eldarg.0

  IL_001fldfld      string Artem.WebSite.Models.Test::_first

  IL_0024stloc.0

  IL_0025br.s       IL_0027

  IL_0027ldloc.0

  IL_0028ret

} // end of method Test::get_First

Second:

.method public hidebysig specialname instance string

        get_Second() cil managed

{

  // Code size       36 (0x24)

  .maxstack  3

  .locals init ([0] string CS$1$0000,

           [1] string CS$0$0001)

  IL_0000nop

  IL_0001ldarg.0

  IL_0002ldfld      string Artem.WebSite.Models.Test::_second

  IL_0007brtrue.s   IL_0019

  IL_0009ldarg.0

  IL_000aldstr      "Initialized"

  IL_000fdup

  IL_0010stloc.1

  IL_0011stfld      string Artem.WebSite.Models.Test::_second

  IL_0016ldloc.1

  IL_0017br.s       IL_001f

  IL_0019ldarg.0

  IL_001aldfld      string Artem.WebSite.Models.Test::_second

  IL_001fstloc.0

  IL_0020br.s       IL_0022

  IL_0022ldloc.0

  IL_0023ret

} // end of method Test::get_Second

Last:

.method public hidebysig specialname instance string

        get_Last() cil managed

{

  // Code size       30 (0x1e)

  .maxstack  3

  .locals init ([0] string CS$1$0000,

           [1] string CS$0$0001)

  IL_0000nop

  IL_0001ldarg.0

  IL_0002ldfld      string Artem.WebSite.Models.Test::_last

  IL_0007dup

  IL_0008brtrue.s   IL_0019

  IL_000apop

  IL_000bldarg.0

  IL_000cldstr      "Initialized"

  IL_0011dup

  IL_0012stloc.1

  IL_0013stfld      string Artem.WebSite.Models.Test::_last

  IL_0018ldloc.1

  IL_0019stloc.0

  IL_001abr.s       IL_001c

  IL_001cldloc.0

  IL_001dret

} // end of method Test::get_Last


Less lines in IL for every next pattern, which should means better and more optimized code.
Great. I'm happy now and I'm sure my lazy-initialization pattern has changed for good

 

Hope this helps...

Regards


Posted 02-13-2009 10:37 by velio
Powered by Community Server (Non-Commercial Edition), by Telligent Systems