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

Enhance NestedTypes in DynamicallyAccessedMemberTypes #2133

Merged
merged 1 commit into from
Jul 8, 2021

Conversation

LakshanF
Copy link
Contributor

@LakshanF LakshanF commented Jul 6, 2021

We don't do much for nested type options in DynamicallyAccessedMemberTypes, forcing the much broader scope All to be used if we want to preserve members inside nested types. This fix is to keep all the members for nested types.

This impacts EventSource since the type is tagged currently with All and there is a related discussion around size. Unfortunately, EventSource needs to preserve its members as well and there does not seem to be much size saving on EventSource with this change.

Fixes #1174

@LakshanF LakshanF requested a review from marek-safar as a code owner July 6, 2021 14:16
@LakshanF LakshanF requested a review from sbomer July 6, 2021 17:45
@sbomer
Copy link
Member

sbomer commented Jul 6, 2021

Should this really keep everything on the nested types? I would not expect PublicNestedTypes to recursively keep private nested types for example.

@LakshanF
Copy link
Contributor Author

LakshanF commented Jul 6, 2021

I was primarily going with the discussion on the issue for the fix from @MichalStrehovsky and the view that there are not wide usage of DAM with NestedTypes in the library

I looked into this change since I was curious if this change would have an impact on the library size due to EventSource proliferations in our library but the size wins are minimal since we need to keep most of parent type members. But created the PR since the consensus in the issue was to fix it.

The trimmer is not able to reason on what is needed for internal types that are decorated with DAM in the library mode since the programming paradigm relies on reflection in application runtime to enable the event source. I think the solution is for custom library source code such as NetEventSource to be refactored to use only what is needed instead of the current model. The runtime issue tracks this but that work might not happen in this release.

Copy link
Member

@vitek-karas vitek-karas left a comment

Choose a reason for hiding this comment

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

Assuming we agree on the behavior (as per the discussion on the issue) the change looks good.
This should be followed by changing the intrinsic behavior of GetNestedType which currently only propagates the All annotation to the return value if the source type was annotated with All. With this change, it can now propagate All always. In fact we may want to change the runtime and add the annotation on the return value directly.

Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

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

Looks good modulo Vitek's comment about the GetNestedType intrinsic.

Sven is right that we might be able to not mark non-public nested types in the deeper levels, but I'm not sure if it will result in savings in practice and it looks like it would complicate the implementation quite a bit.

class Foo
{
    public class PublicNestedAndReflected
    {
        protected class ProtectedNestedButNotReflected { }
    }
}

If we ask for all public nested types on Foo, we might be able to get rid of ProtectedNestedButNotReflected. But I think it's very unlikely that a fully-preserved PublicNestedType would have no references to ProtectedNestedButNotReflected (not many other classes have access to this nested class since it's not public - chances are pretty high we would end up keeping it anyway).

@@ -313,7 +319,9 @@ private void PrivateMethod ()
public static class PublicNestedType
{
// PublicNestedType should be kept but linker won't mark anything besides the declaration
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: We should delete these comments in our testing given that now the link will mark more than the declaration

@LakshanF LakshanF merged commit a80487e into dotnet:main Jul 8, 2021
@LakshanF LakshanF deleted the NestedTypeChange branch July 8, 2021 16:30
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
MichalStrehovsky added a commit to MichalStrehovsky/runtime that referenced this pull request Nov 14, 2024
dotnet/linker#2133 adjusted rooting logic to keep `.All` on all nested types preserved with PublicNestedTypes/NonPublicNestedTypes. The PR resolved the bug and the analysis work that was identified in the review as necessary never happened. So we just uselessly preserve members.

This adds dataflow analysis to propagate this. Since we never had the dataflow analysis, I'm exposing this not as .All, but a subset of .All that doesn't do the worst part - doesn't require reflectability of interface methods.
MichalStrehovsky added a commit to dotnet/runtime that referenced this pull request Nov 20, 2024
dotnet/linker#2133 adjusted rooting logic to keep `.All` on all nested types preserved with PublicNestedTypes/NonPublicNestedTypes. The PR resolved the bug and the analysis work that was identified in the review as necessary never happened. So we just uselessly preserve members.

This adds dataflow analysis to propagate this. Since we never had the dataflow analysis, I'm exposing this not as .All, but a subset of .All that doesn't do the worst part - doesn't require reflectability of interface methods.
mikelle-rogers pushed a commit to mikelle-rogers/runtime that referenced this pull request Dec 10, 2024
dotnet/linker#2133 adjusted rooting logic to keep `.All` on all nested types preserved with PublicNestedTypes/NonPublicNestedTypes. The PR resolved the bug and the analysis work that was identified in the review as necessary never happened. So we just uselessly preserve members.

This adds dataflow analysis to propagate this. Since we never had the dataflow analysis, I'm exposing this not as .All, but a subset of .All that doesn't do the worst part - doesn't require reflectability of interface methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DynamicallyAccessMemberType.PublicNestedTypes behavior discussion
5 participants