Skip to content

Redefine FigureLength as readonly struct to prevent defensive copies #9887

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

h3xds1nz
Copy link
Member

@h3xds1nz h3xds1nz commented Oct 3, 2024

Description

Since FigureLength is immutable by design, we should adjust it to readonly struct. This will also prevent defensive copies. It will also improve code quality. This is not a breaking change.

  • I've also simplified the ctor a bit, which will also offer a very minor performance benefit (it does not matter).
  • Minor cleanup of the usual IEquatable functions to save precious code lines and simplify it.
  • The value was being set to 0.0 in case of Auto but when queried, it is set to 1.0.
    • I couldn't see a reason for it, the only thing that made sense was the hash code creation but the hash code is not unique even for basic values. It is too easy to get duplicates. This should be fixed later, I'll create a tracking issue.

Customer Impact

Possibly a bit of perf and a clear directive that FigureLength and all its methods are not adjusting the state of the struct.

Regression

No.

Testing

Local build, base testing, type creation, etc.

Risk

I do not asses any, since the struct is immutable and this is not a breaking change. It is a change to a public type but such changes have been done previously in runtime, see for example #97421.

Microsoft Reviewers: Open in CodeFlow

@h3xds1nz h3xds1nz requested review from a team as code owners October 3, 2024 19:24
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Oct 3, 2024
@miloush
Copy link
Contributor

miloush commented Oct 4, 2024

Funny hash code. Okay so if someone binary-serialized an instance of this structure to the disk, would upgrade .NET and try to deserialize it back, then the deserialized instances of Auto one would not equal new instances of Auto, correct?

@h3xds1nz
Copy link
Member Author

h3xds1nz commented Oct 4, 2024

@miloush I've found one similar implementation, must have been done by the same engineer as the comments are same.

Equality is done over the property, not the field, so those two would still match. Obviously the payload itself would change but when deserialized and loaded, those two would still be equal.

@miloush
Copy link
Contributor

miloush commented Oct 4, 2024

Ah missed the property, thanks!

@h3xds1nz
Copy link
Member Author

h3xds1nz commented Oct 4, 2024

No worries, thanks for reviewing the changes and asking the right questions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Change Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants