Skip to content

Commit 1b0d89c

Browse files
authored
Reduce allocations in XmlDocumentationProvider.GetDocumentationForSymbol (#80287)
* Reduce allocations in XmlDocumentationProvider.GetDocumentationForSymbol Switches the method from XDocument usage to just using the XmlReader directly. I do notice one *slight* difference between the two, the old way ends up \n in the document to \r\n whereas the new way doesn't do that conversion. I don't *think* that should have an effect on rendering, but I thought I should mention it.
1 parent 39c5106 commit 1b0d89c

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/Workspaces/Core/Portable/Utilities/Documentation/XmlDocumentationProvider.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using System.IO;
1212
using System.Threading;
1313
using System.Xml;
14-
using System.Xml.Linq;
1514
using Roslyn.Utilities;
1615

1716
namespace Microsoft.CodeAnalysis;
@@ -55,13 +54,6 @@ public static XmlDocumentationProvider CreateFromFile(string xmlDocCommentFilePa
5554
return new FileBasedXmlDocumentationProvider(xmlDocCommentFilePath);
5655
}
5756

58-
private XDocument GetXDocument(CancellationToken cancellationToken)
59-
{
60-
using var stream = GetSourceStream(cancellationToken);
61-
using var xmlReader = XmlReader.Create(stream, s_xmlSettings);
62-
return XDocument.Load(xmlReader);
63-
}
64-
6557
protected override string GetDocumentationForSymbol(string documentationMemberID, CultureInfo preferredCulture, CancellationToken cancellationToken = default)
6658
{
6759
if (_docComments == null)
@@ -70,16 +62,26 @@ protected override string GetDocumentationForSymbol(string documentationMemberID
7062
{
7163
try
7264
{
73-
var comments = new Dictionary<string, string>();
74-
75-
var doc = GetXDocument(cancellationToken);
76-
foreach (var e in doc.Descendants("member"))
65+
using (var stream = GetSourceStream(cancellationToken))
66+
using (var xmlReader = XmlReader.Create(stream, s_xmlSettings))
7767
{
78-
if (e.Attribute("name") != null)
79-
comments[e.Attribute("name").Value] = e.ToString();
68+
var comments = new Dictionary<string, string>();
69+
while (xmlReader.Read())
70+
{
71+
if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "member")
72+
{
73+
var name = xmlReader.GetAttribute("name");
74+
if (name != null)
75+
{
76+
// Read the entire <member> element as a string
77+
var memberXml = xmlReader.ReadOuterXml();
78+
comments[name] = memberXml;
79+
}
80+
}
81+
}
82+
83+
_docComments = comments;
8084
}
81-
82-
_docComments = comments;
8385
}
8486
catch (Exception)
8587
{

0 commit comments

Comments
 (0)