-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Very slow deserialization of XElement with text content using DataContractSerializer #37893
Comments
Interesting, its indeed very slow, but i don't think its caused from XElement, i did log the timing and it shows Stopwatch watch = Stopwatch.StartNew();
watch.Start();
var element = new XElement("element", string.Join("\r\n", Enumerable.Repeat(new string('a', 100), 100000)));
Console.WriteLine(watch.ElapsedMilliseconds); // 78
watch.Restart();
var serializer = new DataContractSerializer(element.GetType());
using var memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, element);
memoryStream.Seek(0, SeekOrigin.Begin);
Console.WriteLine(watch.ElapsedMilliseconds); // 146
watch.Restart();
var deserialized = (XElement)serializer.ReadObject(memoryStream);
Console.WriteLine(watch.ElapsedMilliseconds); // 30520 Event without casting to |
@buyaa-n I've profiled the code and the time is lost in |
I can understand the frustration here, and we are going to keep this bug open to look at in a future milestone. 15 minutes is a long time to wait for a serializer. However, it is true that this is not a regression from the 4.8 framework. What is probably happening here is that XElement likely already has custom serialization code to handle this case and thus can do things smartly and quickly, whereas DataContractSerializer is a general-purpose serializer that doesn't take those same shortcuts. It is almost always true that a special-purpose serializer will be faster than a general-purpose serializer for the case that it was specially designed for. But again, 15 minutes is extreme. We will try to come back to this in the future. But we are quite low on resources and cannot prioritize this at this time given that it is not a regression from previous frameworks. |
@StephenMolloy The strange thing is that it is as fast as expected when using |
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process. This process is part of our issue cleanup automation. |
Description
The following code takes about 15 minutes to run on my machine:
The equivalent code using
Save
andLoad
only takes about 200-300 ms:Going through a string using
XmlWriter
+StringWriter
+XmlReader
+StringReader
is also only 200-300 ms:Curiously, If I change the content to use
"\n"
instead of"\r\n"
as line separator, the runtime is reduced to about 15 s, which is still way too slow.Configuration
Analysis
My analysis shows, that the problem is excessive string concatenation in
XElement.AddStringSkipNotify
. The content seems to arrive in very small chunks but I don't know the reason why.The text was updated successfully, but these errors were encountered: