[Perf] Reuse ElementEventArgs in tree propagation#34134
Conversation
There was a problem hiding this comment.
Pull request overview
Improves Microsoft.Maui.Controls.Element descendant add/remove event propagation performance by reusing a single ElementEventArgs instance while walking up the parent chain, instead of creating new args at each propagation step.
Changes:
- Create
ElementEventArgsonce inOnDescendantAdded(Element)/OnDescendantRemoved(Element)and pass it through a new helper overload during parent propagation. - Update propagation to call
RealParent?.OnDescendantAdded(args)/RealParent?.OnDescendantRemoved(args)to avoid repeated allocations while walking ancestors.
| void OnDescendantAdded(ElementEventArgs args) | ||
| { | ||
| DescendantAdded?.Invoke(this, args); | ||
| RealParent?.OnDescendantAdded(args); | ||
| } |
There was a problem hiding this comment.
Introducing an overload OnDescendantAdded(ElementEventArgs args) makes it easy to accidentally call the allocating OnDescendantAdded(Element child) in future edits, and it’s less discoverable that one overload is the non-allocating propagation helper. Consider renaming the helper overload to something like OnDescendantAddedCore/OnDescendantRemovedCore for clarity and consistency with other *Core helper patterns in this file.
StephaneDelcroix
left a comment
There was a problem hiding this comment.
LGTM from my review. I don't see blocking issues; this looks good to merge as-is.
|
/azp run maui-pr-uitests, maui-pr-devicetests |
|
Azure Pipelines successfully started running 2 pipeline(s). |
a4c8314 to
2017c13
Compare
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 34134Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 34134" |
2017c13 to
bafb9b8
Compare
bafb9b8 to
c9b26a2
Compare
Fixes #34093 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Defer ElementEventArgs creation until first non-null subscriber - Zero allocations when no DescendantAdded/Removed handlers exist - Rename recursive helpers to OnDescendantAddedCore/RemovedCore Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fd585e0 to
13fcf8b
Compare
|
@PureWeen @mattleibow could you please have a look at this PR and let me know what you think about it? could we merge this into net11.0 soon? |
|
/azp run maui-pr-uitests,maui-pr-devicetests |
|
Azure Pipelines successfully started running 2 pipeline(s). |
Remove '?' from ElementEventArgs parameters in OnDescendantAddedCore and OnDescendantRemovedCore. The file has '#nullable disable' at the top, so nullable reference type annotations are invalid and cause CS8632 warnings which become errors when TreatWarningsAsErrors=true (e.g. in dotnet/android CI). Introduced in #34134. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…t in Element.cs (#34751) <!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ## Description Removes `?` nullable annotations from `ElementEventArgs` parameters in `OnDescendantAddedCore` and `OnDescendantRemovedCore` in `Element.cs`. The file starts with `#nullable disable`, so nullable reference type annotations are invalid and produce **CS8632** warnings. MAUI CI sets `TreatWarningsAsErrors=false` so these pass silently, but downstream consumers like **dotnet/android** build with `TreatWarningsAsErrors=true`, causing build failures. Introduced in #34134. ## Fix Simply remove the `?` from the `ElementEventArgs` parameter types. Under `#nullable disable`, reference types are already implicitly nullable, so the behavior is unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Fixes #34093
Description
OnDescendantAddedandOnDescendantRemovedallocate a newElementEventArgsat every tree level during parent propagation. This creates O(depth) allocations per child add/remove. This PR creates theElementEventArgsonce at the entry point and passes it up through the recursive walk.