Skip to content

Commit 69435f3

Browse files
committed
Fix performance issue searching in list of 'availableTypes'. GetLogicalParent would be called n^2 times at worst.
1 parent c24de5a commit 69435f3

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/FluentNHibernate/Automapping/AutoMapper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,22 @@ Dictionary<AutoMapType, AutoMapType> GetMappingTypesWithLogicalParents()
121121
var excludedTypes = mappingTypes
122122
.Where(x => cfg.IsConcreteBaseType(x.Type.BaseType))
123123
.ToArray();
124-
var availableTypes = mappingTypes.Except(excludedTypes);
124+
var availableTypes = mappingTypes.Except(excludedTypes).ToDictionary(x => x.Type);
125125
var mappingTypesWithLogicalParents = new Dictionary<AutoMapType, AutoMapType>();
126126

127127
foreach (var type in availableTypes)
128-
mappingTypesWithLogicalParents.Add(type, GetLogicalParent(type.Type, availableTypes));
128+
mappingTypesWithLogicalParents.Add(type.Value, GetLogicalParent(type.Key, availableTypes));
129129
return mappingTypesWithLogicalParents;
130130
}
131131

132-
static AutoMapType GetLogicalParent(Type type, IEnumerable<AutoMapType> availableTypes)
132+
static AutoMapType GetLogicalParent(Type type, IDictionary<Type, AutoMapType> availableTypes)
133133
{
134134
if (type.BaseType == typeof(object) || type.BaseType == null)
135135
return null;
136136

137-
var baseType = availableTypes.FirstOrDefault(x => x.Type == type.BaseType);
137+
AutoMapType baseType;
138138

139-
if (baseType != null)
139+
if (availableTypes.TryGetValue(type.BaseType, out baseType))
140140
return baseType;
141141

142142
return GetLogicalParent(type.BaseType, availableTypes);

0 commit comments

Comments
 (0)