[Windows] Optimize LayoutHandler.Windows.cs (take 2)#26460
[Windows] Optimize LayoutHandler.Windows.cs (take 2)#26460MartyIX wants to merge 13 commits intodotnet:mainfrom
LayoutHandler.Windows.cs (take 2)#26460Conversation
mattleibow
left a comment
There was a problem hiding this comment.
This looks very nice and I do like the fact that you pointed out: we can use the better api in other places.
One thing I think we can do to force usage is to add the old children api to the banned apis file. Then no one can accidentally use it.
| { | ||
| get | ||
| { | ||
| _cachedChildren ??= Children; |
There was a problem hiding this comment.
Can we add the Children api to the banned apis?
There was a problem hiding this comment.
Modified.
However, thanks to this change, the PR exploded a bit in size. I tried to split the work in multiple commits so if necessary, it can be split in multiple PRs1.
Footnotes
-
One variant I like in case the PR is too big: Modify
src/Core/src/Handlers/Layout/LayoutHandler.Windows.csto useCachedChildren. BanChildrenand suppress errors. Successive PRs would remove suppressions and useCachedChildren. ↩
6f2f471 to
7d53052
Compare
| { | ||
| Control.Measure(availableSize); | ||
| } | ||
| Control?.Measure(availableSize); |
There was a problem hiding this comment.
Modified because MSVS shows the original if as an error.
|
|
||
| public UIElement? GeTPlatformElement() => Control; | ||
|
|
||
| UIElementCollection? _cachedChildren; |
There was a problem hiding this comment.
The parent type VisualElementHandler is in Microsoft.Maui.Controls.Handlers.Compatibility namespace. So I'm not sure if this type is to be touched or not.
| } | ||
|
|
||
| #pragma warning disable RS0030 // Do not use banned APIs; Panel.Children is banned for performance reasons. MauiPanel might not be used everywhere though. | ||
| var oldParentChildren = PlatformView.Parent is MauiPanel mauiPanel |
There was a problem hiding this comment.
The thing here is that I don't really believe that PlatformView.Parent is MauiPanel, it appears that MauiPanel is not used everytime.
So we can either
- Leave it as is
- Just suppress the error here instead of modifying code.
- Have a new type
CachedPanelthat would extendPaneland would haveCachedChildrenimplementation.
I didn't want to add excessive changes.
|
|
||
| if (window.VisualDiagnosticsOverlay != null) | ||
| window.VisualDiagnosticsOverlay.Initialize(); | ||
| window.VisualDiagnosticsOverlay?.Initialize(); |
There was a problem hiding this comment.
MSVS reported an error here
| } | ||
|
|
||
| _shadowCanvasCachedChildren = null; | ||
| _shadowCanvas = null; |
There was a problem hiding this comment.
It appears that this was missing? I checked WrapperView in Android code and it has that.
| public partial class WrapperView : Grid, IDisposable | ||
| { | ||
| Canvas? _shadowCanvas; | ||
| UIElementCollection? _shadowCanvasCachedChildren; |
There was a problem hiding this comment.
Not sure if this is not "too much".
There was a problem hiding this comment.
I don't think it's too much, if anything it's a simple example of how to use caching
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
7d53052 to
cfb3772
Compare
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
cfb3772 to
3c7c24c
Compare
|
CI uncovered a few more places requiring changes. Force-pushed. |
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
3c7c24c to
c76d0c5
Compare
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
|
It appears there is no test failure on Windows. |
| #pragma warning disable RS0030 // Do not use banned APIs; Panel.Children is banned for performance reasons. | ||
| container.Children.Add(image); | ||
| container.Children.Add(textBlock); | ||
| #pragma warning restore RS0030 // Do not use banned APIs |
There was a problem hiding this comment.
@mattleibow should we instead just not use BannedApiAnalyzer in src/Compatibility? Maybe we don't care as much about this project going forward?
| #pragma warning disable RS0030 // Do not use banned APIs; Panel.Children is banned for performance reasons. Here we allow it as a part of initialization. | ||
| layout.Children.Add(TextBlockMessage); | ||
| layout.Children.Add(TextBoxInput); | ||
| #pragma warning restore RS0030 // Do not use banned APIs |
There was a problem hiding this comment.
This one, you could have:
| #pragma warning disable RS0030 // Do not use banned APIs; Panel.Children is banned for performance reasons. Here we allow it as a part of initialization. | |
| layout.Children.Add(TextBlockMessage); | |
| layout.Children.Add(TextBoxInput); | |
| #pragma warning restore RS0030 // Do not use banned APIs | |
| #pragma warning disable RS0030 // Do not use banned APIs; Panel.Children is banned for performance reasons. Here we can just cache it. | |
| var children = layout.Children; | |
| #pragma warning restore RS0030 // Do not use banned APIs | |
| children.Add(TextBlockMessage); | |
| children.Add(TextBoxInput); |
There was a problem hiding this comment.
Addressed in the new PR here: fa2d649#diff-fe91d0630fdf7c96daa6fc8ece1b59f70527d8160730d4bfe8666a94583b038a
| #pragma warning disable RS0030 // Do not use banned APIs; Panel.Children is banned for performance reasons. In tests, we don't mind. | ||
| var windowRootViewContainerChildren = windowRootViewContainer.Children; | ||
| #pragma warning restore RS0030 // Do not use banned APIs |
There was a problem hiding this comment.
Wait, do the device tests use BannedApiAnalyzers?!? Seems like tests should not.
There was a problem hiding this comment.
This is what controls it:
Line 17 in ed53c15
Seems like $(IsTestProject) should be true for the DeviceTests. Is it?
|
Closing in favor of #26722 |
Description of Change
I've been scratching my head how to improve performance of
LayoutHandlerand it seems that storingChildrenofPlatformViewleads to a significant performance gain in my test scenario (#21787). This PR is a replacement for #26161 that implements @mattleibow's suggestion from the previous PR. Interestingly, the suggestion allows to easily useCachedChildrenin more places and thus improve performance a bit more.Explanation from @Sergio0694:
Speedscope
-> 97% improvement for
IPanelMethods.get_Children.Issues Fixed
Contributes to #21787