SearchValues<T> in .NET 8 Beats IndexOfAny Only Under the Right Conditions
A developer benchmarking .NET 8's SearchValues<T> type against IndexOfAny on a 32 MB log buffer found only a marginal 0.5 ms improvement when delimiters appeared frequently, but a more meaningful 1.7x speedup when matches were rare and the scanner could run through long stretches uninterrupted. The tests, run on .NET 10 in a Linux container, confirmed that IndexOfAny with a small character set is already heavily vectorized and competitive in dense-match scenarios. Hand-rolled character loops, however, proved nearly twice as slow as either API, suggesting that manual iteration in hot code paths is the bigger performance problem worth fixing. Crucially, creating a SearchValues instance inside a frequently called method rather than as a static readonly field made it nearly three times slower than standard IndexOfAny. The practical takeaway is that SearchValues delivers real gains primarily on large buffers with sparse matches, provided it is initialized once and reused.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in