Skip to content

Remove LINQ usage from OpenAPI comparers #56599

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

Closed
wants to merge 1 commit into from

Conversation

captainsafia
Copy link
Member

Trying to improve schema transformer perf... 🤔

@ghost ghost added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Jul 3, 2024
return false;
}

foreach (var key in x.Keys)
Copy link
Member

Choose a reason for hiding this comment

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

Something to A/B test as I don't know the answer.

Would iterating like foreach ((var key, var valueX) in x) and avoiding the x[key] call be better or worse? I guess it depends on if iterating the keys is cheaper that iterating both and how often the right hand side of the || is accessed.

Comment on lines +53 to +59
for (var i = 0; i < x.Length; i++)
{
if (!Equals(x[i], y[i]))
{
return false;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Would SequenceEquals() on the two arrays be more performant maybe?

@mkArtakMSFT mkArtakMSFT added feature-openapi area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Jul 3, 2024
@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions labels Jul 3, 2024

internal static class ComparerHelpers
{
internal static bool DictionaryEquals<TKey, TValue>(IDictionary<TKey, TValue> x, IDictionary<TKey, TValue> y, IEqualityComparer<TValue> comparer)
Copy link
Member

Choose a reason for hiding this comment

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

Are the IDictionary and IList properties on OpenApiSchema just Dictionary and List? If so, there might be some perf benefits of adding a path that casts to the concrete types and duplicates the comparison logic.

That would avoid interface dispatch overhead, at the cost of some duplication.

Copy link
Member Author

Choose a reason for hiding this comment

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

Are the IDictionary and IList properties on OpenApiSchema just Dictionary and List?

They are. It's technically feasible for an end-user to override them with their own implementations but I suspect that is not likely. This was a good idea. There's a reasonable delta between the baseline and this change.

Remove LINQ from comparers

Method TransformerCount Mean Error StdDev Op/s Gen0 Allocated
SchemaTransformer 10 28.16 us 0.510 us 0.477 us 35,515.0 0.2441 31.78 KB
SchemaTransformer 100 30.16 us 0.480 us 0.449 us 33,158.4 0.2441 31.78 KB
SchemaTransformer 1000 35.36 us 0.395 us 0.370 us 28,277.9 0.2441 31.78 KB

Cast to concrete types in overloads

Method TransformerCount Mean Error StdDev Op/s Gen0 Allocated
SchemaTransformer 10 24.39 us 0.486 us 0.431 us 40,995.4 0.2441 31.19 KB
SchemaTransformer 100 25.32 us 0.304 us 0.284 us 39,491.7 0.2441 31.19 KB
SchemaTransformer 1000 29.84 us 0.328 us 0.290 us 33,517.4 0.2441 32.19 KB

@captainsafia
Copy link
Member Author

Closing this in favor of a new PR that accounts for the latest changes merged into this implementation.

@dotnet-policy-service dotnet-policy-service bot added this to the 9.0-preview7 milestone Jul 17, 2024
@captainsafia captainsafia deleted the safia/schema-transform-perf branch July 22, 2024 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants