Repository navigation
Description
Description
Stream.ReadLineAsync is 6 times slower than Stream.ReadLine in .NET 6. Slightly simpler case is 24 times slower.
Reproduction Steps
Sync version
public static void Main()
{
var sw = Stopwatch.StartNew();
using (var stream = new FileStream(@"E:\Temp\file8.txt.bak", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 32 * 1024, false))
{
using (var reader = new StreamReader(stream))
{
while (true)
{
var read = reader.ReadLine();
if (read == null) break;
}
}
}
Console.WriteLine(sw.Elapsed);
}
Async version:
public static async Task Main()
{
var sw = Stopwatch.StartNew();
using (var stream = new FileStream(@"E:\Temp\file8.txt.bak", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 32 * 1024, true))
{
using (var reader = new StreamReader(stream))
{
while (true)
{
var read = await reader.ReadLineAsync();
if (read == null) break;
}
}
}
Console.WriteLine(sw.Elapsed);
}
Sync version finishes in 11 seconds, Async finishes in 1 minute and 5 seconds
And we take this shorter sync version:
public static void Main()
{
var sw = Stopwatch.StartNew();
using (var reader = new StreamReader(@"E:\Temp\file8.txt.bak"))
{
while (true)
{
var read = reader.ReadLine();
if (read == null) break;
}
}
Console.WriteLine(sw.Elapsed);
}
and async version:
public static async Task Main()
{
var sw = Stopwatch.StartNew();
using (var reader = new StreamReader(@"E:\Temp\file8.txt.bak"))
{
while (true)
{
var read = await reader.ReadLineAsync();
if (read == null) break;
}
}
Console.WriteLine(sw.Elapsed);
}
Sync version took 17 seconds. Async version took 5 minutes and 35 seconds. It is 24 times slower.
Expected behavior
I believe that async version should be slower than sync but not by that much. Especially after I read that there is a lot of improvements of async FileStream in .NET 6
Actual behavior
Stream.ReadLineAsync is 6 times slower than Stream.ReadLine
Regression?
No response
Known Workarounds
No response
Configuration
.NET 6
Windows 10, Pro, x64
x86
Intel 9900K, 32GB RAM
File E:\Temp\file8.txt.bak located on HDD and is 8Gb. It contains only English alpha numeric characters, spaces and periods.
For example: 4815995. Something something something
Other information
I put it as bug report because a lot of developers are really sure that this is perfect case for async and as result they are using async in this case, making code quite slow. It also forces us in some cases using sync version which does not feel right.
Activity
huoyaoyuan commentedon Nov 26, 2021
Additional overhead may be included in your measurement, for example initializing the thread pool. You should use BenchmarkDotNet to get a more reliable measurement. Async version is expected to be slightly slower.
zlatanov commentedon Nov 26, 2021
@vladimir-cheverdyuk-altium can you make sure you're running your app in release. In
DEBUGbuilds, async code and state machines don't generate optimal code.The above code executed with
dotnet run -c releaseoutputs:@huoyaoyuan although
FileStreamhas been rewritten, the implementation ofStreamReaderhasn't changed much. Async methods can be optimized to avoid async state machines when the task based API complete synchronously.vladimir-cheverdyuk-altium commentedon Nov 26, 2021
@zlatanov, I re-run my tests as you suggested. First case is 11 seconds vs 20. Second simpler case is 14 seconds vs 30. Still about 2 times difference. It is much better, but I expected maybe 10-20% difference due to more complex code in async version.
Sorry, I did testing from VS 2022 in Release configuration. I know that debugger affects performance but I expected that both versions would be affected similarly I didn't expect that debugger affects async version that much :(
adamsitnik commentedon Nov 29, 2021
That is why we encourage our users to use BenchmarkDotNet for benchmarking (one of the things it does it enforces running in Release).
I've written a following benchmark based on the source code provided by @zlatanov :
And run it for .NET 5 and 6 using the following command:
And got the following results:
My observations:
I had a quick look at the implementation and there is definitely place for improvement. I am going to change the issue title (there is no such thing as Stream.ReadLineAsync) and it's 2 (not 6) times slower and make it up-for-grabs.
[-]Stream.ReadLineAsync is 6 times slower than Stream.ReadLine in .NET 6[/-][+]StreamReader.ReadLineAsync performance can be improved[/+]5 remaining items
adamsitnik commentedon Dec 6, 2021
I want to work on that issue and improve the performance. Haven't commited to the runtime so I don't really know the rules, going to look to the docs first and then profile it.
@Trapov great! I've assigned you to the issue.
You may find the following docs useful:
Trapov commentedon Dec 7, 2021
@adamsitnik Can I reuse the same benchmark that you posted above in the performance repo?
adamsitnik commentedon Dec 7, 2021
@Trapov sure!
Trapov commentedon Dec 7, 2021
Before:
After:
I think I could squeeze out more. @adamsitnik
adamsitnik commentedon Dec 8, 2021
@Trapov impressive!
Trapov commentedon Dec 8, 2021
#62552
vladimir-cheverdyuk-altium commentedon Dec 8, 2021
May I ask why there is so big difference when I ran these tests from Visual Studio? After all, most development is happened in Visual Studio and somebody could replace sync code with async, will get so awful performance and will decide that it is not worth it and will revert back to sync version.
And even if somebody will do proper measurement and keep async version, then that person will have to wait much longer every time that code runs in Visual Studio.
Perhaps something can be done about it?
stephentoub commentedon Dec 8, 2021
With or without the debugger attached? A debugger attached makes lots of things slower, in particular related to threading.
vladimir-cheverdyuk-altium commentedon Dec 8, 2021
With debugger attached. I understand that performance will be slower but in this case it is up to 12 times slower. Is it reasonable?
tymtam2 commentedon Jan 28, 2023
Results from .NET7 using the benchmark above.
stephentoub commentedon Jan 28, 2023
#69888 was merged for .NET 8.