-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.OpenApi; | ||
|
||
internal static class ComparerHelpers | ||
{ | ||
internal static bool DictionaryEquals<TKey, TValue>(IDictionary<TKey, TValue> x, IDictionary<TKey, TValue> y, IEqualityComparer<TValue> comparer) | ||
where TKey : notnull | ||
where TValue : notnull | ||
{ | ||
if (x.Keys.Count != y.Keys.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
foreach (var key in x.Keys) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
{ | ||
if (!y.TryGetValue(key, out var value) || !comparer.Equals(x[key], value)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
internal static bool ListEquals<T>(IList<T> x, IList<T> y, IEqualityComparer<T> comparer) | ||
{ | ||
if (x.Count != y.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < x.Count; i++) | ||
{ | ||
if (!comparer.Equals(x[i], y[i])) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
internal static bool ByteArrayEquals(byte[] x, byte[] y) | ||
{ | ||
if (x.Length != y.Length) | ||
{ | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < x.Length; i++) | ||
{ | ||
if (!Equals(x[i], y[i])) | ||
{ | ||
return false; | ||
} | ||
} | ||
Comment on lines
+53
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would |
||
|
||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Cast to concrete types in overloads