Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disadvantages of Lazy<T>?

Tags:

c#

mef

I recently started using Lazy throughout my application, and I was wondering if there are any obvious negative aspects that I need to take into account when using Lazy<T>?

I am trying to utilize Lazy<T> as often as I deem it appropriate, primarily to help reduce the memory footprint of our loaded, but inactive plugins.

like image 494
eandersson Avatar asked Sep 27 '11 10:09

eandersson


People also ask

Is Lazy T thread-safe?

By default, Lazy<T> objects are thread-safe. That is, if the constructor does not specify the kind of thread safety, the Lazy<T> objects it creates are thread-safe.

What is Lazy T?

The Lazy<T> object ensures that all threads use the same instance of the lazily initialized object and discards the instances that are not used.

Should I use Lazy C#?

Using the Lazy<T> class in C# Although you can write your own custom code to implement lazy initialization, Microsoft recommends using the Lazy<T> class instead. The Lazy<T> class in the System namespace in C# was introduced as part of . Net Framework 4.0 to provide a thread-safe way to implement lazy initialization.

What is a Lazy class?

The idea of Lazy class is simple - initialize class with action that builds or gets lazy loaded value and when value is asked then run the action if lazy loading is not happened yet.


2 Answers

I'll expand a bit on my comment, which reads:

I've just started using Lazy, and find that it's often indicative of bad design; or laziness on the part of the programmer. Also, one disadvantage is that you have to be more vigilant with scoped up variables, and create proper closures.

For example, I've used Lazy<T> to create the pages the user can see in my (sessionless) MVC app. It's a guiding wizard, so the user might want to go to a random previous step. When the handshake is made, an array of Lazy<Page> objects is crated, and if the user specifies as step, that exact page is evaluated. I find it delivers good performance, but there are some aspects to it that I don't like, for example many of my foreach constructs now look like this:

foreach(var something in somethings){      var somethingClosure = something;      list.Add(new Lazy<Page>(() => new Page(somethingClosure)); }  

I.e. you have to deal with the problem of closures very proactively. Otherwise I don't think it's such a bad performance hit to store a lambda and evaluate it when needed.

On the other hand this might be indicative that the programmer is being a Lazy<Programmer>, in the sense that you'd prefer not thinking through your program now, and instead let the proper logic evaluate when needed, as with example in my case - instead of building that array, I could just figure out just what that specific requested page would be; but I chose to be lazy, and do an all in approach.

EDIT

It occurs to me that Lazy<T> also has a few peculiars when working with concurrency. For example there's a ThreadLocal<T> for some scenarios, and several flag configurations for your particular multi-threaded scenario. You can read more on msdn.

like image 196
Gleno Avatar answered Oct 06 '22 19:10

Gleno


In my opinion, you should always have a reason for choosing Lazy. There are several alternatives depending on the use case and there are definitely cases where this structure is appropriate. But don't use it just because it's cool.

For example I don't get the point in the page selection example in one of the other answers. Using a list of Lazy for selecting a single element can be well done with a list or dictionary of delegates directly without using Lazy or with a simple switch statement.

So the most obvious alternatives are

  • direct instantiation for cheap data structures or structures that are anyway needed
  • delegates for things that are needed zero to few times in some algorithm
  • some caching structure for items that should free the memory when not being used for some time
  • some kind of "future" structure like Task that already may start initializing asynchronously before actual usage consuming idle CPU time in cases where the probability is quite high that the structure will be required later on

In contrast to that, Lazy is often suitable when

  • computationally intense data structures
  • are needed zero to many times in some algorithm where the zero case has a significant probability
  • and the data is local to some method or class and can be garbage collected when not in use any more or the data should be kept in memory for the whole program's runtime
like image 21
CSharper Avatar answered Oct 06 '22 18:10

CSharper