diff --git a/src/Build/Globbing/CompositeGlob.cs b/src/Build/Globbing/CompositeGlob.cs index 96606323bc9..4be643c1695 100644 --- a/src/Build/Globbing/CompositeGlob.cs +++ b/src/Build/Globbing/CompositeGlob.cs @@ -27,14 +27,15 @@ public class CompositeGlob : IMSBuildGlob /// /// Children globs. Input gets shallow cloned public CompositeGlob(IEnumerable globs) - : this(globs is ImmutableArray immutableGlobs ? immutableGlobs : globs.ToImmutableArray()) + : this(globs.ToImmutableArray()) {} /// /// Constructor /// /// Children globs. Input gets shallow cloned - public CompositeGlob(params IMSBuildGlob[] globs) : this(globs.ToImmutableArray()) + public CompositeGlob(params IMSBuildGlob[] globs) + : this(ImmutableArray.Create(globs)) {} /// @@ -61,7 +62,7 @@ public bool IsMatch(string stringToMatch) // Threadpools are a scarce resource in Visual Studio, do not use them. //return Globs.AsParallel().Any(g => g.IsMatch(stringToMatch)); - return _globs.Any(g => g.IsMatch(stringToMatch)); + return _globs.Any(static (glob, str) => glob.IsMatch(str), stringToMatch); } /// diff --git a/src/Build/Microsoft.Build.csproj b/src/Build/Microsoft.Build.csproj index 4091e855a27..cf89522d904 100644 --- a/src/Build/Microsoft.Build.csproj +++ b/src/Build/Microsoft.Build.csproj @@ -159,6 +159,7 @@ + diff --git a/src/Build/Utilities/ImmutableCollectionsExtensions.cs b/src/Build/Utilities/ImmutableCollectionsExtensions.cs new file mode 100644 index 00000000000..242c5248400 --- /dev/null +++ b/src/Build/Utilities/ImmutableCollectionsExtensions.cs @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Immutable; + +// Added to the System.Linq extension method as these extensions augment those +// provided by Linq. The immutable collections library includes ImmutableArrayExtensions +// which is also in this namespace. + +namespace System.Linq +{ + internal static class ImmutableCollectionsExtensions + { + /// + /// Gets a value indicating whether any elements are in this collection + /// that match a given condition. + /// + /// + /// This extension method accepts an argument which is then passed, on the stack, to the predicate. + /// This allows using a static lambda, which can avoid a per-call allocation of a closure object. + /// + /// The type of element contained by the collection. + /// The type of argument passed to . + /// The array to check. + /// The predicate. + /// The argument to pass to . + public static bool Any(this ImmutableArray immutableArray, Func predicate, TArg arg) + { + foreach (TElement element in immutableArray) + { + if (predicate(element, arg)) + { + return true; + } + } + + return false; + } + } +}