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

Add trimming annotations to DotNetObjectReference #41610

Merged
merged 5 commits into from
May 20, 2022

Conversation

eerhardt
Copy link
Member

DotNetDispatcher has some unbounded reflection against DotNetObjectReference Types that the trimmer can't understand. Adding trimming annotations to tell the trimmer to preserve the public methods on the TValue of DotNetObjectReference instances, so the JSInvokable methods won't be trimmed.

See dotnet/maui#6965

cc @vitek-karas

eerhardt added 4 commits May 10, 2022 11:39
DotNetDispatcher has some unbounded reflection against DotNetObjectReference Types that the trimmer can't understand. Adding trimming annotations to tell the trimmer to preserve the public methods on the TValue of DotNetObjectReference instances, so the JSInvokable methods won't be trimmed.

See dotnet/maui#6965
@TanayParikh TanayParikh added feature-blazor-jsinterop This issue is related to JSInterop in Blazor area-blazor Includes: Blazor, Razor Components labels May 10, 2022
@MackinnonBuck
Copy link
Member

Related: #38044

@JamesNK JamesNK mentioned this pull request May 14, 2022
@JamesNK
Copy link
Member

JamesNK commented May 14, 2022

FYI the PR that re-enables trimming analysis on the solution found a number of trimming warnings in JSInterop. Not sure if related to reported trimming related runtime errors.

#41683

@eerhardt
Copy link
Member Author

Looking through the warnings on #41683 (comment), those are different warnings. This one was being suppressed on the whole DotNetDispatcher class. I am removing the suppression with this PR.

Remove the breaking change to only allow methods on the TValue type of IDotNetObjectReference. If a caller provides a base class to the TValue of DotNetObjectReference, and a derived type as the actual instance, only the base class's methods will be preserved.

This allows for the current behavior to be preserved, at the cost of not being fully trim-compatible. In order to make it fully trim-compatible, we will need to introduce a new API that can be made trimming compatible.
Comment on lines +508 to +510
_cachedMethodsByAssembly.Clear();
_cachedMethodsByType.Clear();
_cachedConvertToTaskByType.Clear();
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to this change - but that's where I noticed this. These caches will effectively disable support for unloadable (collectible) assemblies - since they hold a string reference to the Type. Would be really nice if we didn't introduce more hurdles in this space in the new frameworks.

Copy link
Member

Choose a reason for hiding this comment

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

@vitek-karas that's fair, but at the same time its a trade-off between perf and correctness on a very concrete scenario. Do we have a design/proposal on how to deal with these two concerns that we could apply across all frameworks?

In any case, that's something we should discuss outside of this PR, but if we come up with a reasonable design for it, I think we would incorporate it.

Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

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

Looks great!

@eerhardt
Copy link
Member Author

Any more feedback here? Or is this good to merge?

@javiercn
Copy link
Member

@eerhardt I think it is good to merge. To be fair, I am not an expert in the trimming area, but as far as I understand, the changes make sense.

@MackinnonBuck MackinnonBuck merged commit e9a9108 into dotnet:main May 20, 2022
@ghost ghost added this to the 7.0-preview5 milestone May 20, 2022
@eerhardt eerhardt deleted the AnnotateDotNetObjectReference branch April 28, 2023 19:25
@amcasey amcasey mentioned this pull request Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-jsinterop This issue is related to JSInterop in Blazor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants