Skip to content
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

Make Remove-all-items O(1) #5350

Merged
merged 2 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions src/Build/Evaluation/LazyItemEvaluator.LazyItemOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,27 @@ protected bool NeedToExpandMetadataForEachItem(ImmutableList<ProjectMetadataElem

return needToExpandMetadataForEachItem;
}

protected static bool ItemspecContainsASingleItemReference(ItemSpec<P, I> itemSpec, string referencedItemType)
{
if (itemSpec.Fragments.Count != 1)
{
return false;
}

var itemExpressionFragment = itemSpec.Fragments[0] as ItemSpec<P, I>.ItemExpressionFragment;
if (itemExpressionFragment == null)
{
return false;
}

if (!itemExpressionFragment.Capture.ItemType.Equals(referencedItemType, StringComparison.OrdinalIgnoreCase))
{
return false;
}

return true;
}
}
}
}
25 changes: 22 additions & 3 deletions src/Build/Evaluation/LazyItemEvaluator.RemoveOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ public RemoveOperation(RemoveOperationBuilder builder, LazyItemEvaluator<P, I, M
_matchOnMetadataOptions = builder.MatchOnMetadataOptions;
}

// todo port the self referencing matching optimization (e.g. <I Remove="@(I)">) from Update to Remove as well. Ideally make one mechanism for both. https://github.com/Microsoft/msbuild/issues/2314
// todo Perf: do not match against the globs: https://github.com/Microsoft/msbuild/issues/2329
protected override ImmutableList<I> SelectItems(ImmutableList<ItemData>.Builder listBuilder, ImmutableHashSet<string> globsToIgnore)
/// <summary>
/// Apply the Remove operation.
/// </summary>
/// <remarks>
/// This operation is mostly implemented in terms of the default <see cref="LazyItemOperation.ApplyImpl(ImmutableList{ItemData}.Builder, ImmutableHashSet{string})"/>.
/// This override exists to apply the removing-everything short-circuit.
/// </remarks>
protected override void ApplyImpl(ImmutableList<ItemData>.Builder listBuilder, ImmutableHashSet<string> globsToIgnore)
{
var matchOnMetadataValid = !_matchOnMetadata.IsEmpty && _itemSpec.Fragments.Count == 1
&& _itemSpec.Fragments.First() is ItemSpec<ProjectProperty, ProjectItem>.ItemExpressionFragment;
Expand All @@ -34,6 +39,20 @@ protected override ImmutableList<I> SelectItems(ImmutableList<ItemData>.Builder
new BuildEventFileInfo(string.Empty),
"OM_MatchOnMetadataIsRestrictedToOnlyOneReferencedItem");

if (_matchOnMetadata.IsEmpty && ItemspecContainsASingleItemReference(_itemSpec, _itemElement.ItemType))
{
// Perf optimization: If the Remove operation references itself (e.g. <I Remove="@(I)"/>)
// then all items are removed and matching is not necessary
listBuilder.Clear();
return;
}

base.ApplyImpl(listBuilder, globsToIgnore);
}

// todo Perf: do not match against the globs: https://github.com/Microsoft/msbuild/issues/2329
protected override ImmutableList<I> SelectItems(ImmutableList<ItemData>.Builder listBuilder, ImmutableHashSet<string> globsToIgnore)
{
var items = ImmutableHashSet.CreateBuilder<I>();
foreach (ItemData item in listBuilder)
{
Expand Down
21 changes: 0 additions & 21 deletions src/Build/Evaluation/LazyItemEvaluator.UpdateOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,6 @@ private static bool ItemSpecContainsItemReferences(ItemSpec<P, I> itemSpec)
{
return itemSpec.Fragments.Any(f => f is ItemSpec<P,I>.ItemExpressionFragment);
}

private static bool ItemspecContainsASingleItemReference(ItemSpec<P, I> itemSpec, string referencedItemType)
{
if (itemSpec.Fragments.Count != 1)
{
return false;
}

var itemExpressionFragment = itemSpec.Fragments[0] as ItemSpec<P,I>.ItemExpressionFragment;
if (itemExpressionFragment == null)
{
return false;
}

if (!itemExpressionFragment.Capture.ItemType.Equals(referencedItemType, StringComparison.OrdinalIgnoreCase))
{
return false;
}

return true;
}
}
}
}