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
rename configurationProvider to the shorter configuration in most places
  • Loading branch information
lbargaoanu committed Aug 17, 2022
commit dac3062028e8926d34389fc27b9c7a1490dde55c
2 changes: 1 addition & 1 deletion src/AutoMapper/Configuration/MapperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ LambdaExpression GenerateObjectMapperExpression(in MapRequest mapRequest, IObjec
if (nullCheck)
{
var profileMap = mapRequest.MemberMap.Profile ?? Configuration;
fullExpression = NullCheckSource(profileMap, source, destination, fullExpression, mapRequest.MemberMap);
fullExpression = this.NullCheckSource(profileMap, source, destination, fullExpression, mapRequest.MemberMap);
}
return Lambda(fullExpression, source, destination, ContextParameter);
}
Expand Down
66 changes: 47 additions & 19 deletions src/AutoMapper/Execution/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,33 @@ public static class ExpressionBuilder
private static readonly ParameterExpression[] Indexes = new[] { Index };
private static readonly BinaryExpression ResetIndex = Assign(Index, Zero);
private static readonly UnaryExpression IncrementIndex = PostIncrementAssign(Index);

public static Expression MapExpression(this IGlobalConfiguration configurationProvider, ProfileMap profileMap, TypePair typePair, Expression source,
public static (List<ParameterExpression> Variables, List<Expression> Expressions) ScratchPad(this IGlobalConfiguration configuration)
{
var variables = configuration.Variables;
if (variables == null)
{
variables = new();
}
else
{
variables.Clear();
}
var expressions = configuration.Expressions;
if (expressions == null)
{
expressions = new();
}
else
{
expressions.Clear();
}
return (variables, expressions);
}
public static Expression MapExpression(this IGlobalConfiguration configuration, ProfileMap profileMap, TypePair typePair, Expression source,
MemberMap memberMap = null, Expression destination = null)
{
destination ??= Default(typePair.DestinationType);
var typeMap = configurationProvider.ResolveTypeMap(typePair);
var typeMap = configuration.ResolveTypeMap(typePair);
Expression mapExpression = null;
bool nullCheck;
if (typeMap != null)
Expand All @@ -56,7 +77,7 @@ public static Expression MapExpression(this IGlobalConfiguration configurationPr
nullCheck = !typeMap.HasTypeConverter && allowNull.HasValue && allowNull != profileMap.AllowNullDestinationValues;
if (!typeMap.HasDerivedTypesToInclude)
{
typeMap.Seal(configurationProvider);
typeMap.Seal(configuration);
if (typeMap.MapExpression != null)
{
mapExpression = typeMap.Invoke(source, destination);
Expand All @@ -65,10 +86,10 @@ public static Expression MapExpression(this IGlobalConfiguration configurationPr
}
else
{
var mapper = configurationProvider.FindMapper(typePair);
var mapper = configuration.FindMapper(typePair);
if (mapper != null)
{
mapExpression = mapper.MapExpression(configurationProvider, profileMap, memberMap, source, destination);
mapExpression = mapper.MapExpression(configuration, profileMap, memberMap, source, destination);
nullCheck = mapExpression != source;
mapExpression = ToType(mapExpression, typePair.DestinationType);
}
Expand All @@ -78,9 +99,10 @@ public static Expression MapExpression(this IGlobalConfiguration configurationPr
}
}
mapExpression ??= ContextMap(typePair, source, destination, memberMap);
return nullCheck ? NullCheckSource(profileMap, source, destination, mapExpression, memberMap) : mapExpression;
return nullCheck ? configuration.NullCheckSource(profileMap, source, destination, mapExpression, memberMap) : mapExpression;
}
public static Expression NullCheckSource(ProfileMap profileMap, Expression source, Expression destination, Expression mapExpression, MemberMap memberMap)
public static Expression NullCheckSource(this IGlobalConfiguration configuration, ProfileMap profileMap, Expression source, Expression destination,
Expression mapExpression, MemberMap memberMap)
{
var sourceType = source.Type;
if (sourceType.IsValueType && !sourceType.IsNullableType())
Expand Down Expand Up @@ -120,10 +142,12 @@ Expression ClearDestinationCollection()
}
clearMethod = destinationCollectionType.GetMethod("Clear");
}
return Block(new[] { destinationVariable },
Assign(destinationVariable, destination),
Condition(ReferenceEqual(collection, Null), Empty, Expression.Call(collection, clearMethod)),
destinationVariable);
var (variables, statements) = configuration.ScratchPad();
variables.Add(destinationVariable);
statements.Add(Assign(destinationVariable, destination));
statements.Add(Condition(ReferenceEqual(collection, Null), Empty, Expression.Call(collection, clearMethod)));
statements.Add(destinationVariable);
return Block(variables, statements);
}
Expression DefaultDestination()
{
Expand Down Expand Up @@ -264,17 +288,19 @@ public static Expression ForEach(List<ParameterExpression> variables, List<Expre
var getEnumeratorCall = Call(collection, "GetEnumerator");
var enumeratorVar = Variable(getEnumeratorCall.Type, "enumerator");
var breakLabel = Label("LoopBreak");
variables.Add(enumeratorVar);
variables.Add(loopVar);
statements.Add(Assign(enumeratorVar, getEnumeratorCall));
statements.Add(Using(enumeratorVar,
var usingEnumerator = Using(statements, enumeratorVar,
Loop(
IfThenElse(
Call(enumeratorVar, "MoveNext"),
Block(Assign(loopVar, ToType(Property(enumeratorVar, "Current"), loopVar.Type)), loopContent),
Break(breakLabel)
),
breakLabel)));
breakLabel));
statements.Clear();
variables.Add(enumeratorVar);
variables.Add(loopVar);
statements.Add(Assign(enumeratorVar, getEnumeratorCall));
statements.Add(usingEnumerator);
return Block(variables, statements);
static Expression ForEachArrayItem(List<ParameterExpression> variables, List<Expression> statements, ParameterExpression loopVar, Expression array, Expression loopContent)
{
Expand All @@ -291,7 +317,7 @@ static Expression ForEachArrayItem(List<ParameterExpression> variables, List<Exp
breakLabel));
return Block(variables, statements);
}
static Expression Using(Expression target, Expression body)
static Expression Using(List<Expression> statements, Expression target, Expression body)
{
Expression finallyDispose;
if (typeof(IDisposable).IsAssignableFrom(target.Type))
Expand All @@ -305,7 +331,9 @@ static Expression Using(Expression target, Expression body)
return body;
}
var assignDisposable = Assign(Disposable, TypeAs(target, typeof(IDisposable)));
finallyDispose = Block(DisposableArray, assignDisposable, DisposeCall);
statements.Add(assignDisposable);
statements.Add(DisposeCall);
finallyDispose = Block(DisposableArray, statements);
}
return TryFinally(body, finallyDispose);
}
Expand Down
45 changes: 24 additions & 21 deletions src/AutoMapper/Execution/TypeMapPlanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ namespace AutoMapper.Execution
public struct TypeMapPlanBuilder
{
private static readonly MethodInfo MappingError = typeof(TypeMapPlanBuilder).GetStaticMethod(nameof(MemberMappingError));
private readonly IGlobalConfiguration _configurationProvider;
private readonly IGlobalConfiguration _configuration;
private readonly ParameterExpression _destination;
private readonly ParameterExpression _initialDestination;
private readonly TypeMap _typeMap;
private List<ParameterExpression> _variables;
private List<Expression> _expressions;
private CatchBlock[] _catches;
public TypeMapPlanBuilder(IGlobalConfiguration configurationProvider, TypeMap typeMap)
public TypeMapPlanBuilder(IGlobalConfiguration configuration, TypeMap typeMap)
{
_configurationProvider = configurationProvider;
_configuration = configuration;
_typeMap = typeMap;
Source = Parameter(typeMap.SourceType, "source");
_initialDestination = Parameter(typeMap.DestinationType, "destination");
_destination = Variable(_initialDestination.Type, "typeMapDestination");
_variables = configurationProvider.Variables;
_expressions = configurationProvider.Expressions;
_catches = configurationProvider.Catches;
_variables = configuration.Variables;
_expressions = configuration.Expressions;
_catches = configuration.Catches;
}
public Type DestinationType => _destination.Type;
public ParameterExpression Source { get; }
private static AutoMapperMappingException MemberMappingError(Exception innerException, MemberMap memberMap) => new("Error mapping types.", innerException, memberMap);
ParameterExpression[] GetParameters(ParameterExpression source = null, ParameterExpression destination = null)
{
var parameters = _configurationProvider.Parameters ?? new ParameterExpression[] { null, null, ContextParameter };
var parameters = _configuration.Parameters ?? new ParameterExpression[] { null, null, ContextParameter };
parameters[0] = source ?? Source;
parameters[1] = destination ?? _destination;
return parameters;
Expand All @@ -52,9 +52,9 @@ public LambdaExpression CreateMapperLambda()
_variables ??= new();
_expressions ??= new();
_catches ??= new CatchBlock[1];
var typeMapsPath = _configurationProvider.TypeMapsPath;
var typeMapsPath = _configuration.TypeMapsPath;
Clear(ref typeMapsPath);
CheckForCycles(_configurationProvider, _typeMap, typeMapsPath);
CheckForCycles(_configuration, _typeMap, typeMapsPath);
var createDestinationFunc = CreateDestinationFunc();
var assignmentFunc = CreateAssignmentFunc(createDestinationFunc);
var mapperFunc = CreateMapperFunc(assignmentFunc);
Expand Down Expand Up @@ -91,7 +91,7 @@ void IncludeMembers()
_expressions.AddRange(_variables.Zip(_typeMap.IncludedMembersTypeMaps, (v, i) =>
Assign(v, i.MemberExpression.ReplaceParameters(source).NullCheck())));
}
private static void CheckForCycles(IGlobalConfiguration configurationProvider, TypeMap typeMap, HashSet<TypeMap> typeMapsPath)
private static void CheckForCycles(IGlobalConfiguration configuration, TypeMap typeMap, HashSet<TypeMap> typeMapsPath)
{
typeMapsPath.Add(typeMap);
foreach (var memberMap in MemberMaps())
Expand All @@ -101,7 +101,7 @@ private static void CheckForCycles(IGlobalConfiguration configurationProvider, T
{
continue;
}
if (memberMap.Inline && (memberTypeMap.PreserveReferences || typeMapsPath.Count == configurationProvider.MaxExecutionPlanDepth))
if (memberMap.Inline && (memberTypeMap.PreserveReferences || typeMapsPath.Count == configuration.MaxExecutionPlanDepth))
{
memberMap.Inline = false;
TraceInline(typeMap, memberMap);
Expand All @@ -127,21 +127,21 @@ private static void CheckForCycles(IGlobalConfiguration configurationProvider, T
memberMap.Inline = false;
TraceInline(typeMap, memberMap);
}
foreach (var derivedTypeMap in configurationProvider.GetIncludedTypeMaps(memberTypeMap))
foreach (var derivedTypeMap in configuration.GetIncludedTypeMaps(memberTypeMap))
{
derivedTypeMap.PreserveReferences = true;
Trace(typeMap, derivedTypeMap, memberMap);
}
}
CheckForCycles(configurationProvider, memberTypeMap, typeMapsPath);
CheckForCycles(configuration, memberTypeMap, typeMapsPath);
}
typeMapsPath.Remove(typeMap);
return;
IEnumerable<MemberMap> MemberMaps()
{
var memberMaps = typeMap.MemberMaps;
return typeMap.HasDerivedTypesToInclude ?
memberMaps.Concat(configurationProvider.GetIncludedTypeMaps(typeMap).SelectMany(tm => tm.MemberMaps)) :
memberMaps.Concat(configuration.GetIncludedTypeMaps(typeMap).SelectMany(tm => tm.MemberMaps)) :
memberMaps;
}
TypeMap ResolveMemberTypeMap(MemberMap memberMap)
Expand All @@ -151,7 +151,7 @@ TypeMap ResolveMemberTypeMap(MemberMap memberMap)
return null;
}
var types = memberMap.Types();
return types.ContainsGenericParameters ? null : configurationProvider.ResolveAssociatedTypeMap(types);
return types.ContainsGenericParameters ? null : configuration.ResolveAssociatedTypeMap(types);
}
[Conditional("DEBUG")]
static void Trace(TypeMap typeMap, TypeMap memberTypeMap, MemberMap memberMap) =>
Expand Down Expand Up @@ -247,7 +247,7 @@ private Expression CreateMapperFunc(Expression assignmentFunc)
{
mapperFunc = Condition(overMaxDepth, Default(DestinationType), mapperFunc);
}
mapperFunc = NullCheckSource(_typeMap.Profile, Source, _initialDestination, mapperFunc, null);
mapperFunc = _configuration.NullCheckSource(_typeMap.Profile, Source, _initialDestination, mapperFunc, null);
return CheckReferencesCache(mapperFunc);
}
private Expression CheckReferencesCache(Expression valueBuilder)
Expand Down Expand Up @@ -280,10 +280,13 @@ private Expression CreateConstructorParameterExpression(ConstructorParameterMap
var customSource = GetCustomSource(ctorParamMap);
var resolvedExpression = BuildValueResolverFunc(ctorParamMap, customSource, defaultValue);
var resolvedValue = Variable(resolvedExpression.Type, "resolvedValue");
var tryMap = Block(new[] { resolvedValue },
Assign(resolvedValue, resolvedExpression),
MapMember(ctorParamMap, defaultValue, resolvedValue));
return TryMemberMap(ctorParamMap, tryMap);
var mapMember = MapMember(ctorParamMap, defaultValue, resolvedValue);
_variables.Clear();
_variables.Add(resolvedValue);
_expressions.Clear();
_expressions.Add(Assign(resolvedValue, resolvedExpression));
_expressions.Add(mapMember);
return TryMemberMap(ctorParamMap, Block(_variables, _expressions));
}
private Expression TryPropertyMap(PropertyMap propertyMap)
{
Expand Down Expand Up @@ -375,7 +378,7 @@ Expression MapMember(MemberMap memberMap, Expression destinationMemberValue, Par
{
var typePair = memberMap.Types();
var mapMember = memberMap.Inline ?
_configurationProvider.MapExpression(_typeMap.Profile, typePair, resolvedValue, memberMap, destinationMemberValue) :
_configuration.MapExpression(_typeMap.Profile, typePair, resolvedValue, memberMap, destinationMemberValue) :
ContextMap(typePair, resolvedValue, destinationMemberValue, memberMap);
return memberMap.ApplyTransformers(mapMember);
}
Expand Down
8 changes: 4 additions & 4 deletions src/AutoMapper/Features.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace AutoMapper.Features
{
public interface IGlobalFeature
{
void Configure(IGlobalConfiguration configurationProvider);
void Configure(IGlobalConfiguration configuration);
}
public interface IMappingFeature
{
Expand All @@ -16,7 +16,7 @@ public interface IMappingFeature
}
public interface IRuntimeFeature
{
void Seal(IGlobalConfiguration configurationProvider);
void Seal(IGlobalConfiguration configuration);
}
public class Features<TFeature> : IReadOnlyCollection<TFeature>
{
Expand Down Expand Up @@ -91,15 +91,15 @@ internal static void Configure(this Features<IMappingFeature> features, TypeMap
feature.Configure(typeMap);
}
}
internal static void Seal(this Features<IRuntimeFeature> features, IGlobalConfiguration configurationProvider)
internal static void Seal(this Features<IRuntimeFeature> features, IGlobalConfiguration configuration)
{
if (features.Count == 0)
{
return;
}
foreach (var feature in features)
{
feature.Seal(configurationProvider);
feature.Seal(configuration);
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions src/AutoMapper/Internal/PrimitiveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ namespace AutoMapper.Internal
[EditorBrowsable(EditorBrowsableState.Never)]
public static class PrimitiveHelper
{
public static void Clear<T>(ref T collection) where T : IList, new()
{
if (collection == null)
{
collection = new();
}
else
{
collection.Clear();
}
}
public static IReadOnlyCollection<T> NullCheck<T>(this IReadOnlyCollection<T> source) => source ?? Array.Empty<T>();
public static IEnumerable<T> Concat<T>(this IReadOnlyCollection<T> collection, IReadOnlyCollection<T> otherCollection)
{
Expand Down
Loading