Skip to content

Commit 24f6070

Browse files
committed
fixing KeepDuplicates, adding a Hashtable for performance reasons and squashing due to a weird rebase
1 parent 69b3e7a commit 24f6070

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/Build/BackEnd/Components/RequestBuilder/IntrinsicTasks/ItemGroupIntrinsicTask.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,28 @@ private void ExecuteAdd(ProjectItemGroupTaskItemInstance child, ItemBucket bucke
215215
FileSystems.Default,
216216
LoggingContext);
217217

218+
Action<List<ProjectItemInstance>> logFunction = null;
219+
218220
if (LogTaskInputs && !LoggingContext.LoggingService.OnlyLogCriticalEvents && itemsToAdd?.Count > 0)
219221
{
220-
ItemGroupLoggingHelper.LogTaskParameter(
221-
LoggingContext,
222-
TaskParameterMessageKind.AddItem,
223-
parameterName: null,
224-
propertyName: null,
225-
child.ItemType,
226-
itemsToAdd,
227-
logItemMetadata: true,
228-
child.Location);
222+
logFunction = (itemList) =>
223+
{
224+
ItemGroupLoggingHelper.LogTaskParameter(
225+
LoggingContext,
226+
TaskParameterMessageKind.AddItem,
227+
parameterName: null,
228+
propertyName: null,
229+
child.ItemType,
230+
itemList,
231+
logItemMetadata: true,
232+
child.Location);
233+
};
229234
}
235+
230236

231-
// Now add the items we created to the lookup.
232-
bucket.Lookup.AddNewItemsOfItemType(child.ItemType, itemsToAdd, !keepDuplicates); // Add in one operation for potential copy-on-write
237+
// Now add the items we created to the lookup.
238+
bucket.Lookup.AddNewItemsOfItemType(child.ItemType, itemsToAdd, !keepDuplicates, logFunction);
239+
// Add in one operation for potential copy-on-write
233240
}
234241

235242
/// <summary>

src/Build/BackEnd/Components/RequestBuilder/Lookup.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Runtime.CompilerServices;
78
using System.Threading;
89
using Microsoft.Build.Collections;
910
using Microsoft.Build.Evaluation;
@@ -636,7 +637,7 @@ internal void SetProperty(ProjectPropertyInstance property)
636637
/// <summary>
637638
/// Implements a true add, an item that has been created in a batch.
638639
/// </summary>
639-
internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInstance> group, bool doNotAddDuplicates = false)
640+
internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInstance> group, bool doNotAddDuplicates = false, Action<List<ProjectItemInstance>> logFunction = null)
640641
{
641642
// Adding to outer scope could be easily implemented, but our code does not do it at present
642643
MustNotBeOuterScope();
@@ -663,12 +664,32 @@ internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInst
663664

664665
// Ensure we don't also add any that already exist.
665666
var existingItems = GetItems(itemType);
667+
668+
var existingItemsHashSet = existingItems.ToHashSet(ProjectItemInstance.EqualityComparer);
669+
666670
if (existingItems.Count > 0)
667671
{
668-
itemsToAdd = itemsToAdd.Where(item => !existingItems.Contains(item, ProjectItemInstance.EqualityComparer));
672+
673+
var deduplicatedItemsToAdd = new List<ProjectItemInstance>();
674+
foreach (var item in itemsToAdd) {
675+
if (!existingItemsHashSet.Contains(item)) {
676+
deduplicatedItemsToAdd.Add(item);
677+
}
678+
}
679+
itemsToAdd = deduplicatedItemsToAdd;
669680
}
670681
}
671682

683+
if (doNotAddDuplicates)
684+
{
685+
logFunction?.Invoke(itemsToAdd.ToList());
686+
}
687+
else {
688+
var groupAsList = group as List<ProjectItemInstance>;
689+
logFunction?.Invoke(groupAsList ?? group.ToList());
690+
}
691+
692+
672693
PrimaryAddTable.ImportItemsOfType(itemType, itemsToAdd);
673694
}
674695

0 commit comments

Comments
 (0)