-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Mark applicable structs as readonly #14789
Conversation
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.
We should make sure that the corefx contract consistency check start verifying the readonly annotations (if they are not doing it already). The contracts have to stay in sync with the implementation.
| /// TraceLogging: Empty struct indicating no payload data. | ||
| /// </summary> | ||
| internal struct EmptyStruct | ||
| internal readonly struct EmptyStruct |
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.
@brianrob Are you ok with marking these as readonly - will it cause issues with source sharing?
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.
This is also in the same boat as the other "empty" structs below. I'm fine keeping the changes for these and fine getting rid of them.
src/mscorlib/shared/System/Void.cs
Outdated
| { | ||
| // This class represents the void return type | ||
| public struct Void | ||
| public readonly struct Void |
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.
You can mark this readonly, but it has zero value here - just adds extra bytes to the binary. The marking on this type looks a bit odd to me.
I am wondering what the rule should be: mark structs as read only because of it makes senses (it would be my preference), or just because of we can?
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.
Yeah, I went back and forth on that. There are a few types like this in a PR I'm about to put up in corefx as well. I could go either way.
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.
Perhaps mark like public /* readonly */ struct Void so it's clear it was intentionally skipped.
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.
Perhaps mark like public /* readonly */ struct Void so it's clear it was intentionally skipped.
Ok, I'll do that for internal types and leave public types annotated. Seems like a reasonable place to land.
EDIT: Actually, I'll just remove them from these no-member types. We can always add them later if desired.
src/mscorlib/src/Internal/Padding.cs
Outdated
| /// <summary>Padding structure used to minimize false sharing</summary> | ||
| [StructLayout(LayoutKind.Explicit, Size = PaddingHelpers.CACHE_LINE_SIZE - sizeof(int))] | ||
| internal struct PaddingFor32 | ||
| internal readonly struct PaddingFor32 |
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.
Same here.
| [Serializable] | ||
| [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] | ||
| public partial struct DateTime : IComparable, IFormattable, IConvertible, IComparable<DateTime>, IEquatable<DateTime>, ISerializable | ||
| public readonly partial struct DateTime : IComparable, IFormattable, IConvertible, IComparable<DateTime>, IEquatable<DateTime>, ISerializable |
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.
I see that you were not able to mark DateTimeOffset because of its internal implementation detail around binary serialization. It is pretty unfortunate. I am wondering whether the DateTimeOffset case would warrant using the Unsafe helper to cast away the readonly-liness, so that DateTimeOffset can be marked as well.
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.
Exactly. I started looking at fixing it, but decided to keep this PR just focused on the easy cases where implementation changes weren't necessary. There are probably also additional structs that could be made readonly if their fields were appropriately annotated; I only looked at / fixed a few in that category (e.g. DateTime, Nullable), but didn't do a full sweep looking for others; that can be done subsequently, either piecemeal or with an analyzer.
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.
DateTimeOffset #19552
|
KeyValuePair is a good candidate to annotate. |
|
@dotnet-bot test Windows_NT x64 corefx_baseline |
I don't believe they do yet. And in my corefx change, I went through and manually propagated the visible changes to the refs.
Another case where the fields aren't currently annotated as readonly, so I didn't pick it up. I'll fix that one here, but others can be done subsequently. |
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type.
356e306 to
834d439
Compare
|
@dotnet-bot test Windows_NT x64 full_opt ryujit CoreCLR Perf Tests Correctness please |
|
@jkotas, should these CoreCLR Perf Tests Correctness legs be working? The details links just come back as 404s. |
I don't think we are currently. It should be easy enough to add for anyone that wants to verify it. The code lives in Microsoft.Cci.Extensions in Buildtools. |
|
@adiaaida I see you have pending PR to change perf related legs. Are the perf test correctness failures known issue? |
We need tracking issue for this at least. It needs to be done before we ship 2.1 to ensure consistency. |
jkotas
left a comment
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.
LGTM
|
I can't see the failure because I removed the jobs with my last change. I'll re-run the Perf Build and Test runs, which should tell us if this change will break the perf testing. @dotnet-bot test Perf Build and Test please |
VSadov
left a comment
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.
LGTM
Nice change!!!
|
@adiaaida, how long is that perf build and test leg expected to take? It's been running for almost 5 hours. Similarly in the PR at #14836. |
|
We should not block on the |
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type. Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Opened https://github.com/dotnet/corefx/issues/25039 on this |
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type. Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type.
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type. Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type. Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type. Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
| [Serializable] | ||
| [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] | ||
| public struct KeyValuePair<TKey, TValue> | ||
| public readonly struct KeyValuePair<TKey, TValue> |
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.
I'm working on the APICompat checks work but I'm not sure if this a compatible change. @jkotas @stephentoub is this an API compatible change?
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.
Marking struct as readonly should be compatible change. The other direction (removing readonly on a struct) is not a compatible change.
cc @VSadov
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.
Yes this is a compatible change.
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type. Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type. Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type. Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type. Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
In a few known "why wasn't this annotated" cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type.
Contributes to https://github.com/dotnet/corefx/issues/24900
cc: @jaredpar, @VSadov, @jkotas