Skip to content

Conversation

MihaZupan
Copy link
Member

@MihaZupan MihaZupan commented May 14, 2025

IndexOfAny(char[]) has per-call overhead to figure out what to do.
On modern .NET it would eventually hit a vectorized helper, but has to analyze the values every time.

Quick test with a default MemoryCache:

.NET 9/10

Method Job KeyLength Mean Error Ratio
GetCachedValue main 1 41.35 ns 0.650 ns 1.00
GetCachedValue pr 1 41.71 ns 0.824 ns 1.01
GetCachedValue main 16 59.67 ns 1.192 ns 1.00
GetCachedValue pr 16 41.36 ns 0.852 ns 0.69
GetCachedValue main 100 70.41 ns 1.420 ns 1.00
GetCachedValue pr 100 53.16 ns 1.043 ns 0.76

Framework

Method Job KeyLength Mean Error Ratio
GetCachedValue main 1 179.1 ns 3.57 ns 1.00
GetCachedValue pr 1 120.2 ns 2.36 ns 0.67
GetCachedValue main 16 187.5 ns 3.55 ns 1.00
GetCachedValue pr 16 136.2 ns 2.70 ns 0.73
GetCachedValue main 100 296.6 ns 5.82 ns 1.00
GetCachedValue pr 100 208.1 ns 4.07 ns 0.70
Microsoft Reviewers: Open in CodeFlow

Copy link
Contributor

@mgravell mgravell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tip!

@MihaZupan MihaZupan merged commit 28d9967 into dotnet:main May 14, 2025
6 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jun 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants