StyletIoC: Don't cache expressions by default

This in turn causes ConstructorInfos to be cached, and those are pretty
expensive.
This commit is contained in:
Antony Male 2015-03-15 11:52:16 +00:00
parent 9b1b1308b6
commit 95a9995746
3 changed files with 30 additions and 4 deletions

View File

@ -109,8 +109,15 @@ namespace StyletIoC.Internal.Creators
var completeExpression = this.CompleteExpressionFromCreator(creator, registrationContext);
if (StyletIoCContainer.CacheGeneratedExpressions)
{
Interlocked.CompareExchange(ref this.creationExpression, completeExpression, null);
return this.creationExpression;
}
else
{
return completeExpression;
}
}
}
}

View File

@ -60,8 +60,15 @@ namespace StyletIoC.Internal.Registrations
var listNew = Expression.New(listCtor, Expression.Constant(instanceExpressions.Length));
Expression list = instanceExpressions.Any() ? (Expression)Expression.ListInit(listNew, instanceExpressions) : listNew;
if (StyletIoCContainer.CacheGeneratedExpressions)
{
Interlocked.CompareExchange(ref this.expression, list, null);
return this.expression;
}
else
{
return list;
}
}
}
}

View File

@ -11,5 +11,17 @@ namespace StyletIoC
/// Name of the assembly in which abstract factories are built. Use in [assembly: InternalsVisibleTo(StyletIoCContainer.FactoryAssemblyName)] to allow factories created by .ToAbstractFactory() to access internal types
/// </summary>
public static readonly string FactoryAssemblyName = "StyletIoCFactory";
/// <summary>
/// Gets or sets a value indicating whether generated Expressions are cached, or resolved anew each time.
/// Setting to true may speed up the compilation phase in the case where one type is required by many other types.
/// However, it will cause loads of ConstructorInfos to be cached forever, and they're actually pretty expensive to hold on to.
/// </summary>
public static bool CacheGeneratedExpressions { get; set; }
static StyletIoCContainer()
{
CacheGeneratedExpressions = false;
}
}
}