-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Memorytenet-performancePerformance related issuePerformance related issue
Milestone
Description
Background and motivation
Augmenting the recently-added IndexOfAnyValues APIs: #68328 (comment)
When updating existing code to use the new IndexOfAnyValues API, it is very common to replace the existing needle constant with the IndexOfAnyValues instance.
This is fine if the only operations using said needle were {Last}IndexOfAny{Except} calls, but it is not uncommon to also use that needle for single-value checks Needle.Contains(c). In those cases, you are forced into keeping the original needle around.
As IndexOfAnyValues is also already a representation of values optimized for searching, it can be more efficient than a full linear scan of the needle.
API Proposal
namespace System.Buffers;
public class IndexOfAnyValues<T> where T : IEquatable<T>?
{
public virtual bool Contains(T value);
}API Usage
Instead of
private const string Needle = ",[]&*+";
private static readonly IndexOfAnyValues<char> s_needle = IndexOfAnyValues.Create(Needle);
if (Needle.Contains(c))
{
// ...
}you can do
private static readonly IndexOfAnyValues<char> s_needle = IndexOfAnyValues.Create(",[]&*+");
if (s_needle.Contains(c))
{
// ...
}Alternative Designs
No response
Risks
No response
stephentoub, lilinus, PaulusParssinen and Tan90909090LeaFrock
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Memorytenet-performancePerformance related issuePerformance related issue