Skip to content

Avoid unnecessary lazy init in StructuralComparisons - #101344

Merged
stephentoub merged 2 commits into
dotnet:mainfrom
stephentoub:avoidlazyinit
Apr 21, 2024
Merged

Avoid unnecessary lazy init in StructuralComparisons#101344
stephentoub merged 2 commits into
dotnet:mainfrom
stephentoub:avoidlazyinit

Conversation

@stephentoub

@stephentoub stephentoub commented Apr 20, 2024

Copy link
Copy Markdown
Member

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

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
@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.

3 participants