Repository navigation
Description
Description
In adding support for .NET framework 4.8 to a library I maintain I was surprised to find that it was benchmarking significantly faster than the same code running on .NET 6.0. I narrowed the issue down to what appears to be a performance regression in XmlTextReaderImpl.Read() in .NET 6.0 specifically. The library implements a data reader for .xlsx files, and so requires unzipping and reading xml. I'd created baseline benchmarks that decompress and read the xml nodes to understand where my performance floor was:
[Benchmark]
public void ZipXml()
{
using var stream = File.OpenRead(file);
var zipArchive = new ZipArchive(stream);
var sheetEntry = zipArchive.GetEntry("xl/worksheets/sheet1.xml");
using var sheetStream = sheetEntry.Open();
var r = XmlReader.Create(sheetStream);
while (r.Read()) ;
}
[Benchmark]
public void Zip()
{
using var stream = File.OpenRead(file);
var zipArchive = new ZipArchive(stream);
var sheetEntry = zipArchive.GetEntry("xl/worksheets/sheet1.xml");
using var sheetStream = sheetEntry.Open();
byte[] buffer = new byte[0x10000];
while (sheetStream.Read(buffer, 0, buffer.Length) != 0) ;
}
The exact file I use is the .xlsx file here.
The benchmark results of running these on my machine targeting various framework versions:
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19044.1266 (21H2)
Intel Core i7-7700K CPU 4.20GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
| framework | name | time | allocated |
|---|---|---|---|
| netcoreapp3.1 | ZipXml | 316.75 ms | 248 KB |
| netcoreapp3.1 | Zip | 48.83 ms | 84 KB |
| net5.0 | ZipXml | 304.80 ms | 248 KB |
| net5.0 | Zip | 51.62 ms | 84 KB |
| net6.0 | ZipXml | 573.91 ms | 244 KB |
| net6.0 | Zip | 48.50 ms | 84 KB |
| net48 | ZipXml | 287.37 ms | 272 KB |
| net48 | Zip | 61.19 ms | 87 KB |
The ZipXml for net6.0 is an obvious outlier, almost twice as slow as the other framework versions. All of these are running x64, benchmark.net reporting X64 RyuJIT. I specifically call out XmlTextReaderImpl because that is the concrete implementation of Read that is being called on net6.0. No idea if that's true for other frameworks or not.
I have .NET 6.0 version 6.0.100-rc.1.21458.32 currently installed.
Hats off to net4.8, way to go champ.
area-System.Xml
Activity
filipnavara commentedon Oct 10, 2021
This is very typical but the culprit is not the XML reader but the ZIP decompression. It doesn't have the same internal buffering that it had on .NET Framework. Try wrapping the
sheetStreaminBufferedStreamto see how it behaves. We observed the same thing with code in ML.NET.MarkPflug commentedon Oct 10, 2021
@filipnavara Good thought, but that doesn't seem to be related. I added two more benchmarks. The first was to explore your
BufferedStreamsuggestion. The second was to remove the compression from the equation and read directly from the sheet1.xml.Running on .NET 6.0:
Same benchmark running against .NET 5.0:
I tried other combinations of constructing the
XmlReaderwith a FileStream with buffersize = 1 (which as I understand, disables buffering) with a 64k buffer, with aStreamReaderwith different buffer sizes. Nothing seems to make much of a difference. I'm happy to be wrong, but it really looks like the culprit remains in theXmlReader.Readmethod.filipnavara commentedon Oct 10, 2021
Can you share the source code of the benchmark please?(Sorry, I accidentally scrolled over the part where you posted it.) For the record, this is the issue a member of our team filed for ML.NET. It is nearly identical scenario of XML within a ZIP file.MarkPflug commentedon Oct 10, 2021
To clarify one thing: this issue exhibits between .NET 5.0 and .NET 6.0. My mention of .NET framework was only because that is what brought it to my attention.
XmlReader.Readis the core XML API for .NET so I assume it should be considered performance-critical. This performance regression can also be seen in APIs such asXmlDocument.Loadfor example, which for the same file goes from 1,649ms on .NET 5.0 to 1,893ms on .NET 6.0.MarkPflug commentedon Oct 10, 2021
Looking at the change history on XmlTextReaderImpl, there appear to be two significant changes in the last year (roughly since 5.0). Both were to cleanup unsafe code.
The first one appears to remove unsafe code from the
XmlCharType, which is used to categorize characters for the parser. I could easily see this one causing a performance regression, as it seems to be a core part of the parser. However, it looks like great care was taken to preserve the performance of the original.f1f7681
The "unsafe" code:
Was replaced with "Unsafe" code:
I think this "Unsafe" version is attempting to avoid the bounds check as that of the original pointer version.
This second change removes unsafe code from
AdjustLineInfo.9971745
It doesn't appear to make any great effort to avoid bounds checks, but I don't know how important that is, as I don't know how often it's called. If it's called frequently, perhaps its enough to show the performance degredation?
krwq commentedon Oct 11, 2021
@MarkPflug can you try following:
new MemoryStream(File.ReadAllBytes(...)))if that will still produce the same bad results I'll take a look and possibly revert that change. I can't promise it will make it to 6.0
MarkPflug commentedon Oct 11, 2021
@krwq I added two more benchmarks, one with MemoryStream as you requested, and one that uses a StringReader in an attempt to remove yet another layer of IO abstraction:
This doesn't change the performance significantly. Results:
krwq commentedon Oct 11, 2021
@MarkPflug I'm seeing that when they did #41756 they've run some benchmarks and there wasn't any significant difference. I'm curious, could you try going back with git into state where this PR was merged and run the benchmark against your XML file? We should figure out if there was some other change which could have regressed this or if there is something specific in your XML which they've missed out when testing. Either way we should permanently add this benchmark and improve this back
MarkPflug commentedon Oct 11, 2021
Uh, I've never built the runtime before, so I don't really know what I need to do. I pulled the repo and ran build.cmd. This took quite a while, but seemed to finish successfully. I then checked out a commit prior to the the #41756 version and ran build.cmd again. This time it failed with:
EXEC : error : DIA SDK is missing at "C:\Program Files\Microsoft Visual Studio\2022\Preview\DIA SDK". Did you install all the requirements for building on Windows, including the "Desktop Development with C++" workload?
I don't really think I'm going to be able to diagnose this.
eiriktsarpalis commentedon Oct 12, 2021
@MarkPflug please see https://github.com/dotnet/runtime/blob/main/docs/workflow/requirements/windows-requirements.md for windows build prerequisites and instructions to build.
bgrainger commentedon Nov 26, 2021
I'm not able to reproduce this regression on my machine; in fact, .NET 6.0 is fastest.
MarkPflug commentedon Nov 26, 2021
@bgrainger is correct, the issue doesn't exist in 6.0. I figured out why my measurements were wrong: "COMPlus_TieredCompilation" was set to "0" on my machine. I don't remember ever setting that, but I guess I must have. It seems that this particular xml code path is greatly affected by that setting in 6.0, whereas it wasn't in previous versions. Anyway, deleting that env var fixes this "regression". Whoops, sorry for wasting everyone's time.