dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.27k stars 5.9k forks source link

Is it possible to describe "typical" performance degradations of using abstract types, like IReadOnlyDictionary<T,T> over Dictionary<T,T> #38279

Open taylorchasewhite opened 11 months ago

taylorchasewhite commented 11 months ago

Type of issue

Missing information

Description

I appreciate the code analysis warnings introduced in .NET 8, but I'm struggling with this one. In general, my firm likes to enforce read-only usage of collections, via IReadOnlyList and IReadOnlyDictionary, and obviously this warning conflicts with that objective. It would be helpful to many folks getting this warning to understand the extent of the performance degradation from using an interface declaration vs. a concrete type declaration, even if the description of that degradation is very brief and broad (e.g. it's on average 10% slower, or 50% slower to call the virtual method).

Page URL

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1859

Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/code-analysis/quality-rules/ca1859.md

Document Version Independent Id

c8f7f102-2398-9acc-71b8-b0f848a05b92

Article author

gewarren

Metadata

shravan2x commented 1 month ago

From https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-8/, it appears to be ~20%. That said, at a difference of 0.3ns, I'll personally stick to IReadOnlyXyz types except on the hottest of paths.