Repository navigation
Description
Summary
Element.OnDescendantAdded and OnDescendantRemoved allocate a new ElementEventArgs at every level of the tree as the event propagates up to the root. The args are immutable and always wrap the same child element — they should be created once and passed through the recursion.
Current behavior
When a child is added, OnDescendantAdded recurses up the parent chain, creating a new ElementEventArgs(child) at each level:
maui/src/Controls/src/Core/Element/Element.cs
Lines 955 to 959 in 2c26b34
| void OnDescendantAdded(Element child) | |
| { | |
| DescendantAdded?.Invoke(this, new ElementEventArgs(child)); | |
| RealParent?.OnDescendantAdded(child); | |
| } |
The same for removal:
maui/src/Controls/src/Core/Element/Element.cs
Lines 961 to 965 in 2c26b34
| void OnDescendantRemoved(Element child) | |
| { | |
| DescendantRemoved?.Invoke(this, new ElementEventArgs(child)); | |
| RealParent?.OnDescendantRemoved(child); | |
| } |
OnChildAdded also calls OnDescendantAdded in a loop for each descendant, so adding a subtree of N elements to a depth-D tree creates N × D ElementEventArgs allocations:
maui/src/Controls/src/Core/Element/Element.cs
Lines 644 to 646 in 2c26b34
| OnDescendantAdded(child); | |
| foreach (Element element in child.Descendants()) | |
| OnDescendantAdded(element); |
In a complex layout with a depth-20 tree, adding a single child creates 20 identical ElementEventArgs objects. Adding a subtree of 10 elements = 200 allocations.
Proposed change
Create the args once and pass them through the recursive chain:
void OnDescendantAdded(Element child)
{
var args = new ElementEventArgs(child);
OnDescendantAddedCore(args);
}
void OnDescendantAddedCore(ElementEventArgs args)
{
DescendantAdded?.Invoke(this, args);
RealParent?.OnDescendantAddedCore(args);
}Same pattern for OnDescendantRemoved. In OnChildAdded, the loop calling OnDescendantAdded for each descendant should also use the core method to reuse args per-child.
Benchmark
Tree propagation benchmark at depth 5, 10, and 20:
| Method | TreeDepth | Mean | Gen0 | Allocated | Alloc Ratio |
|------------------------ |---------- |----------:|-------:|----------:|------------:|
| Current_NewArgsPerLevel | 5 | 24.65 ns | 0.0229 | 144 B | 1.00 |
| Optimized_ReuseArgs | 5 | 11.24 ns | 0.0076 | 48 B | 0.33 |
| Current_NewArgsPerLevel | 10 | 51.53 ns | 0.0421 | 264 B | 1.00 |
| Optimized_ReuseArgs | 10 | 15.28 ns | 0.0076 | 48 B | 0.18 |
| Current_NewArgsPerLevel | 20 | 108.89 ns | 0.0802 | 504 B | 1.00 |
| Optimized_ReuseArgs | 20 | 19.88 ns | 0.0076 | 48 B | 0.10 |
At depth 20: 5.5x faster, 90% fewer allocations.
Benchmark source code
[MemoryDiagnoser]
public class Bench03_ElementEventArgsTreePropagation
{
private sealed class ElementEventArgs(object child) : EventArgs
{
public object Child { get; } = child;
}
private sealed class FakeElement
{
public FakeElement? Parent;
public event EventHandler<ElementEventArgs>? DescendantAdded;
// Current MAUI pattern: allocates new args at each level
public void OnDescendantAdded_Current(object child)
{
DescendantAdded?.Invoke(this, new ElementEventArgs(child));
Parent?.OnDescendantAdded_Current(child);
}
// Optimized: reuse same args instance up the chain
public void OnDescendantAdded_Optimized(object child)
{
var args = new ElementEventArgs(child);
OnDescendantAdded_Optimized_Inner(args);
}
private void OnDescendantAdded_Optimized_Inner(ElementEventArgs args)
{
DescendantAdded?.Invoke(this, args);
Parent?.OnDescendantAdded_Optimized_Inner(args);
}
}
[Params(5, 10, 20)]
public int TreeDepth;
[GlobalSetup]
public void Setup()
{
_root = new FakeElement();
_root.DescendantAdded += (s, e) => { };
var current = _root;
for (int i = 1; i < TreeDepth; i++)
{
var child = new FakeElement { Parent = current };
child.DescendantAdded += (s, e) => { };
current = child;
}
_leaf = current;
}
[Benchmark(Baseline = true)]
public void Current_NewArgsPerLevel()
{
_leaf.OnDescendantAdded_Current(new object());
}
[Benchmark]
public void Optimized_ReuseArgs()
{
_leaf.OnDescendantAdded_Optimized(new object());
}
}
Activity
Reuse ElementEventArgs in OnDescendantAdded/Removed tree propagation
Reuse ElementEventArgs in OnDescendantAdded/Removed tree propagation
Reuse ElementEventArgs in OnDescendantAdded/Removed tree propagation
Reuse ElementEventArgs in OnDescendantAdded/Removed tree propagation
Reuse ElementEventArgs in OnDescendantAdded/Removed tree propagation
Reuse ElementEventArgs in OnDescendantAdded/Removed tree propagation
[Perf] Reuse ElementEventArgs in tree propagation (#34134)