56014 xml writer.create perf regression (#56524)
authorSteve Molloy <smolloy@microsoft.com>
Wed, 4 Aug 2021 23:43:38 +0000 (16:43 -0700)
committerGitHub <noreply@github.com>
Wed, 4 Aug 2021 23:43:38 +0000 (16:43 -0700)
* Use non-indented XmlWriter to avoid perf hit.
* Add test for issue #46974 which was also fixed by original PR that caused this regression.

src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs
src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.RuntimeOnly.cs
src/libraries/System.Runtime.Serialization.Xml/tests/SerializationTypes.RuntimeOnly.cs

index de4eb51..3c36a83 100644 (file)
@@ -324,7 +324,7 @@ namespace System.Xml.Serialization
         [RequiresUnreferencedCode(TrimSerializationWarning)]
         public void Serialize(TextWriter textWriter, object? o, XmlSerializerNamespaces? namespaces)
         {
-            XmlWriter xmlWriter = XmlWriter.Create(textWriter, new XmlWriterSettings() { Indent = true });
+            XmlWriter xmlWriter = XmlWriter.Create(textWriter);
             Serialize(xmlWriter, o, namespaces);
         }
 
@@ -337,7 +337,7 @@ namespace System.Xml.Serialization
         [RequiresUnreferencedCode(TrimSerializationWarning)]
         public void Serialize(Stream stream, object? o, XmlSerializerNamespaces? namespaces)
         {
-            XmlWriter xmlWriter = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true });
+            XmlWriter xmlWriter = XmlWriter.Create(stream);
             Serialize(xmlWriter, o, namespaces);
         }
 
index c81f2df..b12703d 100644 (file)
@@ -503,6 +503,21 @@ public static partial class XmlSerializerTests
     }
 
     [Fact]
+    public static void Xml_WithXElementWithEmptyNestedElement()
+    {
+        var original = new WithXmlElement(true);
+        original.xml.InnerXml = "<empty></empty>";
+
+        MemoryStream ms = new MemoryStream();
+        new XmlSerializer(typeof(WithXmlElement)).Serialize(ms, original);
+
+        ms.Position = 0;
+        StreamReader sr = new StreamReader(ms);
+        string output = sr.ReadToEnd();
+        Assert.Contains("<empty></empty>", output);   // Self-closed, or completely empty is OK. No added space.
+    }
+
+    [Fact]
     public static void Xml_WithArrayOfXElement()
     {
         var original = new WithArrayOfXElement(true);
index a7cc974..95d51d4 100644 (file)
@@ -767,6 +767,19 @@ namespace SerializationTypes
         }
     }
 
+    public class WithXmlElement
+    {
+        public XmlElement xml;
+
+        public WithXmlElement() { }
+
+        public WithXmlElement(bool init)
+        {
+            var doc = new XmlDocument();
+            xml = doc.CreateElement("Element1");
+        }
+    }
+
     public class WithXElementWithNestedXElement
     {
         public XElement e1;