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

Remove System.Linq.Expressions usages in ReflectionClrPropertyInfo #16568

Merged
merged 1 commit into from
Aug 1, 2024

Conversation

MrJul
Copy link
Member

@MrJul MrJul commented Aug 1, 2024

What does the pull request do?

This PR removes the System.Linq.Expressions usages in ReflectionClrPropertyInfo by using standard reflection instead.
With NativeAOT on win-x64, this removes 446 KB of native code.

Rationale

Expressions were probably used for performance in the past for standard property accesses, but it turns out this class is almost unused nowadays.

The only code path creating a ReflectionClrPropertyInfo is in StaticResourceExtension:

PropertyInfo pi => new Avalonia.Data.Core.ReflectionClrPropertyInfo(pi),

This is never hit by the XamlCompiler, which provides a ClrPropertyInfo directly if StaticResource is used on a standard CLR property. The only way to get through this is to pass a System.Reflection.PropertyInfo manually to StaticResourceExtension.ProvideValue.

Plus, expressions are interpreted on platform where dynamic code isn't allowed, making them slower than standard reflection in this case.

@MrJul MrJul force-pushed the feature/avoid-system-expressions branch from fc339d2 to b8fd25d Compare August 1, 2024 16:32
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0050818-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6
Copy link
Member

maxkatz6 commented Aug 1, 2024

Nice.

I was considering playing around .NET feature flags, including Trimming Definition files which can be configured to replace specific properties with const stubs.

Something like bool IsReflectionBindingEnabled => AppContext.TryGetSwitch("Avalonia.Markup.Data.IsReflectionBindingEnabled"), which then can be replaced with const "false" value, when we have AOT build with compiled bindings enabled. Or when reflection bindings were explicitly disabled by another property.

This way, whole reflection binding subsystem could be trimmed out.

See https://github.com/dotnet/runtime/blob/main/docs/workflow/trimming/ILLink-files.md#illinksubstitutionsxml

@maxkatz6 maxkatz6 added this pull request to the merge queue Aug 1, 2024
Merged via the queue into AvaloniaUI:master with commit 8493faf Aug 1, 2024
10 checks passed
@MrJul
Copy link
Member Author

MrJul commented Aug 2, 2024

Having that switch would indeed be nice.

I think we can wait for .NET 9's new feature attributes (which I saw you discussing in the past), this would make implementation easier, not even requiring the extra XML file.

@MrJul MrJul deleted the feature/avoid-system-expressions branch August 2, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants