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
reuse the execution plan between user Map calls and internal Map calls
  • Loading branch information
lbargaoanu committed Aug 2, 2022
commit e3aecd30a24c09b0549553a7f32a7db608214c3f
6 changes: 3 additions & 3 deletions src/AutoMapper/Configuration/MapperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ public MapperConfiguration(Action<IMapperConfigurationExpression> configure)
public IMapper CreateMapper(Func<Type, object> serviceCtor) => new Mapper(this, serviceCtor);
public void CompileMappings()
{
foreach (var request in _resolvedMaps.Keys.Where(t => !t.IsGenericTypeDefinition).Select(types => new MapRequest(types, types)).ToArray())
foreach (var request in _resolvedMaps.Keys.Where(t => !t.IsGenericTypeDefinition).Select(types => new MapRequest(types, types, MemberMap.Instance)).ToArray())
{
GetExecutionPlan(request);
}
}
public LambdaExpression BuildExecutionPlan(Type sourceType, Type destinationType)
{
var typePair = new TypePair(sourceType, destinationType);
return this.Internal().BuildExecutionPlan(new(typePair, typePair));
return this.Internal().BuildExecutionPlan(new(typePair, typePair, MemberMap.Instance));
}
LambdaExpression IGlobalConfiguration.BuildExecutionPlan(in MapRequest mapRequest)
{
Expand Down Expand Up @@ -237,7 +237,7 @@ LambdaExpression GenerateObjectMapperExpression(in MapRequest mapRequest, IObjec
var throwExpression = Throw(newException, runtimeDestinationType);
fullExpression = TryCatch(ToType(map, runtimeDestinationType), Catch(ExceptionParameter, throwExpression));
}
var profileMap = mapRequest.MemberMap?.Profile ?? Configuration;
var profileMap = mapRequest.MemberMap.Profile ?? Configuration;
var nullCheckSource = NullCheckSource(profileMap, source, destination, fullExpression, mapRequest.MemberMap);
return Lambda(nullCheckSource, source, destination, ContextParameter);
}
Expand Down
18 changes: 4 additions & 14 deletions src/AutoMapper/Execution/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class ExpressionBuilder
public static readonly MethodInfo OverTypeDepthMethod = typeof(ResolutionContext).GetInstanceMethod(nameof(ResolutionContext.OverTypeDepth));
public static readonly MethodInfo CacheDestinationMethod = typeof(ResolutionContext).GetInstanceMethod(nameof(ResolutionContext.CacheDestination));
public static readonly MethodInfo GetDestinationMethod = typeof(ResolutionContext).GetInstanceMethod(nameof(ResolutionContext.GetDestination));
private static readonly MethodCallExpression CheckContextCall = Expression.Call(
public static readonly MethodCallExpression CheckContextCall = Expression.Call(
typeof(ResolutionContext).GetStaticMethod(nameof(ResolutionContext.CheckContext)), ContextParameter);
private static readonly MethodInfo ContextMapMethod = typeof(ResolutionContext).GetInstanceMethod(nameof(ResolutionContext.MapInternal));
private static readonly MethodInfo ArrayEmptyMethod = typeof(Array).GetStaticMethod(nameof(Array.Empty));
Expand Down Expand Up @@ -81,9 +81,7 @@ public static Expression NullCheckSource(ProfileMap profileMap, Expression sourc
var destinationType = destinationParameter.Type;
var isCollection = destinationType.IsCollection();
var mustUseDestination = memberMap is { MustUseDestination: true };
var ifSourceNull = memberMap == null ?
destinationParameter.IfNullElse(DefaultDestination(), ClearDestinationCollection()) :
mustUseDestination ? ClearDestinationCollection() : DefaultDestination();
var ifSourceNull = mustUseDestination ? ClearDestinationCollection() : DefaultDestination();
return sourceParameter.IfNullElse(ifSourceNull, mapExpression);
Expression ClearDestinationCollection()
{
Expand All @@ -109,13 +107,13 @@ Expression ClearDestinationCollection()
}
var destinationElementType = GetEnumerableElementType(destinationType);
destinationCollectionType = typeof(ICollection<>).MakeGenericType(destinationElementType);
collection = Convert(collection, destinationCollectionType);
collection = TypeAs(collection, destinationCollectionType);
}
clearMethod = destinationCollectionType.GetMethod("Clear");
}
return Block(new[] { destinationVariable },
Assign(destinationVariable, destinationParameter),
Condition(ReferenceEqual(destinationVariable, Null), Empty, Expression.Call(collection, clearMethod)),
Condition(ReferenceEqual(collection, Null), Empty, Expression.Call(collection, clearMethod)),
destinationVariable);
}
Expression DefaultDestination()
Expand All @@ -141,14 +139,6 @@ public static Expression ContextMap(TypePair typePair, Expression sourceParamete
var mapMethod = ContextMapMethod.MakeGenericMethod(typePair.SourceType, typePair.DestinationType);
return Expression.Call(ContextParameter, mapMethod, sourceParameter, destinationParameter, Constant(memberMap, typeof(MemberMap)));
}
public static Expression CheckContext(TypeMap typeMap)
{
if (typeMap.MaxDepth > 0 || typeMap.PreserveReferences)
{
return CheckContextCall;
}
return null;
}
public static Expression OverMaxDepth(TypeMap typeMap) => typeMap?.MaxDepth > 0 ? Expression.Call(ContextParameter, OverTypeDepthMethod, Constant(typeMap)) : null;
public static Expression NullSubstitute(this MemberMap memberMap, Expression sourceExpression) =>
Coalesce(sourceExpression, ToType(Constant(memberMap.NullSubstitute), sourceExpression.Type));
Expand Down
5 changes: 2 additions & 3 deletions src/AutoMapper/Execution/TypeMapPlanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ public LambdaExpression CreateMapperLambda(HashSet<TypeMap> typeMapsPath)
var createDestinationFunc = CreateDestinationFunc();
var assignmentFunc = CreateAssignmentFunc(createDestinationFunc);
var mapperFunc = CreateMapperFunc(assignmentFunc);
var checkContext = CheckContext(_typeMap);
if (checkContext != null)
if (_typeMap.MaxDepth > 0 || _typeMap.PreserveReferences)
{
statements.Add(checkContext);
statements.Add(CheckContextCall);
}
statements.Add(mapperFunc);
variables.Add(_destination);
Expand Down
6 changes: 3 additions & 3 deletions src/AutoMapper/Internal/TypePair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace AutoMapper.Internal
public readonly TypePair RequestedTypes;
public readonly TypePair RuntimeTypes;
public readonly MemberMap MemberMap;
public MapRequest(TypePair requestedTypes, TypePair runtimeTypes, MemberMap memberMap = null)
public MapRequest(TypePair requestedTypes, TypePair runtimeTypes, MemberMap memberMap)
{
RequestedTypes = requestedTypes;
RuntimeTypes = runtimeTypes;
MemberMap = memberMap;
}
public bool Equals(MapRequest other) => RequestedTypes.Equals(other.RequestedTypes) && RuntimeTypes.Equals(other.RuntimeTypes) &&
(MemberMap == other.MemberMap || (MemberMap?.MapperEquals(other.MemberMap)).GetValueOrDefault());
(MemberMap == other.MemberMap || MemberMap.MapperEquals(other.MemberMap));
public override bool Equals(object obj) => obj is MapRequest other && Equals(other);
public override int GetHashCode() => HashCode.Combine(RequestedTypes, RuntimeTypes, MemberMap?.MapperGetHashCode());
public override int GetHashCode() => HashCode.Combine(RequestedTypes, RuntimeTypes, MemberMap.MapperGetHashCode());
public static bool operator ==(in MapRequest left, in MapRequest right) => left.Equals(right);
public static bool operator !=(in MapRequest left, in MapRequest right) => !left.Equals(right);
}
Expand Down
1 change: 1 addition & 0 deletions src/AutoMapper/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private TDestination MapCore<TSource, TDestination>(
{
TypePair requestedTypes = new(typeof(TSource), typeof(TDestination));
TypePair runtimeTypes = new(source?.GetType() ?? sourceType ?? typeof(TSource), destination?.GetType() ?? destinationType ?? typeof(TDestination));
memberMap ??= destination == null ? MemberMap.Instance : MemberMap.InstanceUseDestination;
MapRequest mapRequest = new(requestedTypes, runtimeTypes, memberMap);
return _configurationProvider.GetExecutionPlan<TSource, TDestination>(mapRequest)(source, destination, context);
}
Expand Down
21 changes: 1 addition & 20 deletions src/AutoMapper/Mappers/CollectionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,12 @@ Expression MapCollectionCore(Expression destExpression)
addItems = Condition(overMaxDepth, ExpressionBuilder.Empty, addItems);
}
var clearMethod = isIList ? IListClear : destinationCollectionType.GetMethod("Clear");
var checkNull = Block(new[] { newExpression, passedDestination },
return Block(new[] { newExpression, passedDestination },
Assign(passedDestination, destExpression),
assignNewExpression,
Call(destination, clearMethod),
addItems,
destination);
if (memberMap != null)
{
return checkNull;
}
return CheckContext();
void GetDestinationType()
{
var immutableCollection = !mustUseDestination && destinationType.IsValueType;
Expand Down Expand Up @@ -133,20 +128,6 @@ void UseDestinationValue()
assignNewExpression = Assign(newExpression, Coalesce(passedDestination, ObjectFactory.GenerateConstructorExpression(passedDestination.Type)));
}
}
Expression CheckContext()
{
var elementTypeMap = configurationProvider.ResolveTypeMap(sourceElementType, destinationElementType);
if (elementTypeMap == null)
{
return checkNull;
}
var checkContext = ExpressionBuilder.CheckContext(elementTypeMap);
if (checkContext == null)
{
return checkNull;
}
return Block(checkContext, checkNull);
}
}
}
private static Expression CreateNameValueCollection(Expression sourceExpression) =>
Expand Down
12 changes: 3 additions & 9 deletions src/AutoMapper/MemberMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class MemberMap : IValueResolver
{
private static readonly LambdaExpression EmptyLambda = Lambda(ExpressionBuilder.Null);
protected MemberMap(TypeMap typeMap = null) => TypeMap = typeMap;
public static readonly MemberMap Instance = new();
internal static readonly MemberMap Instance = new();
internal static readonly MemberMap InstanceUseDestination = new PropertyMap(default(MemberInfo), null, null) { UseDestinationValue = true };
public TypeMap TypeMap { get; protected set; }
public LambdaExpression CustomMapExpression => Resolver?.ProjectToExpression;
public bool IsResolveConfigured => Resolver != null && Resolver != this;
Expand Down Expand Up @@ -64,15 +65,8 @@ public Expression ChainSourceMembers(Expression source, Type destinationType, Ex
public bool AllowsNullCollections => (Profile?.AllowsNullCollectionsFor(this)).GetValueOrDefault();
public ProfileMap Profile => TypeMap?.Profile;
private int MaxDepth => (TypeMap?.MaxDepth).GetValueOrDefault();
public bool MapperEquals(MemberMap other)
{
if (other == null)
{
return false;
}
return other.MustUseDestination == MustUseDestination && other.MaxDepth == MaxDepth &&
public bool MapperEquals(MemberMap other) => other.MustUseDestination == MustUseDestination && other.MaxDepth == MaxDepth &&
other.AllowsNullDestinationValues == AllowsNullDestinationValues && other.AllowsNullCollections == AllowsNullCollections;
}
public int MapperGetHashCode() => HashCode.Combine(MustUseDestination, MaxDepth, AllowsNullDestinationValues, AllowsNullCollections);
protected Type GetSourceType() => Resolver?.ResolvedType ?? DestinationType;
public void MapByConvention(MemberInfo[] sourceMembers)
Expand Down