Skip to content

Reduce allocations in TaskExecutionHost #11804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

using TaskItem = Microsoft.Build.Execution.ProjectItemInstance.TaskItem;
using Task = System.Threading.Tasks.Task;
using Microsoft.Build.Collections;

#nullable disable

Expand Down Expand Up @@ -1423,9 +1424,26 @@ private void GatherTaskItemOutputs(bool outputTargetIsItem, string outputTargetN

static IEnumerable<KeyValuePair<string, string>> EnumerateMetadata(IDictionary customMetadata)
{
foreach (DictionaryEntry de in customMetadata)
Copy link
Member

@SimaTian SimaTian May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the penalty for using the interface here please? Is it boxing in this case or something else?
(e.g. from what I read, we use specialized iterations based on type, so I assume there is something to be gained, hence the boxing question)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal here is to avoid boxing the struct enumerator of these collections. In sufficiently hot paths, the allocations can really add up.

if (customMetadata is CopyOnWriteDictionary<string> copyOnWriteDictionary)
{
yield return new KeyValuePair<string, string>((string)de.Key, EscapingUtilities.Escape((string)de.Value));
foreach (KeyValuePair<string, string> kvp in copyOnWriteDictionary)
{
yield return new KeyValuePair<string, string>(kvp.Key, EscapingUtilities.Escape(kvp.Value));
}
}
else if (customMetadata is Dictionary<string, string> dictionary)
{
foreach (KeyValuePair<string, string> kvp in dictionary)
{
yield return new KeyValuePair<string, string>(kvp.Key, EscapingUtilities.Escape(kvp.Value));
}
}
else
{
foreach (DictionaryEntry de in customMetadata)
{
yield return new KeyValuePair<string, string>((string)de.Key, EscapingUtilities.Escape((string)de.Value));
}
}
}
}
Expand Down
Loading