Speeding Up Searches with SearchValues in .NET 8

By FoxLearn 12/31/2024 4:59:55 AM   88
With the release of .NET 8, Microsoft introduced the SearchValues class, a new addition that leverages vectorization and hardware acceleration to improve search performance.

In this article, we’ll explain how you can use SearchValues to improve the speed of searches in .NET Core applications.

What is SearchValues?

SearchValues is a new type in the System.Buffers namespace in .NET designed to enhance search efficiency and performance. It leverages vectorization and hardware acceleration, allowing for faster searches. The SearchValues class represents an immutable and read-only collection of values, optimizing repeated searches in various datasets.

Why Traditional String Searches Can Be Inefficient

In many applications, string searches are a common but often slow operation. For example, the IndexOfAny() method in .NET searches for a character or set of characters in a string, but it may not be the most efficient when performed repeatedly or with large datasets.

Here’s an example using IndexOfAny() to search for individual characters within a string:

string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
char[] charsToFind = { 'a', 'b', 'c' };
int index = str.IndexOfAny(charsToFind);
Console.WriteLine(index);  // Outputs the index of 'a', 'b', or 'c'

However, if you need to perform this search repeatedly, especially with multiple characters, this approach becomes inefficient. You can overcome this issue with SearchValues, which optimizes the search process, handling multiple character lookups in parallel.

How SearchValues Improves Performance

By precomputing the necessary search data, SearchValues enables faster lookups. The key benefit is the ability to execute searches on large datasets efficiently using the CPU’s vector processing power.

If you're checking for alphanumeric characters within a string, you can use SearchValues.

SearchValues<char> searchValues = SearchValues.Create("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
bool ContainsAlphanumeric(ReadOnlySpan<char> text) =>
    text.IndexOfAny(searchValues) != -1;

Optimized Search in a Large Text Dataset

Consider a scenario where you need to check if a large block of text contains any digits.

SearchValues<char> searchValues = SearchValues.Create("0123456789");
bool ContainsDigits(ReadOnlySpan<char> text) =>
    text.IndexOfAny(searchValues) != -1;

The SearchValues type is designed to find the first occurrence of a specific value in a collection. When used in code, the runtime automatically selects the most efficient implementation based on the scenario, such as optimizing searches for contiguous ranges of values.

Performance Benchmarking

To measure the performance benefits, you can compare the traditional IndexOfAny() method with SearchValues using benchmarking tools like BenchmarkDotNet.

[MemoryDiagnoser]
public class PerformanceBenchmark
{
    private char[] charValues = { 'a', 'b', 'c', 'd' };
    private SearchValues<char> searchValues = SearchValues.Create("abcd");
    private string text = "abcdefghi";

    [Benchmark]
    public int IndexOfCharBenchmark() =>
        text.AsSpan().IndexOfAny(charValues);

    [Benchmark]
    public int IndexOfSearchValuesBenchmark() =>
        text.AsSpan().IndexOfAny(searchValues);
}

The SearchValues class in .NET 8 revolutionizes search performance in data-intensive applications. By leveraging vectorization and hardware acceleration, it drastically reduces search times, making it an ideal choice for scenarios that require frequent searches over large datasets. Whether you are working with strings, collections, or raw memory, SearchValues optimizes the process and provides a significant performance boost.