Skip to content

XDocument.Parse / XDocument.Load take O(depth²) time: XContainer.AddNodeSkipNotify walks to the root for each added node #136658

Description

@Albasha0x0

Versions

  • .NET 10.0.14, 9.0.22, 8.0.33 (Microsoft.NETCore.App), SDK 10.0.403, Windows 11 x64: same shape on all three.
  • Source read: System.Private.Xml.Linq 10.0.14, XContainer.AddNodeSkipNotify (decompiled).

Repro

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
</Project>
using System.Diagnostics;
using System.Text;
using System.Xml;
using System.Xml.Linq;

Console.WriteLine($"runtime {Environment.Version}");
foreach (int n in new[] { 5_000, 10_000, 20_000, 40_000 })
{
    var xml = new StringBuilder().Insert(0, "<a>", n).Insert(n * 3, "</a>", n).ToString();
    var sw = Stopwatch.StartNew();
    using (var reader = XmlReader.Create(new StringReader(xml)))
        while (reader.Read()) { }
    var read = sw.Elapsed;
    sw.Restart();
    XDocument.Parse(xml);
    Console.WriteLine($"depth {n,6}: XmlReader {read.TotalMilliseconds,8:0.0} ms, XDocument.Parse {sw.Elapsed.TotalMilliseconds,9:0.0} ms");
}

dotnet build -c Release, then dotnet bin/Release/<tfm>/xdepth.dll.

Observed

Depth Input XmlReader pass XDocument.Parse 10.0.14 9.0.22 8.0.33
5,000 35 KB 2-30 ms 104 ms 119 ms 125 ms
10,000 70 KB 2.1 ms 362 ms 388 ms 737 ms
20,000 140 KB 3.7 ms 1,687 ms 1,738 ms 3,311 ms
40,000 280 KB 11.6 ms 12,593 ms 15,824 ms 16,162 ms

Doubling the depth multiplies the load time by about 4 to 7; the reader pass stays linear. A 1 MB manifest of 41,903 nested elements took 28 s of CPU in XDocument.Load(XmlReader).

Expected

Load time linear in the input, as the XmlReader pass underneath it is. The loader builds the tree itself, so the node it adds can never be an ancestor of its container.

Cause

XContainer.ReadContentFrom adds each node with AddNodeSkipNotify, which (after ValidateNode) walks from the container to the root to check that the new node is not the root:

internal void AddNodeSkipNotify(XNode n)
{
    ValidateNode(n, this);
    if (n.parent != null) n = n.CloneNode();
    else
    {
        XNode p = this;
        while (p.parent != null) p = p.parent;
        if (n == p) n = n.CloneNode();
    }
    ConvertTextToNode();
    AppendNodeSkipNotify(n);
}

At depth d the walk costs d steps, so a chain of n elements costs n²/2. The loader's new nodes have no parent and cannot be the root; the loader could append them without the walk (AppendNodeSkipNotify after the parent check).

Activity

  1. added
    untriaged
    on Oct 29, 2026
  2. dotnet-policy-service commented on Oct 29, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-xml
    See info in area-owners.md if you want to be subscribed.

  3. asad24-dev commented on Oct 30, 2026

    @asad24-dev
    Contributor

    I'm working on a fix for this. I've reproduced the issue and have a local change that removes the redundant ancestor traversal during XML loading. I'll open a PR shortly.

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

    area-System.Xmluntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions