Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
667a3d5
use Enum.Parse<TEnum>
lbargaoanu Jul 30, 2022
77086e8
less allocations
lbargaoanu Jul 31, 2022
e3aecd3
reuse the execution plan between user Map calls and internal Map calls
lbargaoanu Aug 1, 2022
6581906
remove IObjectMapperInfo
lbargaoanu Aug 2, 2022
5f0cebd
use the more efficient Expression.Invoke instead of ConvertReplacePar…
lbargaoanu Aug 3, 2022
9fc7cdf
less allocations
lbargaoanu Aug 3, 2022
7fdf915
less null checks
lbargaoanu Aug 4, 2022
4b507ee
unreachable, As maps are sealed by default
lbargaoanu Aug 6, 2022
84e3fff
less allocations
lbargaoanu Aug 6, 2022
b5cc0a6
don't try to match ignored members
lbargaoanu Aug 7, 2022
df48e72
more efficient
lbargaoanu Aug 7, 2022
12eaef9
drop RegEx
lbargaoanu Aug 8, 2022
06b0713
docs
lbargaoanu Aug 8, 2022
dc7e58f
cosmetic
lbargaoanu Aug 8, 2022
9356388
less allocations
lbargaoanu Aug 8, 2022
39fccd9
cosmetic
lbargaoanu Aug 9, 2022
a3ae964
less allocations
lbargaoanu Aug 9, 2022
00d29ee
we don't need to create As type maps
lbargaoanu Aug 11, 2022
c9bceef
remove ITypeMapConfiguration
lbargaoanu Aug 11, 2022
a0988d7
cosmetic
lbargaoanu Aug 11, 2022
0b9cf8c
try to show a better error message when mapping records through const…
lbargaoanu Aug 11, 2022
b9699dd
less allocations
lbargaoanu Aug 13, 2022
ed9097f
special case the defaults for naming conventions
lbargaoanu Aug 13, 2022
93576ef
make TypeMapPlanBuilder a struct
lbargaoanu Aug 15, 2022
3adb02f
use readonly record struct
lbargaoanu Aug 15, 2022
7720ac8
assume that a protected constructor with CompilerGeneratedAttribute m…
lbargaoanu Aug 15, 2022
4e1117f
less allocations
lbargaoanu Aug 15, 2022
dac3062
rename configurationProvider to the shorter configuration in most places
lbargaoanu Aug 17, 2022
b24560f
less allocations
lbargaoanu Aug 17, 2022
953d66d
converters with no source should throw
lbargaoanu Aug 17, 2022
ebd2c43
less allocations
lbargaoanu Aug 18, 2022
c5d7c36
cosmetic
lbargaoanu Aug 18, 2022
12676dd
less allocations
lbargaoanu Aug 19, 2022
85ff705
make MapFrom name more like the built-in mapping
lbargaoanu Aug 19, 2022
c71f168
less allocations
lbargaoanu Aug 20, 2022
3de255f
cosmetic
lbargaoanu Aug 21, 2022
6925737
use profiles in the benchmark
lbargaoanu Aug 21, 2022
08ec72a
simplify conventions
lbargaoanu Aug 21, 2022
e9287a2
merge the profile member configuration with the global one
lbargaoanu Aug 21, 2022
0d2aa50
cosmetic
lbargaoanu Aug 22, 2022
ec0a832
test reusing the execution plan
lbargaoanu Aug 22, 2022
593ad53
refactor test base classes
lbargaoanu Aug 22, 2022
806e73f
cosmetic
lbargaoanu Aug 23, 2022
d0e0e24
avoid changing the resolver configuration objects because they're reu…
lbargaoanu Aug 31, 2022
77e0015
cosmetic
lbargaoanu Aug 31, 2022
20c283a
less allocations
lbargaoanu Sep 3, 2022
66f0020
cosmetic
lbargaoanu Sep 3, 2022
f1655e1
file scoped namespaces
lbargaoanu Sep 3, 2022
288c4ad
global usings
lbargaoanu Sep 3, 2022
e6f2ca3
cosmetic
lbargaoanu Sep 4, 2022
daacd80
less allocations
lbargaoanu Sep 10, 2022
c608787
NullMappingOrderComesFirst
lbargaoanu Sep 10, 2022
434703c
less allocations
lbargaoanu Sep 10, 2022
4ba0e5f
we could include a different constructor
lbargaoanu Sep 10, 2022
b98273e
less allocations
lbargaoanu Sep 10, 2022
7eeaf59
cosmetic
lbargaoanu Sep 11, 2022
efd1df9
allocate _runtimeTypeDetails lazily
lbargaoanu Sep 12, 2022
3a491a3
create the projection builder lazily
lbargaoanu Sep 12, 2022
7da36f6
cosmetic
lbargaoanu Sep 12, 2022
970ab2d
an adhoc fix for including open generic config
lbargaoanu Sep 14, 2022
e7ba023
cosmetic
lbargaoanu Sep 18, 2022
2b99e96
MapFrom should override ignore
lbargaoanu Sep 18, 2022
3f8448a
UseDestinationValueNullable
lbargaoanu Sep 19, 2022
8b83aa7
cosmetic
lbargaoanu Sep 21, 2022
5e0066e
avoid LINQ
lbargaoanu Sep 23, 2022
7d65b78
the destination type name is already in the error message
lbargaoanu Sep 24, 2022
3c2d8cd
cosmetic
lbargaoanu Sep 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
less allocations
  • Loading branch information
lbargaoanu committed Aug 16, 2022
commit 4e1117f67080d7ec2f94d121607f16e6ecc75dce
4 changes: 3 additions & 1 deletion src/AutoMapper/ApiCompatBaseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ MembersMustExist : Member 'public System.Boolean AutoMapper.Execution.TypeDescri
MembersMustExist : Member 'public System.Boolean AutoMapper.Execution.TypeDescription.op_Inequality(AutoMapper.Execution.TypeDescription, AutoMapper.Execution.TypeDescription)' does not exist in the implementation but it does exist in the contract.
CannotSealType : Type 'AutoMapper.Execution.TypeMapPlanBuilder' is actually (has the sealed modifier) sealed in the implementation but not sealed in the contract.
TypeCannotChangeClassification : Type 'AutoMapper.Execution.TypeMapPlanBuilder' is a 'struct' in the implementation but is a 'class' in the contract.
MembersMustExist : Member 'public System.Linq.Expressions.LambdaExpression AutoMapper.Execution.TypeMapPlanBuilder.CreateMapperLambda(System.Collections.Generic.HashSet<AutoMapper.TypeMap>)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Reflection.ConstructorInfo System.Reflection.ConstructorInfo AutoMapper.Internal.ConstructorParameters.Constructor' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Reflection.ParameterInfo[] System.Reflection.ParameterInfo[] AutoMapper.Internal.ConstructorParameters.Parameters' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public AutoMapper.Internal.TypePair AutoMapper.Internal.TypePair AutoMapper.Internal.MapRequest.RequestedTypes' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public AutoMapper.Internal.TypePair AutoMapper.Internal.TypePair AutoMapper.Internal.MapRequest.RuntimeTypes' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public AutoMapper.MemberMap AutoMapper.MemberMap AutoMapper.Internal.MapRequest.MemberMap' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Boolean AutoMapper.Internal.MapRequest.op_Equality(AutoMapper.Internal.MapRequest, AutoMapper.Internal.MapRequest)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Boolean AutoMapper.Internal.MapRequest.op_Inequality(AutoMapper.Internal.MapRequest, AutoMapper.Internal.MapRequest)' does not exist in the implementation but it does exist in the contract.
CannotChangeAttribute : Attribute 'System.Runtime.CompilerServices.IteratorStateMachineAttribute' on 'AutoMapper.Internal.TypeExtensions.GetTypeInheritance(System.Type)' changed from '[IteratorStateMachineAttribute(typeof(TypeExtensions.<GetTypeInheritance>d__19))]' in the contract to '[IteratorStateMachineAttribute(typeof(TypeExtensions.<GetTypeInheritance>d__22))]' in the implementation.
MembersMustExist : Member 'public System.Type System.Type AutoMapper.Internal.TypePair.DestinationType' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Type System.Type AutoMapper.Internal.TypePair.SourceType' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Boolean AutoMapper.Internal.TypePair.Equals(AutoMapper.Internal.TypePair)' does not exist in the implementation but it does exist in the contract.
Expand All @@ -86,4 +88,4 @@ MembersMustExist : Member 'public AutoMapper.Internal.TypePair AutoMapper.Intern
MembersMustExist : Member 'public System.Linq.IQueryable<TDestination> AutoMapper.QueryableExtensions.Extensions.Map<TSource, TDestination>(System.Linq.IQueryable<TSource>, System.Linq.IQueryable<TDestination>, AutoMapper.IConfigurationProvider)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Collections.Generic.IReadOnlyCollection<System.Reflection.MemberInfo> AutoMapper.QueryableExtensions.MemberVisitor.MemberPath.get()' does not exist in the implementation but it does exist in the contract.
TypesMustExist : Type 'AutoMapper.QueryableExtensions.Impl.MemberAccessQueryMapperVisitor' does not exist in the implementation but it does exist in the contract.
Total Issues: 87
Total Issues: 89
25 changes: 19 additions & 6 deletions src/AutoMapper/Configuration/MapperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Security.Cryptography;
using AutoMapper.Configuration;
using AutoMapper.Features;
using AutoMapper.Internal;
Expand Down Expand Up @@ -50,8 +51,6 @@ public class MapperConfiguration : IGlobalConfiguration
private readonly IObjectMapper[] _mappers;
private readonly Dictionary<TypePair, TypeMap> _configuredMaps;
private readonly Dictionary<TypePair, TypeMap> _resolvedMaps;
private HashSet<TypeMap> _typeMapsPath = new();
private List<MemberInfo> _sourceMembers = new();
private readonly LockingConcurrentDictionary<TypePair, TypeMap> _runtimeMaps;
private readonly ProjectionBuilder _projectionBuilder;
private readonly LockingConcurrentDictionary<MapRequest, Delegate> _executionPlans;
Expand All @@ -63,6 +62,12 @@ public class MapperConfiguration : IGlobalConfiguration
private readonly Func<Type, object> _serviceCtor;
private readonly bool _sealed;
private readonly bool _hasOpenMaps;
private readonly HashSet<TypeMap> _typeMapsPath = new();
private readonly List<MemberInfo> _sourceMembers = new();
private readonly List<ParameterExpression> _variables = new();
private readonly ParameterExpression[] _parameters = new ParameterExpression[] { null, null, ContextParameter };
private readonly CatchBlock[] _catches = new CatchBlock[1];
private readonly List<Expression> _expressions = new();
public MapperConfiguration(MapperConfigurationExpression configurationExpression)
{
var configuration = (IGlobalConfigurationExpression)configurationExpression;
Expand Down Expand Up @@ -107,6 +112,10 @@ public MapperConfiguration(MapperConfigurationExpression configurationExpression
}
_typeMapsPath = null;
_sourceMembers = null;
_expressions = null;
_variables = null;
_parameters = null;
_catches = null;
_sealed = true;
return;
void Seal()
Expand Down Expand Up @@ -134,7 +143,7 @@ void Seal()
}
foreach (var typeMap in _configuredMaps.Values)
{
typeMap.Seal(this, _typeMapsPath);
typeMap.Seal(this);
}
_features.Seal(this);
}
Expand Down Expand Up @@ -245,6 +254,11 @@ static MapperConfigurationExpression Build(Action<IMapperConfigurationExpression
int IGlobalConfiguration.RecursiveQueriesMaxDepth => _recursiveQueriesMaxDepth;
Features<IRuntimeFeature> IGlobalConfiguration.Features => _features;
List<MemberInfo> IGlobalConfiguration.SourceMembers => _sourceMembers;
List<ParameterExpression> IGlobalConfiguration.Variables => _variables;
List<Expression> IGlobalConfiguration.Expressions => _expressions;
HashSet<TypeMap> IGlobalConfiguration.TypeMapsPath => _typeMapsPath;
ParameterExpression[] IGlobalConfiguration.Parameters => _parameters;
CatchBlock[] IGlobalConfiguration.Catches => _catches;
Func<TSource, TDestination, ResolutionContext, TDestination> IGlobalConfiguration.GetExecutionPlan<TSource, TDestination>(in MapRequest mapRequest)
=> (Func<TSource, TDestination, ResolutionContext, TDestination>)GetExecutionPlan(mapRequest);
private Delegate GetExecutionPlan(in MapRequest mapRequest) => _executionPlans.GetOrAdd(mapRequest);
Expand Down Expand Up @@ -285,7 +299,7 @@ TypeMap ResolveTypeMap(TypePair typePair)
{
lock (typeMap)
{
typeMap.Seal(this, null);
typeMap.Seal(this);
}
}
}
Expand All @@ -295,7 +309,7 @@ TypeMap ResolveTypeMap(TypePair typePair)
_resolvedMaps.Add(typePair, typeMap);
if (typeMap != null && typeMap.MapExpression == null)
{
typeMap.Seal(this, _typeMapsPath);
typeMap.Seal(this);
}
}
return typeMap;
Expand Down Expand Up @@ -460,7 +474,6 @@ void IGlobalConfiguration.AssertConfigurationIsValid(string profileName)
_validator.AssertConfigurationIsValid(_configuredMaps.Values.Where(typeMap => typeMap.Profile.Name == profileName));
}
void IGlobalConfiguration.AssertConfigurationIsValid<TProfile>() => this.Internal().AssertConfigurationIsValid(typeof(TProfile).FullName);
void IGlobalConfiguration.Seal(TypeMap typeMap) => typeMap.Seal(this, _typeMapsPath);
void IGlobalConfiguration.RegisterAsMap(TypeMapConfiguration typeMapConfiguration) =>
_resolvedMaps[typeMapConfiguration.Types] = GetIncludedTypeMap(new(typeMapConfiguration.SourceType, typeMapConfiguration.DestinationTypeOverride));
}
Expand Down
52 changes: 30 additions & 22 deletions src/AutoMapper/Execution/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static class ExpressionBuilder
private static readonly ParameterExpression[] DisposableArray = new[] { Disposable };
private static readonly Expression DisposeCall = IfNullElse(Disposable, Empty, Expression.Call(Disposable, DisposeMethod));
private static readonly ParameterExpression Index = Variable(typeof(int), "sourceArrayIndex");
private static readonly ParameterExpression[] Indexes = new[] { Index };
private static readonly BinaryExpression ResetIndex = Assign(Index, Zero);
private static readonly UnaryExpression IncrementIndex = PostIncrementAssign(Index);

Expand All @@ -55,7 +56,7 @@ public static Expression MapExpression(this IGlobalConfiguration configurationPr
nullCheck = !typeMap.HasTypeConverter && allowNull.HasValue && allowNull != profileMap.AllowNullDestinationValues;
if (!typeMap.HasDerivedTypesToInclude)
{
configurationProvider.Seal(typeMap);
typeMap.Seal(configurationProvider);
if (typeMap.MapExpression != null)
{
mapExpression = typeMap.Invoke(source, destination);
Expand Down Expand Up @@ -254,39 +255,41 @@ public static bool IsMemberPath(this LambdaExpression lambda, out Stack<Member>
return currentExpression == lambda.Body;
}
public static LambdaExpression MemberAccessLambda(Type type, string memberPath) => GetMemberPath(type, memberPath).Lambda();
public static Expression ForEach(ParameterExpression loopVar, Expression collection, Expression loopContent)
public static Expression ForEach(List<ParameterExpression> variables, List<Expression> statements, ParameterExpression loopVar, Expression collection, Expression loopContent)
{
if (collection.Type.IsArray)
{
return ForEachArrayItem(loopVar, collection, loopContent);
return ForEachArrayItem(variables, statements, loopVar, collection, loopContent);
}
var getEnumeratorCall = Call(collection, "GetEnumerator");
var enumeratorVar = Variable(getEnumeratorCall.Type, "enumerator");
var breakLabel = Label("LoopBreak");
var loop = Block(new[] { enumeratorVar, loopVar },
Assign(enumeratorVar, getEnumeratorCall),
Using(enumeratorVar,
variables.Add(enumeratorVar);
variables.Add(loopVar);
statements.Add(Assign(enumeratorVar, getEnumeratorCall));
statements.Add(Using(enumeratorVar,
Loop(
IfThenElse(
Call(enumeratorVar, "MoveNext"),
Block(Assign(loopVar, ToType(Property(enumeratorVar, "Current"), loopVar.Type)), loopContent),
Break(breakLabel)
),
breakLabel)));
return loop;
static Expression ForEachArrayItem(ParameterExpression loopVar, Expression array, Expression loopContent)
return Block(variables, statements);
static Expression ForEachArrayItem(List<ParameterExpression> variables, List<Expression> statements, ParameterExpression loopVar, Expression array, Expression loopContent)
{
var breakLabel = Label("LoopBreak");
var loop = Block(new[] { Index, loopVar },
ResetIndex,
Loop(
variables.Add(Index);
variables.Add(loopVar);
statements.Add(ResetIndex);
statements.Add(Loop(
IfThenElse(
LessThan(Index, ArrayLength(array)),
Block(Assign(loopVar, ArrayAccess(array, Index)), loopContent, IncrementIndex),
Block(Assign(loopVar, ArrayAccess(array, Indexes)), loopContent, IncrementIndex),
Break(breakLabel)
),
breakLabel));
return loop;
return Block(variables, statements);
}
static Expression Using(Expression target, Expression body)
{
Expand Down Expand Up @@ -315,21 +318,26 @@ public static MethodCallExpression Call(Expression target, string name, params E
Expression.Call(target, target.Type.GetInheritedMethod(name), arguments);
public static Expression ToObject(this Expression expression) => expression.Type.IsValueType ? Convert(expression, typeof(object)) : expression;
public static Expression ToType(Expression expression, Type type) => expression.Type == type ? expression : Convert(expression, type);
public static Expression ReplaceParameters(this LambdaExpression initialLambda, params Expression[] newParameters) =>
public static Expression ReplaceParameters(this LambdaExpression initialLambda, Expression newParameter) =>
new ParameterReplaceVisitor().Replace(initialLambda, newParameter);
public static Expression ReplaceParameters(this LambdaExpression initialLambda, Expression[] newParameters) =>
new ParameterReplaceVisitor().Replace(initialLambda, newParameters);
public static Expression ConvertReplaceParameters(this LambdaExpression initialLambda, params Expression[] newParameters) =>
public static Expression ConvertReplaceParameters(this LambdaExpression initialLambda, Expression newParameter) =>
new ConvertParameterReplaceVisitor().Replace(initialLambda, newParameter);
public static Expression ConvertReplaceParameters(this LambdaExpression initialLambda, Expression[] newParameters) =>
new ConvertParameterReplaceVisitor().Replace(initialLambda, newParameters);
private static Expression Replace(this ParameterReplaceVisitor visitor, LambdaExpression initialLambda, params Expression[] newParameters)
private static Expression Replace(this ParameterReplaceVisitor visitor, LambdaExpression initialLambda, Expression newParameter) =>
visitor.Replace(initialLambda.Body, initialLambda.Parameters[0], newParameter);
private static Expression Replace(this ParameterReplaceVisitor visitor, LambdaExpression initialLambda, Expression[] newParameters)
{
var newLambda = initialLambda.Body;
for (var i = 0; i < Math.Min(newParameters.Length, initialLambda.Parameters.Count); i++)
{
visitor.Replace(initialLambda.Parameters[i], newParameters[i]);
newLambda = visitor.Visit(newLambda);
newLambda = visitor.Replace(newLambda, initialLambda.Parameters[i], newParameters[i]);
}
return newLambda;
}
public static Expression Replace(this Expression exp, Expression old, Expression replace) => new ReplaceVisitor(old, replace).Visit(exp);
public static Expression Replace(this Expression exp, Expression old, Expression replace) => new ReplaceVisitor().Replace(exp, old, replace);
public static Expression NullCheck(this Expression expression, MemberMap memberMap = null, Expression defaultValue = null)
{
var chain = expression.GetChain();
Expand Down Expand Up @@ -396,15 +404,15 @@ class ReplaceVisitorBase : ExpressionVisitor
{
protected Expression _oldNode;
protected Expression _newNode;
public virtual void Replace(Expression oldNode, Expression newNode)
public virtual Expression Replace(Expression target, Expression oldNode, Expression newNode)
{
_oldNode = oldNode;
_newNode = newNode;
return base.Visit(target);
}
}
class ReplaceVisitor : ReplaceVisitorBase
{
public ReplaceVisitor(Expression oldNode, Expression newNode) => Replace(oldNode, newNode);
public override Expression Visit(Expression node) => _oldNode == node ? _newNode : base.Visit(node);
}
class ParameterReplaceVisitor : ReplaceVisitorBase
Expand All @@ -413,7 +421,7 @@ class ParameterReplaceVisitor : ReplaceVisitorBase
}
class ConvertParameterReplaceVisitor : ParameterReplaceVisitor
{
public override void Replace(Expression oldNode, Expression newNode) => base.Replace(oldNode, ToType(newNode, oldNode.Type));
public override Expression Replace(Expression target, Expression oldNode, Expression newNode) => base.Replace(target, oldNode, ToType(newNode, oldNode.Type));
}
}
public readonly record struct Member(Expression Expression, MemberInfo MemberInfo, Expression Target);
Expand Down
Loading