Skip to content

[Perf] Reuse ElementEventArgs in OnDescendantAdded/Removed tree propagation #34093

Closed

Description

@simonrozsival

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:

void OnDescendantAdded(Element child)
{
DescendantAdded?.Invoke(this, new ElementEventArgs(child));
RealParent?.OnDescendantAdded(child);
}

The same for removal:

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:

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

  1. added
    perf/general
    on Feb 17, 2026
  2. added 2 commits that reference this issue on Feb 27, 2026
    2017c13
    bafb9b8
  3. added a commit that references this issue on Mar 11, 2026
    c9b26a2
  4. added a commit that references this issue on Mar 20, 2026
    0dbb657
  5. locked and limited conversation to collaborators on May 17, 2026
  6. added this to the .NET 11.0-preview3 milestone on Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    perf/general

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions