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

Mark some structs as byreflike #104870

Closed
wants to merge 3 commits into from
Closed

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Jul 14, 2024

Just a CI codegen test

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 14, 2024
@EgorBo
Copy link
Member Author

EgorBo commented Jul 14, 2024

@MihuBot

@@ -654,7 +654,7 @@ internal unsafe bool WriteEvent(ref EventDescriptor eventDescriptor, IntPtr even
}

/// <summary>Workaround for inability to stackalloc object[EtwAPIMaxRefObjCount == 8].</summary>
private struct EightObjects
private ref struct EightObjects
{
internal object? _arg0;
Copy link
Member

Choose a reason for hiding this comment

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

This should be InlineArray. Can InlineArrays be ref structs?

Copy link
Contributor

@hez2010 hez2010 Jul 14, 2024

Choose a reason for hiding this comment

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

It would result in a warning:

warning CS9184: 'Inline arrays' language feature is not supported for an inline array type that is not valid as a type argument, or has element type that is not valid as a type argument.

IMO this diagnostic message is out-of-date as in C# 13 we have added the support for byref-like type arguments. /cc: @jaredpar

Copy link
Member

Choose a reason for hiding this comment

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

@AaronRobinsonMSFT is this supported in the runtime?

Copy link
Member

Choose a reason for hiding this comment

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

This should be InlineArray. Can InlineArrays be ref structs?

Yes.

@AaronRobinsonMSFT is this supported in the runtime?

As in, can ref structs be marked as InlineArray or something else? If the former, see above.

Copy link
Member Author

Choose a reason for hiding this comment

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

Applied InlineArray with #pragma warning disable CS9184

Copy link
Member

Choose a reason for hiding this comment

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

As in, can ref structs be marked as InlineArray or something else?

I mean in the runtime. Basically want to make sure it's fully supported there before we look into potentially changing the language too.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, applying InlineArray to a ByRefLike type is a supported scenario from the runtime perspective.

@EgorBo EgorBo marked this pull request as ready for review July 15, 2024 10:15
@EgorBo
Copy link
Member Author

EgorBo commented Jul 15, 2024

@jkotas can we merge as is then? I couldn't find many good candidates with codegen improvements (I wrote an adhoc roslyn analyzer to find them), diffs: MihuBot/runtime-utils#525 removed a few write barriers

@@ -2258,7 +2258,7 @@ public static bool TrueForAll<T>(T[] array, Predicate<T> match)
0X7FFFFFC7;

// Private value type used by the Sort methods.
private readonly struct SorterObjectArray
private readonly ref struct SorterObjectArray
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth adding a comment about why ref exists, here and elsewhere? e.g.

Suggested change
private readonly ref struct SorterObjectArray
private readonly ref struct SorterObjectArray // ref isn't functionally necessary but enables additional JIT optimization

It's a little unfortunate we're going to start to see this sprinkled around. Is there anything more we could do in the JIT to effectively infer it?

Copy link
Member Author

@EgorBo EgorBo Jul 15, 2024

Choose a reason for hiding this comment

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

Yeah a comment makes sense.

It's a little unfortunate we're going to start to see this sprinkled around.

I don't see a difference with other things we sprinkle around like marking classes as sealed, etc 🤷

Is there anything more we could do in the JIT to effectively infer it?

Only NativeAOT might I guess..

Copy link
Member

@stephentoub stephentoub Jul 15, 2024

Choose a reason for hiding this comment

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

I don't see a difference with other things we sprinkle around like marking classes as sealed, etc 🤷

Sealed says "this type wasn't designed to be derived from, please think really hard before you remove the sealed as usage might not expect it"; it has use separate from the perf angle, in fact most of the perf angle came long after it was introduced. Making these types ref doesn't IMO have any such design benefit; it's being done solely to get a JIT optimization to kick in. You typically only make a type ref out of necessity because it needs to contain something ref.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, you're right - I don't have a strong opinion on this, I was just testing my script and hoped to see some nice codegen diffs, but they're quite small

@EgorBo EgorBo closed this Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants