From 1dca8f4beb9d2aa9788a06d40ff7505fcb6677b7 Mon Sep 17 00:00:00 2001 From: avk Date: Tue, 14 Aug 2018 13:33:20 +0500 Subject: [PATCH] review fixes --- Mutators.Tests/TreeMutatorsTest.cs | 2 - Mutators/ConverterCollection.cs | 77 ++++++++++-------------------- Mutators/MutatorsContext.cs | 12 +---- Mutators/MutatorsTreeBase.cs | 10 ---- 4 files changed, 27 insertions(+), 74 deletions(-) diff --git a/Mutators.Tests/TreeMutatorsTest.cs b/Mutators.Tests/TreeMutatorsTest.cs index 67fe8b65..e870c3e5 100644 --- a/Mutators.Tests/TreeMutatorsTest.cs +++ b/Mutators.Tests/TreeMutatorsTest.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Linq.Expressions; -using GrobExp.Compiler; using GrobExp.Mutators; using GrobExp.Mutators.Exceptions; diff --git a/Mutators/ConverterCollection.cs b/Mutators/ConverterCollection.cs index ce316c00..e23fc1f6 100644 --- a/Mutators/ConverterCollection.cs +++ b/Mutators/ConverterCollection.cs @@ -47,7 +47,7 @@ public Action GetMerger(MutatorsContext context) public MutatorsTreeBase Migrate(MutatorsTreeBase mutatorsTree, MutatorsContext context) { - return mutatorsTree == null ? null : mutatorsTree.Migrate(GetOrCreateHashtableSlot(context).ConverterTree); + return mutatorsTree?.Migrate(GetOrCreateHashtableSlot(context).ConverterTree); } public MutatorsTreeBase GetValidationsTree(MutatorsContext context, int priority) @@ -71,7 +71,7 @@ public MutatorsTreeBase GetValidationsTree(MutatorsContext context, int public MutatorsTreeBase MigratePaths(MutatorsTreeBase mutatorsTree, MutatorsContext context) { - return mutatorsTree == null ? null : mutatorsTree.MigratePaths(GetOrCreateHashtableSlot(context).ConverterTree); + return mutatorsTree?.MigratePaths(GetOrCreateHashtableSlot(context).ConverterTree); } protected abstract void Configure(MutatorsContext context, ConverterConfigurator configurator); @@ -105,33 +105,37 @@ private HashtableSlot GetOrCreateHashtableSlot(MutatorsContext context) tree.ExtractValidationsFromConverters(validationsTree); var treeConverter = (Expression>)tree.BuildTreeMutator(typeof(TSource)); - var sw = Stopwatch.StartNew(); - var compiledTreeConverter = LambdaCompiler.Compile(treeConverter, CompilerOptions.All); - var compilationTime = sw.ElapsedMilliseconds; - - var typeName = GetType().Name; - var properties = GetLogProperties(context, typeName, compilationTime); - logger.Log(new LogEvent(LogLevel.Info, DateTimeOffset.Now, $"{typeName} was compiled in {compilationTime} ms", properties)); + Action compiledTreeConverter; + var sw = Stopwatch.StartNew(); + try + { + compiledTreeConverter = LambdaCompiler.Compile(treeConverter, CompilerOptions.All); + } + finally + { + sw.Stop(); + LogConverterCompilation(context, sw); + } slot = new HashtableSlot { ConverterTree = tree, ValidationsTree = validationsTree, - Converter = (source => + Converter = source => { var dest = new TDest(); BeforeConvert(source); compiledTreeConverter(dest, source); AfterConvert(dest, source); return dest; - }), - Merger = ((source, dest) => + }, + Merger = (source, dest) => { BeforeConvert(source); compiledTreeConverter(dest, source); AfterConvert(dest, source); - }), + }, ValidationMutatorsTrees = new Hashtable() }; //if(!MutatorsAssignRecorder.IsRecording()) @@ -143,45 +147,17 @@ private HashtableSlot GetOrCreateHashtableSlot(MutatorsContext context) return slot; } - private static Dictionary GetLogProperties(MutatorsContext context, string typeName, long compilationTime) + private void LogConverterCompilation(MutatorsContext context, Stopwatch sw) { - var properties = new Dictionary + var logProperties = new Dictionary { - {"ConverterCollectionName", typeName}, - {"CompilationTime", compilationTime} - }; - foreach (var propertyPair in context.GetProperties()) - properties.Add($"Context.{propertyPair.Key}", propertyPair.Value); - return properties; - } - - private HashtableSlot CreateHashtableSlot(MutatorsContext context) - { - var converterTree = ModelConfigurationNode.CreateRoot(typeof(TDest)); - ConfigureInternal(context, new ConverterConfigurator(converterTree)); - var validationsTree = ModelConfigurationNode.CreateRoot(typeof(TSource)); - converterTree.ExtractValidationsFromConverters(validationsTree); - var treeMutator = (Expression>)converterTree.BuildTreeMutator(typeof(TSource)); - var compiledTreeMutator = LambdaCompiler.Compile(treeMutator, CompilerOptions.All); - return new HashtableSlot - { - ConverterTree = converterTree, - ValidationsTree = validationsTree, - Converter = source => - { - var dest = new TDest(); - BeforeConvert(source); - compiledTreeMutator(dest, source); - AfterConvert(dest, source); - return dest; - }, - Merger = (source, dest) => - { - BeforeConvert(source); - compiledTreeMutator(dest, source); - AfterConvert(dest, source); - } + {"ConverterCollectionName", GetType().Name}, + {"CompilationTimeMilliseconds", sw.ElapsedMilliseconds} }; + var mutatorsContextTypeName = context.GetType().Name; + foreach (var propertyInfo in context.GetType().GetProperties()) + logProperties.Add($"{mutatorsContextTypeName}.{propertyInfo.Name}", propertyInfo.GetValue(context)); + logger.Log(new LogEvent(LogLevel.Info, DateTimeOffset.UtcNow, "{ConverterCollectionName} was compiled in {CompilationTimeMilliseconds} ms", logProperties)); } private static TypeCode GetTypeCode(Type type) @@ -484,8 +460,7 @@ private void ConfigureCustomFieldsForArrays(ConverterConfigurator GetProperties() - { - var properties = GetType().GetProperties(); - var propertiesDict = properties.ToDictionary(prop => prop.Name, prop => prop.GetValue(this)); - return propertiesDict; - } - public static readonly MutatorsContext Empty = new EmptyMutatorsContext(); } @@ -21,7 +11,7 @@ public sealed class EmptyMutatorsContext : MutatorsContext { public override string GetKey() { - return ""; + return string.Empty; } } } \ No newline at end of file diff --git a/Mutators/MutatorsTreeBase.cs b/Mutators/MutatorsTreeBase.cs index 89d50727..4d3afdd8 100644 --- a/Mutators/MutatorsTreeBase.cs +++ b/Mutators/MutatorsTreeBase.cs @@ -1,10 +1,8 @@ using System; using System.Collections; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Linq.Expressions; -using System.Reflection; using GrobExp.Mutators.Aggregators; using GrobExp.Mutators.ModelConfiguration; @@ -127,13 +125,6 @@ internal MutatorsTreeBase MigratePaths(string key, ModelConfigurationN public abstract MutatorsTreeBase Merge(MutatorsTreeBase other); - public void SetValidatorsAssembly() - { - if (File.Exists("Validators.dll")) - validatorsAssembly = Assembly.LoadFrom("Validators.dll"); - } - - //TODO: прикрутить чтение из dll, как и в компиляторах internal Action GetValidatorInternal(Expression> path) { var nodeInfo = GetOrCreateNodeInfo(path); @@ -279,7 +270,6 @@ private NodeInfo GetOrCreateNodeInfo(Expression> path) return nodeInfo; } - private Assembly validatorsAssembly; private readonly Hashtable hashtable = new Hashtable(); private readonly object lockObject = new object();