Skip to content

Reduce per-lookup overhead from key validation in HybridCache - #6441

Merged
MihaZupan merged 2 commits into
dotnet:mainfrom
MihaZupan:key-validation
May 14, 2025
Merged

Reduce per-lookup overhead from key validation in HybridCache#6441
MihaZupan merged 2 commits into
dotnet:mainfrom
MihaZupan:key-validation

Conversation

@MihaZupan

@MihaZupanMihaZupan commented May 14, 2025

Copy link
Copy Markdown
Member

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

MethodJobKeyLengthMeanErrorRatio
GetCachedValuemain141.35 ns0.650 ns1.00
GetCachedValuepr141.71 ns0.824 ns1.01
GetCachedValuemain1659.67 ns1.192 ns1.00
GetCachedValuepr1641.36 ns0.852 ns0.69
GetCachedValuemain10070.41 ns1.420 ns1.00
GetCachedValuepr10053.16 ns1.043 ns0.76

Framework

MethodJobKeyLengthMeanErrorRatio
GetCachedValuemain1179.1 ns3.57 ns1.00
GetCachedValuepr1120.2 ns2.36 ns0.67
GetCachedValuemain16187.5 ns3.55 ns1.00
GetCachedValuepr16136.2 ns2.70 ns0.73
GetCachedValuemain100296.6 ns5.82 ns1.00
GetCachedValuepr100208.1 ns4.07 ns0.70
Microsoft Reviewers: Open in CodeFlow

@mgravellmgravell left a comment

Copy link
Copy Markdown
Contributor

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:mainMay 14, 2025
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Jun 14, 2025
Sign up for freeto 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

@MihaZupan@mgravell@stephentoub