You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
XDocument.Parse / XDocument.Load take O(depth²) time: XContainer.AddNodeSkipNotify walks to the root for each added node #136658
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:
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).
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.
Activity
dotnet-policy-service commentedon Oct 29, 2026
Tagging subscribers to this area: @dotnet/area-system-xml
See info in area-owners.md if you want to be subscribed.
docs(dotnet): cite the upstream issue for the XML nesting bound
asad24-dev commentedon Oct 30, 2026
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.