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

Avoid unnecessary lazy init in StructuralComparisons #101344

Merged
merged 2 commits into from
Apr 21, 2024

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Apr 20, 2024

The only benefit of having these be lazy in this manner is avoiding one of the two objects being constructed. We can instead just combine them into a singleton that's initialized in a static readonly.

Best reviewed without whitespace diff: https://github.com/dotnet/runtime/pull/101344/files?w=1

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

  • Some code out there may get broken by StructuralComparisons.StructuralComparer implementing IEqualityComparer or StructuralComparisons.StructuralEqualityComparer implementing IComparer.
  • IL trimmer may end up keeping unusued interface implementation around now that both interfaces are implemented by the same type.

Structural equality is used very rarely, so it is unlikely for these concerns to be a problem in practice. I am mentioning them out of abundance of caution. An alternative would be to keep the two types and add static readonly field in each of the types to get the lazy initialization for free.

@stephentoub
Copy link
Member Author

An alternative would be to keep the two types and add static readonly field in each of the types

I'll switch to that

Move the fields to the target types and make them readonly instead of manually lazy.
@stephentoub stephentoub merged commit 4aeb952 into dotnet:main Apr 21, 2024
84 of 87 checks passed
@stephentoub stephentoub deleted the avoidlazyinit branch April 21, 2024 17:27
matouskozak pushed a commit to matouskozak/runtime that referenced this pull request Apr 30, 2024
* Avoid unnecessary lazy init in StructuralComparisons

Move the fields to the target types and make them readonly instead of manually lazy.

* Update StructuralComparisons.cs
@github-actions github-actions bot locked and limited conversation to collaborators May 23, 2024
Sign up for free to 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