The Wayback Machine - https://web.archive.org/web/20201106150711/https://github.com/microsoft/aspnet-api-versioning/issues/690
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider improving the performance of Microsoft.AspNetCore.Mvc.Versioning.QueryStringApiVersionReader #690

Open
paulomorgado opened this issue Nov 4, 2020 · 1 comment
Assignees
Labels

Comments

@paulomorgado
Copy link

@paulomorgado paulomorgado commented Nov 4, 2020

The Read method of Microsoft.AspNetCore.Mvc.Versioning.QueryStringApiVersionReader unconditionally instantiates an HashSet<string> regardless of having a version specified or not with the only purpose of invoking CollectionExtensions.EnsureZeroOrOneApiVersions.

CollectionExtensions.EnsureZeroOrOneApiVersions uses Enumerable.SingleOrDefault to return null, if the hash set is empty; and its single item if it isn't.

Enumerable.SingleOrDefault has optimizations for IList<T>, but not for ICollection<T>, which requires the instantiation and boxing of HashSet<T>.Enumerator.

All this could be optimized by only instantiating the HashSet<string> if 2 or more different versions are found and having an extension method to report the error that takes an HashSet<string> for the optimized construction of the list of versions.

@commonsensesoftware
Copy link
Member

@commonsensesoftware commonsensesoftware commented Nov 4, 2020

This is not an unreasonable enhancement. I've converted many iterator loops to indexing loops, but there's still a few left. This is was one of the earlier bits of code. I'll review all of the readers for potential optimization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.