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 18, 2022
commit ebd2c434a71dd424e979dd64a25b5d168d8d076d
19 changes: 18 additions & 1 deletion src/AutoMapper/Configuration/MapperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 @@ -68,6 +67,7 @@ public class MapperConfiguration : IGlobalConfiguration
private readonly ParameterExpression[] _parameters = new ParameterExpression[] { null, null, ContextParameter };
private readonly CatchBlock[] _catches = new CatchBlock[1];
private readonly List<Expression> _expressions = new();
private readonly Dictionary<Type, DefaultExpression> _defaults;
public MapperConfiguration(MapperConfigurationExpression configurationExpression)
{
var configuration = (IGlobalConfigurationExpression)configurationExpression;
Expand Down Expand Up @@ -98,6 +98,7 @@ public MapperConfiguration(MapperConfigurationExpression configurationExpression
typeMapsCount += profileMap.TypeMapsCount;
openTypeMapsCount += profileMap.OpenTypeMapsCount;
}
_defaults = new(3 * typeMapsCount);
_configuredMaps = new(typeMapsCount);
_hasOpenMaps = openTypeMapsCount > 0;
_runtimeMaps = new(GetTypeMap, openTypeMapsCount);
Expand All @@ -110,12 +111,15 @@ public MapperConfiguration(MapperConfigurationExpression configurationExpression
{
profile.Clear();
}
_configuredMaps.TrimExcess();
_resolvedMaps.TrimExcess();
_typeMapsPath = null;
_sourceMembers = null;
_expressions = null;
_variables = null;
_parameters = null;
_catches = null;
_defaults = null;
_sealed = true;
return;
void Seal()
Expand Down Expand Up @@ -259,6 +263,19 @@ static MapperConfigurationExpression Build(Action<IMapperConfigurationExpression
HashSet<TypeMap> IGlobalConfiguration.TypeMapsPath => _typeMapsPath;
ParameterExpression[] IGlobalConfiguration.Parameters => _parameters;
CatchBlock[] IGlobalConfiguration.Catches => _catches;
DefaultExpression IGlobalConfiguration.GetDefault(Type type)
{
if (_defaults == null)
{
return Default(type);
}
if (!_defaults.TryGetValue(type, out var defaultExpression))
{
defaultExpression = Default(type);
_defaults.Add(type, defaultExpression);
}
return defaultExpression;
}
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
17 changes: 15 additions & 2 deletions src/AutoMapper/Configuration/TypeMapConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,21 @@ protected void IncludeBaseCore(Type sourceBase, Type destinationBase)
_types.CheckIsDerivedFrom(baseTypes);
TypeMapActions.Add(tm => tm.IncludeBaseTypes(baseTypes));
}
public IPropertyMapConfiguration GetDestinationMemberConfiguration(MemberInfo destinationMember) =>
_memberConfigurations?.Find(m => m.DestinationMember == destinationMember);
public IPropertyMapConfiguration GetDestinationMemberConfiguration(MemberInfo destinationMember)
{
if (_memberConfigurations == null)
{
return null;
}
foreach (var config in _memberConfigurations)
{
if (config.DestinationMember == destinationMember)
{
return config;
}
}
return null;
}
protected abstract void IgnoreDestinationMember(MemberInfo property, bool ignorePaths = true);
}
public abstract class MappingExpressionBase<TSource, TDestination, TMappingExpression> : TypeMapConfiguration, IMappingExpressionBase<TSource, TDestination, TMappingExpression>
Expand Down
3 changes: 2 additions & 1 deletion src/AutoMapper/ConstructorMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
namespace AutoMapper
{
using Execution;
[EditorBrowsable(EditorBrowsableState.Never)]
public class ConstructorMap
{
Expand Down Expand Up @@ -96,7 +97,7 @@ public ConstructorParameterMap(ConstructorParameterMap parameterMap, IncludedMem
public override IncludedMember IncludedMember { get; }
public override MemberInfo[] SourceMembers { get; set; }
public override string DestinationName => Parameter.Name;
public Expression DefaultValue() => Parameter.IsOptional ? Parameter.GetDefaultValue() : Expression.Default(DestinationType);
public Expression DefaultValue(IGlobalConfiguration configuration) => Parameter.IsOptional ? Parameter.GetDefaultValue(configuration) : configuration.Default(DestinationType);
public override string ToString() => Parameter.Member.DeclaringType + "." + Parameter.Member + ".parameter " + Parameter.Name;
}
}
14 changes: 8 additions & 6 deletions src/AutoMapper/Execution/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ 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 DefaultExpression Default(this IGlobalConfiguration configuration, Type type) =>
configuration == null ? Expression.Default(type) : configuration.GetDefault(type);
public static (List<ParameterExpression> Variables, List<Expression> Expressions) ScratchPad(this IGlobalConfiguration configuration)
{
var variables = configuration.Variables;
var variables = configuration?.Variables;
if (variables == null)
{
variables = new();
Expand All @@ -53,7 +55,7 @@ public static (List<ParameterExpression> Variables, List<Expression> Expressions
{
variables.Clear();
}
var expressions = configuration.Expressions;
var expressions = configuration?.Expressions;
if (expressions == null)
{
expressions = new();
Expand All @@ -67,7 +69,7 @@ public static (List<ParameterExpression> Variables, List<Expression> Expressions
public static Expression MapExpression(this IGlobalConfiguration configuration, ProfileMap profileMap, TypePair typePair, Expression source,
MemberMap memberMap = null, Expression destination = null)
{
destination ??= Default(typePair.DestinationType);
destination ??= configuration.Default(typePair.DestinationType);
var typeMap = configuration.ResolveTypeMap(typePair);
Expression mapExpression = null;
bool nullCheck;
Expand Down Expand Up @@ -153,7 +155,7 @@ Expression DefaultDestination()
{
if ((isCollection && profileMap.AllowsNullCollectionsFor(memberMap)) || (!isCollection && profileMap.AllowsNullDestinationValuesFor(memberMap)))
{
return destination.NodeType == ExpressionType.Default ? destination : Default(destinationType);
return destination.NodeType == ExpressionType.Default ? destination : configuration.Default(destinationType);
}
if (destinationType.IsArray)
{
Expand All @@ -163,7 +165,7 @@ Expression DefaultDestination()
Expression.Call(ArrayEmptyMethod.MakeGenericMethod(destinationElementType)) :
NewArrayBounds(destinationElementType, Enumerable.Repeat(Zero, rank));
}
return ObjectFactory.GenerateConstructorExpression(destinationType);
return ObjectFactory.GenerateConstructorExpression(destinationType, configuration);
}
}
public static Expression ServiceLocator(Type type) => Expression.Call(ContextParameter, ContextCreate, Constant(type));
Expand Down Expand Up @@ -378,7 +380,7 @@ public static Expression NullCheck(this Expression expression, IGlobalConfigurat
var destinationType = memberMap?.DestinationType;
var returnType = (destinationType != null && destinationType != expression.Type && Nullable.GetUnderlyingType(destinationType) == expression.Type) ?
destinationType : expression.Type;
var defaultReturn = (defaultValue is { NodeType: ExpressionType.Default } && defaultValue.Type == returnType) ? defaultValue : Default(returnType);
var defaultReturn = (defaultValue is { NodeType: ExpressionType.Default } && defaultValue.Type == returnType) ? defaultValue : configuration.Default(returnType);
List<ParameterExpression> variables = null;
List<Expression> expressions = null;
var name = parameter.Name;
Expand Down
12 changes: 6 additions & 6 deletions src/AutoMapper/Execution/ObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ public static class ObjectFactory
private static readonly LockingConcurrentDictionary<Type, Func<object>> CtorCache = new(GenerateConstructor);
public static object CreateInstance(Type type) => CtorCache.GetOrAdd(type)();
private static Func<object> GenerateConstructor(Type type) =>
Lambda<Func<object>>(GenerateConstructorExpression(type).ToObject()).Compile();
Lambda<Func<object>>(GenerateConstructorExpression(type, null).ToObject()).Compile();
public static object CreateInterfaceProxy(Type interfaceType) => CreateInstance(ProxyGenerator.GetProxyType(interfaceType));
public static Expression GenerateConstructorExpression(Type type) => type switch
public static Expression GenerateConstructorExpression(Type type, IGlobalConfiguration configuration) => type switch
{
{ IsValueType: true } => Default(type),
{ IsValueType: true } => configuration.Default(type),
Type stringType when stringType == typeof(string) => Constant(string.Empty),
{ IsInterface: true } => CreateInterfaceExpression(type),
{ IsAbstract: true } => InvalidType(type, $"Cannot create an instance of abstract type {type}."),
_ => CallConstructor(type)
_ => CallConstructor(type, configuration)
};
private static Expression CallConstructor(Type type)
private static Expression CallConstructor(Type type, IGlobalConfiguration configuration)
{
var defaultCtor = type.GetConstructor(TypeExtensions.InstanceFlags, null, Type.EmptyTypes, null);
if (defaultCtor != null)
Expand All @@ -38,7 +38,7 @@ private static Expression CallConstructor(Type type)
{
return InvalidType(type, $"{type} needs to have a constructor with 0 args or only optional args. Validate your configuration for details.");
}
var arguments = ctorWithOptionalArgs.args.Select(p => p.GetDefaultValue());
var arguments = ctorWithOptionalArgs.args.Select(p => p.GetDefaultValue(configuration));
return New(ctorWithOptionalArgs.ctor, arguments);
}
private static Expression CreateInterfaceExpression(Type type) =>
Expand Down
Loading