diff --git a/src/Benchmark/Benchmark.csproj b/src/Benchmark/Benchmark.csproj
index c1215562..3b9b1438 100644
--- a/src/Benchmark/Benchmark.csproj
+++ b/src/Benchmark/Benchmark.csproj
@@ -1,13 +1,42 @@
-
+
Exe
netcoreapp2.0
+ true
-
+
+
+
+
+
+
+ TextTemplatingFileGenerator
+ CustomerMapper.cs
+
+
+ TextTemplatingFileGenerator
+ FooMapper.cs
+
+
+
+
+
+
+
+ True
+ True
+ CustomerMapper.tt
+
+
+ True
+ True
+ FooMapper.tt
+
+
\ No newline at end of file
diff --git a/src/Benchmark/CustomerMapper.cs b/src/Benchmark/CustomerMapper.cs
new file mode 100644
index 00000000..eaef0f03
--- /dev/null
+++ b/src/Benchmark/CustomerMapper.cs
@@ -0,0 +1,103 @@
+
+
+
+
+
+
+
+using System.Collections.Generic;
+using Benchmark.Classes;
+
+
+namespace Benchmark
+{
+ public static class CustomerMapper
+ {
+ public static CustomerDTO Map(Customer p1)
+ {
+ return p1 == null ? null : new CustomerDTO()
+ {
+ Id = p1.Id,
+ Name = p1.Name,
+ Address = p1.Address == null ? null : new Address()
+ {
+ Id = p1.Address.Id,
+ Street = p1.Address.Street,
+ City = p1.Address.City,
+ Country = p1.Address.Country
+ },
+ HomeAddress = p1.HomeAddress == null ? null : new AddressDTO()
+ {
+ Id = p1.HomeAddress.Id,
+ City = p1.HomeAddress.City,
+ Country = p1.HomeAddress.Country
+ },
+ Addresses = func1(p1.Addresses),
+ WorkAddresses = func2(p1.WorkAddresses),
+ AddressCity = p1.Address == null ? null : p1.Address.City
+ };
+ }
+
+ private static AddressDTO[] func1(Address[] p2)
+ {
+ AddressDTO[] result;
+
+ if (p2 == null)
+ {
+ result = null;
+ }
+ else
+ {
+ result = new AddressDTO[p2.Length];
+
+ int v = 0;
+
+ int i = 0;
+ int len = p2.Length;
+
+ while (i < len)
+ {
+ Address item = p2[i];
+ result[v++] = item == null ? null : new AddressDTO()
+ {
+ Id = item.Id,
+ City = item.City,
+ Country = item.Country
+ };
+ i++;
+ }
+ }
+ return result;
+ }
+
+ private static List func2(ICollection p3)
+ {
+ List result;
+
+ if (p3 == null)
+ {
+ result = null;
+ }
+ else
+ {
+ result = new List(p3.Count);
+
+ ICollection list = result;
+
+ IEnumerator enumerator = p3.GetEnumerator();
+
+ while (enumerator.MoveNext())
+ {
+ Address item = enumerator.Current;
+ list.Add(item == null ? null : new AddressDTO()
+ {
+ Id = item.Id,
+ City = item.City,
+ Country = item.Country
+ });
+ }
+ }
+ return result;
+ }
+ }
+}
diff --git a/src/Benchmark/CustomerMapper.tt b/src/Benchmark/CustomerMapper.tt
new file mode 100644
index 00000000..4521a513
--- /dev/null
+++ b/src/Benchmark/CustomerMapper.tt
@@ -0,0 +1,35 @@
+<#@ template debug="true" hostspecific="true" language="C#" #>
+<#@ output extension=".cs" #>
+
+<#@ Assembly Name="/usr/local/share/dotnet/sdk/2.2.103/Microsoft/Microsoft.NET.Build.Extensions/net461/lib/netstandard.dll" #>
+<#@ Assembly Name="System.Core" #>
+<#@ Assembly Name="System.Runtime" #>
+<#@ Assembly Name="System.Linq.Expressions" #>
+<#@ Assembly Name="$(TargetDir)/Benchmark.dll" #>
+<#@ Assembly Name="$(TargetDir)/Mapster.dll" #>
+<#@ Assembly Name="$(TargetDir)/ExpressionTranslator.dll" #>
+<#@ import namespace="System" #>
+<#@ import namespace="System.IO" #>
+<#@ import namespace="System.Diagnostics" #>
+<#@ import namespace="System.Linq" #>
+<#@ import namespace="System.Collections" #>
+<#@ import namespace="System.Collections.Generic" #>
+<#@ import namespace="Benchmark.Classes" #>
+<#@ import namespace="ExpressionDebugger" #>
+<#@ import namespace="Mapster" #>
+
+<#
+ TypeAdapterConfig.GlobalSettings.SelfContainedCodeGeneration = true;
+ var foo = default(Customer);
+ var def = new ExpressionDefinitions
+ {
+ IsStatic = true,
+ MethodName = "Map",
+ Namespace = "Benchmark",
+ TypeName = "CustomerMapper"
+ };
+ var code = foo.BuildAdapter()
+ .CreateMapExpression()
+ .ToScript(def);
+ WriteLine(code);
+#>
\ No newline at end of file
diff --git a/src/Benchmark/FooMapper.cs b/src/Benchmark/FooMapper.cs
new file mode 100644
index 00000000..08095908
--- /dev/null
+++ b/src/Benchmark/FooMapper.cs
@@ -0,0 +1,86 @@
+
+
+
+
+
+
+
+using System;
+using System.Linq;
+using Benchmark.Classes;
+using Mapster;
+using Mapster.Utils;
+
+
+namespace Benchmark
+{
+ public static class FooMapper
+ {
+ public static Foo Map(Foo p1)
+ {
+ return p1 == null ? null : new Foo()
+ {
+ Name = p1.Name,
+ Int32 = p1.Int32,
+ Int64 = p1.Int64,
+ NullInt = p1.NullInt,
+ Floatn = p1.Floatn,
+ Doublen = p1.Doublen,
+ DateTime = p1.DateTime,
+ Foo1 = Map(p1.Foo1),
+ Foos = p1.Foos == null ? null : p1.Foos.Select(func1),
+ FooArr = func2(p1.FooArr),
+ IntArr = func3(p1.IntArr),
+ Ints = p1.Ints == null ? null : MapsterHelper.ToEnumerable(p1.Ints)
+ };
+ }
+
+ private static Foo func1(Foo p2)
+ {
+ return Map(p2);
+ }
+
+ private static Foo[] func2(Foo[] p3)
+ {
+ Foo[] result;
+
+ if (p3 == null)
+ {
+ result = null;
+ }
+ else
+ {
+ result = new Foo[p3.Length];
+
+ int v = 0;
+
+ int i = 0;
+ int len = p3.Length;
+
+ while (i < len)
+ {
+ Foo item = p3[i];
+ result[v++] = Map(item);
+ i++;
+ }
+ }
+ return result;
+ }
+
+ private static int[] func3(int[] p4)
+ {
+ int[] result;
+
+ if (p4 == null)
+ {
+ result = null;
+ }
+ else
+ {
+ result = new int[p4.Length];
+ Array.Copy(p4, 0, result, 0, p4.Length);
+ }
+ return result;
+ }
+ }
+}
diff --git a/src/Benchmark/FooMapper.tt b/src/Benchmark/FooMapper.tt
new file mode 100644
index 00000000..9fa1c2e7
--- /dev/null
+++ b/src/Benchmark/FooMapper.tt
@@ -0,0 +1,37 @@
+<#@ template debug="true" hostspecific="true" language="C#" #>
+<#@ output extension=".cs" #>
+
+<#@ Assembly Name="/usr/local/share/dotnet/sdk/2.2.103/Microsoft/Microsoft.NET.Build.Extensions/net461/lib/netstandard.dll" #>
+<#@ Assembly Name="System.Core" #>
+<#@ Assembly Name="System.Runtime" #>
+<#@ Assembly Name="System.Linq.Expressions" #>
+<#@ Assembly Name="$(TargetDir)/Benchmark.dll" #>
+<#@ Assembly Name="$(TargetDir)/Mapster.dll" #>
+<#@ Assembly Name="$(TargetDir)/ExpressionTranslator.dll" #>
+<#@ import namespace="System" #>
+<#@ import namespace="System.IO" #>
+<#@ import namespace="System.Diagnostics" #>
+<#@ import namespace="System.Linq" #>
+<#@ import namespace="System.Linq.Expressions" #>
+<#@ import namespace="System.Collections" #>
+<#@ import namespace="System.Collections.Generic" #>
+<#@ import namespace="Benchmark.Classes" #>
+<#@ import namespace="ExpressionDebugger" #>
+<#@ import namespace="Mapster" #>
+
+<#
+ TypeAdapterConfig.GlobalSettings.SelfContainedCodeGeneration = true;
+ var foo = default(Foo);
+ var def = new ExpressionDefinitions
+ {
+ IsStatic = true,
+ MethodName = "Map",
+ Namespace = "Benchmark",
+ TypeName = "FooMapper"
+ };
+ var code = foo.BuildAdapter()
+ .CreateMapExpression()
+ .ToScript(def);
+ code = code.Replace("TypeAdapter.Map.Invoke", "Map");
+ WriteLine(code);
+#>
\ No newline at end of file
diff --git a/src/Benchmark/Program.cs b/src/Benchmark/Program.cs
index ae6dc33a..54cb8777 100644
--- a/src/Benchmark/Program.cs
+++ b/src/Benchmark/Program.cs
@@ -6,6 +6,7 @@
using Mapster;
using FastExpressionCompiler;
using System.Linq.Expressions;
+using ExpressionDebugger;
namespace Benchmark
{
@@ -117,10 +118,11 @@ static void Test(Customer item, int iterations)
item.Adapt(); //exercise
TestMapsterAdapter(item, iterations, ref sMP);
- TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileFast(); //switch compiler
- TypeAdapterConfig.GlobalSettings.Compile(typeof(Customer), typeof(CustomerDTO)); //recompile
- item.Adapt(); //exercise
- TestMapsterAdapter(item, iterations, ref sMF);
+ //TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileFast(); //switch compiler
+ //TypeAdapterConfig.GlobalSettings.Compile(typeof(Customer), typeof(CustomerDTO)); //recompile
+ //item.Adapt(); //exercise
+ //TestMapsterAdapter(item, iterations, ref sMF);
+ TestCodeGen(item, iterations, ref sMF);
ExpressMapper.Mapper.Map(item); //exercise
TestExpressMapper(item, iterations, ref sEM);
@@ -143,10 +145,11 @@ static void TestSimple(Foo item, int iterations)
item.Adapt(); //exercise
TestMapsterAdapter(item, iterations, ref sMP);
- TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileFast(); //switch compiler
- TypeAdapterConfig.GlobalSettings.Compile(typeof(Foo), typeof(Foo)); //recompile
- item.Adapt(); //exercise
- TestMapsterAdapter(item, iterations, ref sMF);
+ //TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileFast(); //switch compiler
+ //TypeAdapterConfig.GlobalSettings.Compile(typeof(Foo), typeof(Foo)); //recompile
+ //item.Adapt(); //exercise
+ //TestMapsterAdapter(item, iterations, ref sMF);
+ TestCodeGen(item, iterations, ref sMF);
//TestValueInjecter(item, iterations);
@@ -218,6 +221,20 @@ static void TestAutoMapper(TSrc item, int iterations, ref double co
counter += time;
}
+ static void TestCodeGen(Customer item, int iterations, ref double counter)
+ {
+ var time = Loop(item, get => CustomerMapper.Map(get), iterations);
+ Console.WriteLine("Codegen:\t\t" + time);
+ counter += time;
+ }
+
+ static void TestCodeGen(Foo item, int iterations, ref double counter)
+ {
+ var time = Loop(item, get => FooMapper.Map(get), iterations);
+ Console.WriteLine("Codegen:\t\t" + time);
+ counter += time;
+ }
+
static long Loop(T item, Action action, int iterations = 1000000)
{
return Time(item, a =>
diff --git a/src/Mapster/Adapters/BaseAdapter.cs b/src/Mapster/Adapters/BaseAdapter.cs
index db6d167b..6e337300 100644
--- a/src/Mapster/Adapters/BaseAdapter.cs
+++ b/src/Mapster/Adapters/BaseAdapter.cs
@@ -63,6 +63,8 @@ protected virtual bool CanInline(Expression source, Expression destination, Comp
return false;
if (arg.Settings.AfterMappingFactories.Count > 0)
return false;
+ if (arg.Settings.BeforeMappingFactories.Count > 0)
+ return false;
if (arg.Settings.Includes.Count > 0)
return false;
return true;
@@ -70,16 +72,18 @@ protected virtual bool CanInline(Expression source, Expression destination, Comp
protected virtual Expression CreateExpressionBody(Expression source, Expression destination, CompileArgument arg)
{
- if (this.CheckExplicitMapping
- && arg.Context.Config.RequireExplicitMapping
- && !arg.ExplicitMapping)
+ if (this.CheckExplicitMapping)
{
- throw new InvalidOperationException("Implicit mapping is not allowed (check GlobalSettings.RequireExplicitMapping) and no configuration exists");
+ if (arg.Settings.MaxDepth <= 0)
+ return arg.DestinationType.CreateDefault();
+
+ if (arg.Context.Config.RequireExplicitMapping && !arg.ExplicitMapping)
+ throw new InvalidOperationException("Implicit mapping is not allowed (check GlobalSettings.RequireExplicitMapping) and no configuration exists");
}
if (CanInline(source, destination, arg) && arg.Settings.AvoidInlineMapping != true)
return CreateInlineExpressionBody(source, arg).To(arg.DestinationType, true);
- else if (arg.Context.Running.Count > 1)
+ else if (!arg.Context.Config.SelfContainedCodeGeneration && arg.Context.Running.Count > 1)
return null;
else
return CreateBlockExpressionBody(source, destination, arg);
@@ -96,34 +100,22 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression des
if (set.NodeType != ExpressionType.Throw)
{
- Expression assign = Expression.Assign(result, set);
- set = CreateBlockExpression(source, result, arg);
+ var actions = new List
+ {
+ Expression.Assign(result, set)
+ };
+
+ //before(source, result);
+ var beforeMappings = arg.Settings.BeforeMappingFactories.Select(it => InvokeMapping(it, source, result, arg));
+ actions.AddRange(beforeMappings);
//result.prop = adapt(source.prop);
- //action(source, result);
- if (arg.Settings.AfterMappingFactories.Count > 0)
- {
- var actions = new List { set };
-
- foreach (var afterMappingFactory in arg.Settings.AfterMappingFactories)
- {
- var afterMapping = afterMappingFactory(arg);
- var args = afterMapping.Parameters;
- Expression invoke;
- if (args[0].Type.IsReferenceAssignableFrom(source.Type) && args[1].Type.IsReferenceAssignableFrom(result.Type))
- {
- var replacer = new ParameterExpressionReplacer(args, source, result);
- invoke = replacer.Visit(afterMapping.Body);
- }
- else
- {
- invoke = Expression.Invoke(afterMapping, source.To(args[0].Type), result.To(args[1].Type));
- }
- actions.Add(invoke);
- }
+ set = CreateBlockExpression(source, result, arg);
+ actions.Add(set);
- set = Expression.Block(actions);
- }
+ //after(source, result);
+ var afterMappings = arg.Settings.AfterMappingFactories.Select(it => InvokeMapping(it, source, result, arg));
+ actions.AddRange(afterMappings);
//using (var scope = new MapContextScope()) {
// var references = scope.Context.Reference;
@@ -153,7 +145,8 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression des
var refAssign = Expression.Assign(
Expression.Property(dict, indexer, Expression.Convert(source, typeof(object))),
Expression.Convert(result, typeof(object)));
- var setResultAndCache = Expression.Block(assign, refAssign, set);
+ actions.Add(refAssign);
+ var setResultAndCache = Expression.Block(actions);
var cache = Expression.Variable(typeof(object), "cache");
var tryGetMethod = typeof(Dictionary