Skip to content

Commit

Permalink
Remove unnecessary list allocations (#6529)
Browse files Browse the repository at this point in the history
Fixes #6062
  • Loading branch information
AR-May committed Jun 17, 2021
1 parent f4b792b commit c86ab72
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Build/Evaluation/ExpressionShredder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal static SemiColonTokenizer SplitSemiColonSeparatedList(string expression
/// where metadata key is like "itemname.metadataname" or "metadataname".
/// PERF: Tables are null if there are no entries, because this is quite a common case.
/// </summary>
internal static ItemsAndMetadataPair GetReferencedItemNamesAndMetadata(List<string> expressions)
internal static ItemsAndMetadataPair GetReferencedItemNamesAndMetadata(IEnumerable<string> expressions)
{
ItemsAndMetadataPair pair = new ItemsAndMetadataPair(null, null);

Expand Down
14 changes: 7 additions & 7 deletions src/Build/Evaluation/LazyItemEvaluator.LazyItemOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ protected void DecorateItemsWithMetadata(IEnumerable<ItemBatchingContext> itemBa
// End of legal area for metadata expressions.
_expander.Metadata = null;
}

// End of pseudo batching
////////////////////////////////////////////////////
// Start of old code
Expand Down Expand Up @@ -283,17 +282,18 @@ protected void DecorateItemsWithMetadata(IEnumerable<ItemBatchingContext> itemBa
}
}

protected bool NeedToExpandMetadataForEachItem(ImmutableList<ProjectMetadataElement> metadata, out ItemsAndMetadataPair itemsAndMetadataFound)
private static IEnumerable<string> GetMetadataValuesAndConditions(ImmutableList<ProjectMetadataElement> metadata)
{
List<string> values = new List<string>(metadata.Count * 2);

foreach (var metadataElement in metadata)
{
values.Add(metadataElement.Value);
values.Add(metadataElement.Condition);
yield return metadataElement.Value;
yield return metadataElement.Condition;
}
}

itemsAndMetadataFound = ExpressionShredder.GetReferencedItemNamesAndMetadata(values);
protected bool NeedToExpandMetadataForEachItem(ImmutableList<ProjectMetadataElement> metadata, out ItemsAndMetadataPair itemsAndMetadataFound)
{
itemsAndMetadataFound = ExpressionShredder.GetReferencedItemNamesAndMetadata(GetMetadataValuesAndConditions(metadata));

bool needToExpandMetadataForEachItem = false;

Expand Down
50 changes: 24 additions & 26 deletions src/Build/Evaluation/LazyItemEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,38 +612,36 @@ private void ProcessItemSpec(string rootDirectory, string itemSpec, IElementLoca
}
}

private static IEnumerable<string> GetExpandedMetadataValuesAndConditions(ICollection<ProjectMetadataElement> metadata, Expander<P, I> expander)
{
// Since we're just attempting to expand properties in order to find referenced items and not expanding metadata,
// unexpected errors may occur when evaluating property functions on unexpanded metadata. Just ignore them if that happens.
// See: https://github.com/Microsoft/msbuild/issues/3460
const ExpanderOptions expanderOptions = ExpanderOptions.ExpandProperties | ExpanderOptions.LeavePropertiesUnexpandedOnError;

// Expand properties here, because a property may have a value which is an item reference (ie "@(Bar)"), and
// if so we need to add the right item reference.
foreach (var metadatumElement in metadata)
{
yield return expander.ExpandIntoStringLeaveEscaped(
metadatumElement.Value,
expanderOptions,
metadatumElement.Location);

yield return expander.ExpandIntoStringLeaveEscaped(
metadatumElement.Condition,
expanderOptions,
metadatumElement.ConditionLocation);
}
}

private void ProcessMetadataElements(ProjectItemElement itemElement, OperationBuilderWithMetadata operationBuilder)
{
if (itemElement.HasMetadata)
{
operationBuilder.Metadata.AddRange(itemElement.Metadata);

var values = new List<string>(itemElement.Metadata.Count * 2);

// Expand properties here, because a property may have a value which is an item reference (ie "@(Bar)"), and
// if so we need to add the right item reference.
foreach (var metadatumElement in itemElement.Metadata)
{
// Since we're just attempting to expand properties in order to find referenced items and not expanding metadata,
// unexpected errors may occur when evaluating property functions on unexpanded metadata. Just ignore them if that happens.
// See: https://github.com/Microsoft/msbuild/issues/3460
const ExpanderOptions expanderOptions = ExpanderOptions.ExpandProperties | ExpanderOptions.LeavePropertiesUnexpandedOnError;

var valueWithPropertiesExpanded = _expander.ExpandIntoStringLeaveEscaped(
metadatumElement.Value,
expanderOptions,
metadatumElement.Location);

var conditionWithPropertiesExpanded = _expander.ExpandIntoStringLeaveEscaped(
metadatumElement.Condition,
expanderOptions,
metadatumElement.ConditionLocation);

values.Add(valueWithPropertiesExpanded);
values.Add(conditionWithPropertiesExpanded);
}

var itemsAndMetadataFound = ExpressionShredder.GetReferencedItemNamesAndMetadata(values);
var itemsAndMetadataFound = ExpressionShredder.GetReferencedItemNamesAndMetadata(GetExpandedMetadataValuesAndConditions(itemElement.Metadata, _expander));
if (itemsAndMetadataFound.Items != null)
{
foreach (var itemType in itemsAndMetadataFound.Items)
Expand Down

0 comments on commit c86ab72

Please sign in to comment.