Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Build/Utilities/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,21 @@ internal static string GetXmlNodeInnerContents(XmlElementWithLocation node)

// XmlNode.InnerXml is much more expensive than InnerText. Don't use it for trivial cases.
// (single child node with a trivial value or no child nodes)
if (!node.HasChildNodes || (node.ChildNodes.Count == 1 && node.FirstChild.NodeType == XmlNodeType.Whitespace))
// Avoid accessing node.ChildNodes -- it allocates a new XmlChildNodes wrapper on every call.
// Use FirstChild/NextSibling linked-list traversal instead (allocation-free).
XmlNode firstChild = node.FirstChild;
if (firstChild is null)
{
return String.Empty;
}

if (node.ChildNodes.Count == 1 && (node.FirstChild.NodeType == XmlNodeType.Text || node.FirstChild.NodeType == XmlNodeType.CDATA))
bool isSingleChild = firstChild.NextSibling is null;
if (isSingleChild && firstChild.NodeType == XmlNodeType.Whitespace)
{
return String.Empty;
}

if (isSingleChild && (firstChild.NodeType == XmlNodeType.Text || firstChild.NodeType == XmlNodeType.CDATA))
{
return node.InnerText;
}
Expand Down