-Fixed performance issue serializing XML document with a large number of nodes

This commit is contained in:
James Newton-King
2014-05-09 14:47:10 +12:00
parent cf7a83e91c
commit 57bd1f7daf
12 changed files with 31878 additions and 19 deletions
@@ -1432,7 +1432,7 @@ namespace Newtonsoft.Json.Tests.Bson
} }
} }
#if !(NET20 || NET35 || NETFX_CORE) #if !(NETFX_CORE)
public void Utf8Text() public void Utf8Text()
{ {
string badText = System.IO.File.ReadAllText(@"PoisonText.txt"); string badText = System.IO.File.ReadAllText(@"PoisonText.txt");
@@ -315,6 +315,17 @@
<Install>true</Install> <Install>true</Install>
</BootstrapperPackage> </BootstrapperPackage>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="bunny_pancake.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="large_sample.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="PoisonText.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
@@ -537,10 +537,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="bunny_pancake.jpg"> <Content Include="bunny_pancake.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="large_sample.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="PoisonText.txt"> <Content Include="PoisonText.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -345,10 +345,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="bunny_pancake.jpg"> <Content Include="bunny_pancake.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="large_sample.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="PoisonText.txt"> <Content Include="PoisonText.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -342,10 +342,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="bunny_pancake.jpg"> <Content Include="bunny_pancake.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="large_sample.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="PoisonText.txt"> <Content Include="PoisonText.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -336,6 +336,9 @@
</AppxManifest> </AppxManifest>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="bunny_pancake.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Images\UnitTestLogo.png"> <Content Include="Images\UnitTestLogo.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
@@ -348,6 +351,12 @@
<Content Include="Images\UnitTestStoreLogo.png"> <Content Include="Images\UnitTestStoreLogo.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="large_sample.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="PoisonText.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.WinRT.csproj"> <ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.WinRT.csproj">
@@ -493,7 +493,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="bunny_pancake.jpg"> <Content Include="bunny_pancake.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="large_sample.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="PoisonText.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
@@ -23,6 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
#endregion #endregion
using System.Xml;
#if !(NET20 || NET35 || NETFX_CORE || PORTABLE) #if !(NET20 || NET35 || NETFX_CORE || PORTABLE)
using System; using System;
using System.Collections; using System.Collections;
@@ -180,6 +181,32 @@ namespace Newtonsoft.Json.Tests
SerializeSize(image); SerializeSize(image);
} }
#if !(PORTABLE40)
[Test]
public void ConvertXmlNode()
{
XmlDocument doc = new XmlDocument();
using (FileStream file = System.IO.File.OpenRead("large_sample.xml"))
{
doc.Load(file);
}
JsonConvert.SerializeXmlNode(doc);
}
[Test]
public void ConvertXNode()
{
XDocument doc;
using (FileStream file = System.IO.File.OpenRead("large_sample.xml"))
{
doc = XDocument.Load(file);
}
JsonConvert.SerializeXNode(doc);
}
#endif
private T TimeOperation<T>(Func<T> operation, string name) private T TimeOperation<T>(Func<T> operation, string name)
{ {
// warm up // warm up
@@ -75,4 +75,4 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")] [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.3.17308")] [assembly: AssemblyFileVersion("6.0.3.17309")]
File diff suppressed because it is too large Load Diff
@@ -226,6 +226,7 @@ namespace Newtonsoft.Json.Converters
internal class XmlNodeWrapper : IXmlNode internal class XmlNodeWrapper : IXmlNode
{ {
private readonly XmlNode _node; private readonly XmlNode _node;
private IList<IXmlNode> _childNodes;
public XmlNodeWrapper(XmlNode node) public XmlNodeWrapper(XmlNode node)
{ {
@@ -249,7 +250,15 @@ namespace Newtonsoft.Json.Converters
public IList<IXmlNode> ChildNodes public IList<IXmlNode> ChildNodes
{ {
get { return _node.ChildNodes.Cast<XmlNode>().Select(n => WrapNode(n)).ToList(); } get
{
// childnodes is read multiple times
// cache results to prevent multiple reads which kills perf in large documents
if (_childNodes == null)
_childNodes = _node.ChildNodes.Cast<XmlNode>().Select(WrapNode).ToList();
return _childNodes;
}
} }
private IXmlNode WrapNode(XmlNode node) private IXmlNode WrapNode(XmlNode node)
@@ -257,11 +266,11 @@ namespace Newtonsoft.Json.Converters
switch (node.NodeType) switch (node.NodeType)
{ {
case XmlNodeType.Element: case XmlNodeType.Element:
return new XmlElementWrapper((XmlElement)node); return new XmlElementWrapper((XmlElement) node);
case XmlNodeType.XmlDeclaration: case XmlNodeType.XmlDeclaration:
return new XmlDeclarationWrapper((XmlDeclaration)node); return new XmlDeclarationWrapper((XmlDeclaration) node);
case XmlNodeType.DocumentType: case XmlNodeType.DocumentType:
return new XmlDocumentTypeWrapper((XmlDocumentType)node); return new XmlDocumentTypeWrapper((XmlDocumentType) node);
default: default:
return new XmlNodeWrapper(node); return new XmlNodeWrapper(node);
} }
@@ -274,7 +283,7 @@ namespace Newtonsoft.Json.Converters
if (_node.Attributes == null) if (_node.Attributes == null)
return null; return null;
return _node.Attributes.Cast<XmlAttribute>().Select(a => WrapNode(a)).ToList(); return _node.Attributes.Cast<XmlAttribute>().Select(WrapNode).ToList();
} }
} }
@@ -283,7 +292,7 @@ namespace Newtonsoft.Json.Converters
get get
{ {
XmlNode node = (_node is XmlAttribute) XmlNode node = (_node is XmlAttribute)
? ((XmlAttribute)_node).OwnerElement ? ((XmlAttribute) _node).OwnerElement
: _node.ParentNode; : _node.ParentNode;
if (node == null) if (node == null)
@@ -301,8 +310,9 @@ namespace Newtonsoft.Json.Converters
public IXmlNode AppendChild(IXmlNode newChild) public IXmlNode AppendChild(IXmlNode newChild)
{ {
XmlNodeWrapper xmlNodeWrapper = (XmlNodeWrapper)newChild; XmlNodeWrapper xmlNodeWrapper = (XmlNodeWrapper) newChild;
_node.AppendChild(xmlNodeWrapper._node); _node.AppendChild(xmlNodeWrapper._node);
_childNodes = null;
return newChild; return newChild;
} }
@@ -459,7 +469,7 @@ namespace Newtonsoft.Json.Converters
{ {
IList<IXmlNode> childNodes = base.ChildNodes; IList<IXmlNode> childNodes = base.ChildNodes;
if (Document.Declaration != null) if (Document.Declaration != null && childNodes[0].NodeType != XmlNodeType.XmlDeclaration)
childNodes.Insert(0, new XDeclarationWrapper(Document.Declaration)); childNodes.Insert(0, new XDeclarationWrapper(Document.Declaration));
return childNodes; return childNodes;
@@ -640,6 +650,8 @@ namespace Newtonsoft.Json.Converters
internal class XContainerWrapper : XObjectWrapper internal class XContainerWrapper : XObjectWrapper
{ {
private IList<IXmlNode> _childNodes;
private XContainer Container private XContainer Container
{ {
get { return (XContainer)WrappedNode; } get { return (XContainer)WrappedNode; }
@@ -652,7 +664,15 @@ namespace Newtonsoft.Json.Converters
public override IList<IXmlNode> ChildNodes public override IList<IXmlNode> ChildNodes
{ {
get { return Container.Nodes().Select(n => WrapNode(n)).ToList(); } get
{
// childnodes is read multiple times
// cache results to prevent multiple reads which kills perf in large documents
if (_childNodes == null)
_childNodes = Container.Nodes().Select(WrapNode).ToList();
return _childNodes;
}
} }
public override IXmlNode ParentNode public override IXmlNode ParentNode
@@ -691,6 +711,8 @@ namespace Newtonsoft.Json.Converters
public override IXmlNode AppendChild(IXmlNode newChild) public override IXmlNode AppendChild(IXmlNode newChild)
{ {
Container.Add(newChild.WrappedNode); Container.Add(newChild.WrappedNode);
_childNodes = null;
return newChild; return newChild;
} }
} }
@@ -91,5 +91,5 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")] [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.3.17308")] [assembly: AssemblyFileVersion("6.0.3.17309")]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]