From 73ab9292e786722a76564c77e7e462dd6d9d5d75 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Sun, 24 Nov 2019 04:11:15 +0100 Subject: [PATCH] ObjectActivator: Cache creators on a dictionary. --- VAR.Json/ObjectActivator.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/VAR.Json/ObjectActivator.cs b/VAR.Json/ObjectActivator.cs index 69ed4f5..52d6588 100644 --- a/VAR.Json/ObjectActivator.cs +++ b/VAR.Json/ObjectActivator.cs @@ -6,7 +6,7 @@ namespace VAR.Json { public class ObjectActivator { - private static Dictionary> _creators = new Dictionary>(); + private static readonly Dictionary> _creators = new Dictionary>(); public static Func GetLambdaNew(Type type) { @@ -17,13 +17,18 @@ namespace VAR.Json lock (_creators) { + if (_creators.ContainsKey(type)) + { + return _creators[type]; + } + NewExpression newExp = Expression.New(type); LambdaExpression lambda = Expression.Lambda(typeof(Func), newExp); Func compiledLambdaNew = (Func)lambda.Compile(); _creators.Add(type, compiledLambdaNew); + return _creators[type]; } - return _creators[type]; } public static object CreateInstance(Type type)