forked from dotnet/msbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#7052 from drewnoakes/dev/drnoakes/globbing-…
…perf-2 Remove closure allocation in CompositeGlob.IsMatch
- Loading branch information
Showing
3 changed files
with
45 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// Gets a value indicating whether any elements are in this collection | ||
/// that match a given condition. | ||
/// </summary> | ||
/// <remarks> | ||
/// 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. | ||
/// </remarks> | ||
/// <typeparam name="TElement">The type of element contained by the collection.</typeparam> | ||
/// <typeparam name="TArg">The type of argument passed to <paramref name="predicate"/>.</typeparam> | ||
/// <param name="immutableArray">The array to check.</param> | ||
/// <param name="predicate">The predicate.</param> | ||
/// <param name="arg">The argument to pass to <paramref name="predicate"/>.</param> | ||
public static bool Any<TElement, TArg>(this ImmutableArray<TElement> immutableArray, Func<TElement, TArg, bool> predicate, TArg arg) | ||
{ | ||
foreach (TElement element in immutableArray) | ||
{ | ||
if (predicate(element, arg)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |