Skip to content

Commit

Permalink
TargetEntry optimizations for Results (#9590)
Browse files Browse the repository at this point in the history
Context
@jeffkl has a case with potentially thousands of items that would all go into one bucket in the Returns. MSBuild is deduplicating it, but so is NuGet, which is wasteful, so he will likely stop using Returns. This change will save him a small amount of allocations, but it would save a ton of allocations if he were to keep using it, since it will no longer need to repeatedly reallocate the HashSet, and it won't need to allocate as many Lists. It will still save one List's worth of allocations on every single Target that doesn't have a Returns.

Changes Made
Effectively making List allocations lazier and taking advantage of ToList to avoid doing as many (duplicated) collection operations.
  • Loading branch information
Forgind committed Feb 1, 2024
1 parent 0868f37 commit 07fd5d5
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/Build/BackEnd/Components/RequestBuilder/TargetEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.Collections;
Expand Down Expand Up @@ -568,7 +569,7 @@ internal async Task ExecuteTarget(ITaskBuilder taskBuilder, BuildRequestEntry re
}

// Produce the final results.
List<TaskItem> targetOutputItems = new List<TaskItem>();
TaskItem[] targetOutputItems = Array.Empty<TaskItem>();

try
{
Expand Down Expand Up @@ -622,37 +623,50 @@ internal async Task ExecuteTarget(ITaskBuilder taskBuilder, BuildRequestEntry re

if (keepDupes)
{
List<TaskItem> targetOutputItemsList = new();
foreach (ItemBucket bucket in batchingBuckets)
{
targetOutputItems.AddRange(bucket.Expander.ExpandIntoTaskItemsLeaveEscaped(targetReturns, ExpanderOptions.ExpandAll, targetReturnsLocation));
if (targetOutputItems is null)
{
// As an optimization, use the results for the first bucket and if there are no more buckets to process, only a single list is allocated.
targetOutputItemsList = bucket.Expander.ExpandIntoTaskItemsLeaveEscaped(targetReturns, ExpanderOptions.ExpandAll, targetReturnsLocation).ToList();
}
else
{
targetOutputItemsList.AddRange(bucket.Expander.ExpandIntoTaskItemsLeaveEscaped(targetReturns, ExpanderOptions.ExpandAll, targetReturnsLocation));
}
}

targetOutputItems = targetOutputItemsList.ToArray();
}
else
{
HashSet<TaskItem> addedItems = new HashSet<TaskItem>();
foreach (ItemBucket bucket in batchingBuckets)
// Optimize for only one bucket by initializing the HashSet<T> with the first one's items in case there are a lot of items, it won't need to be resized.
if (batchingBuckets.Count == 1)
{
IList<TaskItem> itemsToAdd = bucket.Expander.ExpandIntoTaskItemsLeaveEscaped(targetReturns, ExpanderOptions.ExpandAll, targetReturnsLocation);

foreach (TaskItem item in itemsToAdd)
targetOutputItems = new HashSet<TaskItem>(batchingBuckets[0].Expander.ExpandIntoTaskItemsLeaveEscaped(targetReturns, ExpanderOptions.ExpandAll, targetReturnsLocation)).ToArray();
}
else
{
HashSet<TaskItem> addedItems = new HashSet<TaskItem>();
foreach (ItemBucket bucket in batchingBuckets)
{
if (!addedItems.Contains(item))
{
targetOutputItems.Add(item);
addedItems.Add(item);
}
IList<TaskItem> itemsToAdd = bucket.Expander.ExpandIntoTaskItemsLeaveEscaped(targetReturns, ExpanderOptions.ExpandAll, targetReturnsLocation);
addedItems.UnionWith(itemsToAdd);
}

targetOutputItems = addedItems.ToArray();
}
}
}
}
finally
{
// log the last target finished since we now have the target outputs.
targetLoggingContext?.LogTargetBatchFinished(projectFullPath, targetSuccess, targetOutputItems?.Count > 0 ? targetOutputItems : null);
targetLoggingContext?.LogTargetBatchFinished(projectFullPath, targetSuccess, targetOutputItems.Length > 0 ? targetOutputItems : null);
}

_targetResult = new TargetResult(targetOutputItems.ToArray(), aggregateResult, targetLoggingContext?.BuildEventContext);
_targetResult = new TargetResult(targetOutputItems, aggregateResult, targetLoggingContext?.BuildEventContext);

if (aggregateResult.ResultCode == WorkUnitResultCode.Failed && aggregateResult.ActionCode == WorkUnitActionCode.Stop)
{
Expand Down

0 comments on commit 07fd5d5

Please sign in to comment.