Skip to content

Commit 714df25

Browse files
committed
Speed up large lists
1 parent e54297e commit 714df25

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/AutoMapper.Collection.Tests/MapCollectionWithEquality.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public void Should_Update_Existing_Item()
5050
Mapper.Map(dtos, items.ToList()).Should().HaveElementAt(0, items.First());
5151
}
5252

53+
public void Should_Be_Fast_With_Large_Lists()
54+
{
55+
var dtos = new object[100000].Select((_, i) => new ThingDto {ID = i}).ToList();
56+
57+
var items = new object[100000].Select((_, i) => new Thing { ID = i }).ToList();
58+
59+
Mapper.Map(dtos, items.ToList()).Should().HaveElementAt(0, items.First());
60+
}
61+
5362
public void Should_Work_With_Null_Destination()
5463
{
5564
var dtos = new List<ThingDto>

src/AutoMapper.Collection/Mappers/EquivalentExpressionAddRemoveCollectionMapper.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ public static TDestination Map<TSource, TSourceItem, TDestination, TDestinationI
2121
if (source == null || destination == null)
2222
return destination;
2323

24-
var compareSourceToDestination = source.ToDictionary(s => s, s => destination.FirstOrDefault(d => EquivalencyExpression.IsEquivalent(s, d)));
24+
var destList = destination.ToList();
25+
var compareSourceToDestination = source.ToDictionary(s => s, s =>
26+
{
27+
var match = destList.FirstOrDefault(d => EquivalencyExpression.IsEquivalent(s, d));
28+
destList.Remove(match);
29+
return match;
30+
});
2531

2632
foreach (var removedItem in destination.Except(compareSourceToDestination.Values).ToList())
2733
destination.Remove(removedItem);

0 commit comments

Comments
 (0)