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

[X] Simplify OnPlatformExtension #4829

Merged
merged 1 commit into from
Mar 10, 2022
Merged

[X] Simplify OnPlatformExtension #4829

merged 1 commit into from
Mar 10, 2022

Conversation

StephaneDelcroix
Copy link
Contributor

When the platform is known at compile time, and it's known by default as
we are multitargetting, replace OnPlatfomExtension by the actual value.

This is done early in the parsing so every other optimization is
unaffected.

From a perf point of view, there are multiple gains:

  • OnPlatformExtension is executed at runtime, and makes use of
    reflection. Removing OnPlatformExtensions from the tree reduces this
    to nothing.
  • Values in OnPlatformExtension never benefitted from
    compiledtypeconverters
  • not generating the IL for the OnPlatform is huge win too. The unitest
    goes from 189 instructions to 42.

fixes #4716

Description of Change

Implements #

Additions made

  • Adds

PR Checklist

  • Targets the correct branch
  • Tests are passing (or failures are unrelated)
  • Targets a single property for a single control (or intertwined few properties)
  • Adds the property to the appropriate interface
  • Avoids any changes not essential to the handler property
  • Adds the mapping to the PropertyMapper in the handler
  • Adds the mapping method to the WinUI, Android, iOS, and Standard aspects of the handler
  • Implements the actual property updates (usually in extension methods in the Platform section of Core)
  • Tags ported renderer methods with [PortHandler]
  • Adds an example of the property to the sample project (MainPage)
  • Adds the property to the stub class
  • Implements basic property tests in DeviceTests

Does this PR touch anything that might affect accessibility?

  • Does this PR introduce a new control? (If yes, add an example using SemanticProperties to the SemanticsPage)
  • APIs that modify focusability?
  • APIs that modify any text property on a control?
  • Does this PR modify view nesting or view arrangement in anyway?
  • Is there the smallest possibility that your PR will change accessibility?
  • I'm not sure, please help me

If any of the above checkboxes apply to your PR, then the PR will need to provide testing to demonstrate that accessibility still works.

When the platform is known at compile time, and it's known by default as
we are multitargetting, replace OnPlatfomExtension by the actual value.

This is done early in the parsing so every other optimization is
unaffected.

From a perf point of view, there are multiple gains:
- OnPlatformExtension is executed at runtime, and makes use of
  reflection. Removing OnPlatformExtensions from the tree reduces this
  to nothing.
- Values in OnPlatformExtension never benefitted from
  compiledtypeconverters
- not generating the IL for the OnPlatform is huge win too. The unitest
  goes from 189 instructions to 42.

fixes #4716
@StephaneDelcroix
Copy link
Contributor Author

/cc @eerhardt

@jsuarezruiz jsuarezruiz added the area-xaml XAML, CSS, Triggers, Behaviors label Feb 22, 2022
target = nameof(OnPlatformExtension.iOS);
if (TargetFramework.Contains("-macos"))
target = nameof(OnPlatformExtension.macOS);
if (TargetFramework.Contains("-maccatalyst"))
Copy link
Member

Choose a reason for hiding this comment

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

Is there no "Windows" platform extensions? Or do we not care in that case?

Copy link
Member

Choose a reason for hiding this comment

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

This is kind of interesting since -windows could be wpf, or winforms or winappsdk in theory right? We only support winappsdk now, but who knows in the future... So perhaps target framework is not enough to check for -windows at some point...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there's no 1-1 mapping between .net TFM for windows and Maui targeted platform

Copy link
Member

Choose a reason for hiding this comment

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

What are the "platform extensions" for windows? How does that switch happen at runtime?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/dotnet/maui/blob/main/src/Controls/src/Xaml/MarkupExtensions/OnPlatformExtension.cs#L13-L21
the platform for the running app is set by the IPlatformApplication scaffolding the whole Maui thing

Copy link
Member

@Redth Redth Mar 3, 2022

Choose a reason for hiding this comment

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

#4491 added WinUI as a platform in the OnPlatform markup extension for windows...

So, today we only have 1 windows target, perhaps we should optimize for the now. If we need to, in the future could we optimize such that at least -windows only has a limited number of options to check through and perhaps on -windows it's a slower path for this xamlc, but that's probably fine anyway because it's not android.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this might be a good time to review our current platform list, and names, and what defines a platform. should iPadOS be it's own platform, what about deprecating windows in favour of winui, wpf, ...

Copy link
Member

Choose a reason for hiding this comment

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

Am I reading this correctly, in that if it's not windows, then there's just no optimization in the code path like there is for the other known tfm's? If that's the case I think this is fine as is and we should try and merge it in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, that's it, if the TFM is unlisted, we do nothing

return;

string target = null;
if (TargetFramework.Contains("-android"))
Copy link
Member

Choose a reason for hiding this comment

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

TargetFramework doesn't change for the lifetime of this object. Does it make sense to do this switch every time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

despite being in the Xaml project, this code is only ever executed by the compiler, and we're usually ok to sacrifice a few ms over code simplicity and readability. I'm 100% sure there's an alternative code that is as readable, as debuggable and as simple, but I'm not sure it will ever save the time we currently spend discussing it :)

@StephaneDelcroix
Copy link
Contributor Author

there's also a discussion gaining some traction about doing conditionals in XAML that would make OnPlatform obsolete... I'll make a proposal soon

@StephaneDelcroix StephaneDelcroix merged commit 43ca83b into main Mar 10, 2022
@StephaneDelcroix StephaneDelcroix deleted the fix_4716 branch March 10, 2022 08:02
@github-actions github-actions bot locked and limited conversation to collaborators Dec 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OnPlatform could be simplified at build time
4 participants