LightInject icon indicating copy to clipboard operation
LightInject copied to clipboard

PerContainerLifetime speed up

Open xmedeko opened this issue 8 years ago • 1 comments

Hi, PerContainerLifetime.GetInstance() may be sped up by using local variable:

object result = singleton;
if (result != null)
{
    return result;
}

lock (syncRoot)
{
    result = singleton;
    if (result == null)
    {
        result = singleton = createInstance();
     }
}
return result;

The code tries to minimize the access to the volatile variable.

BTW. why not lock(this) and remove the syncRoot?

xmedeko avatar Jan 15 '16 17:01 xmedeko