Skip to content

Commit 91e6678

Browse files
committed
Using statically-cached root mapper keys where possible / Lazy-loading MappingRuleSet name constant Expression
1 parent 4f911fc commit 91e6678

File tree

6 files changed

+76
-14
lines changed

6 files changed

+76
-14
lines changed

AgileMapper/MappingExecutor.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Api;
99
using Api.Configuration;
1010
using Extensions.Internal;
11+
using Members;
1112
using ObjectPopulation;
1213

1314
internal class MappingExecutor<TSource> :
@@ -126,19 +127,19 @@ private TTarget PerformMapping<TTarget>(
126127

127128
private TTarget PerformMapping<TTarget>(TTarget target)
128129
{
129-
if (TypeInfo<TSource>.RuntimeTypeNeeded || TypeInfo<TTarget>.RuntimeTypeNeeded)
130+
if (MappingTypes<TSource, TTarget>.SkipTypesCheck)
130131
{
131-
var rootMappingData = ObjectMappingDataFactory.ForRoot(_source, target, this);
132-
var result = rootMappingData.MapStart();
132+
// Optimise for the most common scenario:
133+
var typedRootMappingData = ObjectMappingDataFactory
134+
.ForRootFixedTypes(_source, target, this);
133135

134-
return (TTarget)result;
136+
return typedRootMappingData.MapStart();
135137
}
136138

137-
// Optimise for the most common scenario:
138-
var typedRootMappingData = ObjectMappingDataFactory
139-
.ForRootFixedTypes(_source, target, this);
139+
var rootMappingData = ObjectMappingDataFactory.ForRoot(_source, target, this);
140+
var result = rootMappingData.MapStart();
140141

141-
return typedRootMappingData.MapStart();
142+
return (TTarget)result;
142143
}
143144

144145
#region IFlatteningSelector Members

AgileMapper/MappingRuleSet.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace AgileObjects.AgileMapper
99

1010
internal class MappingRuleSet
1111
{
12+
private Expression _nameConstant;
13+
1214
public MappingRuleSet(
1315
string name,
1416
MappingRuleSetSettings settings,
@@ -18,7 +20,6 @@ public MappingRuleSet(
1820
IDataSourceFactory fallbackDataSourceFactory)
1921
{
2022
Name = name;
21-
NameConstant = name.ToConstantExpression();
2223
Settings = settings;
2324
EnumerablePopulationStrategy = enumerablePopulationStrategy;
2425
RecursiveMemberMappingStrategy = recursiveMemberMappingStrategy;
@@ -28,7 +29,7 @@ public MappingRuleSet(
2829

2930
public string Name { get; }
3031

31-
public Expression NameConstant { get; }
32+
public Expression NameConstant => _nameConstant ?? (_nameConstant = Name.ToConstantExpression());
3233

3334
public MappingRuleSetSettings Settings { get; }
3435

AgileMapper/ObjectPopulation/ObjectMapperFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public ObjectMapper<TSource, TTarget> GetOrCreateRoot<TSource, TTarget>(ObjectMa
4949
return mapper;
5050
}
5151

52-
mappingData.MapperKey.MappingData = mappingData;
52+
mappingData.GenerateUniqueRootKey();
5353

5454
mapper = (ObjectMapper<TSource, TTarget>)_rootMappersCache.GetOrAdd(
5555
mappingData.MapperKey,

AgileMapper/ObjectPopulation/ObjectMappingData.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ private ObjectMappingData(
6767

6868
public MapperContext MapperContext => MappingContext.MapperContext;
6969

70-
public ObjectMapperKeyBase MapperKey { get; }
70+
public ObjectMapperKeyBase MapperKey { get; private set; }
71+
72+
public void GenerateUniqueRootKey()
73+
{
74+
MapperKey = new RootObjectMapperKey(MapperKey.MappingTypes, MappingContext)
75+
{
76+
MappingData = this
77+
};
78+
}
7179

7280
public IObjectMapper GetOrCreateMapper()
7381
{

AgileMapper/ObjectPopulation/ObjectMappingDataFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static ObjectMappingData<TSource, TTarget> ForRootFixedTypes<TSource, TTa
4545
return ForRootFixedTypes(
4646
source,
4747
target,
48-
new RootObjectMapperKey(MappingTypes<TSource, TTarget>.Fixed, mappingContext),
48+
RootObjectMapperKey.Cache<TSource, TTarget>.GetKeyFor(mappingContext),
4949
mappingContext);
5050
}
5151

AgileMapper/ObjectPopulation/RootObjectMapperKey.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public RootObjectMapperKey(MappingTypes mappingTypes, IMappingContext mappingCon
1515
{
1616
}
1717

18-
private RootObjectMapperKey(MappingRuleSet ruleSet, MappingTypes mappingTypes, MapperContext mapperContext)
18+
public RootObjectMapperKey(MappingRuleSet ruleSet, MappingTypes mappingTypes, MapperContext mapperContext)
1919
: base(mappingTypes)
2020
{
2121
_mapperContext = mapperContext;
@@ -57,5 +57,57 @@ public override string ToString()
5757
}
5858
#endif
5959
#endregion
60+
61+
public static class Cache<TSource, TTarget>
62+
{
63+
// ReSharper disable StaticMemberInGenericType
64+
private static ObjectMapperKeyBase _createNew;
65+
private static ObjectMapperKeyBase _overwrite;
66+
private static ObjectMapperKeyBase _project;
67+
private static ObjectMapperKeyBase _merge;
68+
// ReSharper restore StaticMemberInGenericType
69+
70+
private static ObjectMapperKeyBase CreateNew
71+
=> _createNew ?? (_createNew = CreateKey(MappingRuleSetCollection.Default.CreateNew));
72+
73+
private static ObjectMapperKeyBase Overwrite
74+
=> _overwrite ?? (_overwrite = CreateKey(MappingRuleSetCollection.Default.Overwrite));
75+
76+
private static ObjectMapperKeyBase Project
77+
=> _project ?? (_project = CreateKey(MappingRuleSetCollection.Default.Project));
78+
79+
private static ObjectMapperKeyBase Merge
80+
=> _merge ?? (_merge = CreateKey(MappingRuleSetCollection.Default.Merge));
81+
82+
private static ObjectMapperKeyBase CreateKey(MappingRuleSet ruleSet)
83+
{
84+
return new RootObjectMapperKey(
85+
ruleSet,
86+
MappingTypes<TSource, TTarget>.Fixed,
87+
mapperContext: null);
88+
}
89+
90+
public static ObjectMapperKeyBase GetKeyFor(IMappingContext mappingContext)
91+
{
92+
var ruleSets = mappingContext.MapperContext.RuleSets;
93+
94+
if (mappingContext.RuleSet == ruleSets.CreateNew)
95+
{
96+
return CreateNew;
97+
}
98+
99+
if (mappingContext.RuleSet == ruleSets.Overwrite)
100+
{
101+
return Overwrite;
102+
}
103+
104+
if (mappingContext.RuleSet == ruleSets.Project)
105+
{
106+
return Project;
107+
}
108+
109+
return Merge;
110+
}
111+
}
60112
}
61113
}

0 commit comments

Comments
 (0)