Repository navigation
Description
Description
I have a .NET Standard 2.0 library implementing a high performance interpreter for the brainf*ck language. At the end of the day it is essentially just a Turing machine, so all the operations being performed are basically accesses to memory, incrementing values, looking up tables, etc. It's all "self-contained", with no external APIs being invoked, IO operations or anything.
I've run some benchmarks on .NET Core 2.1, .NET Core 3.1 and .NET 5 (Preview 4) and noticed that .NET 5 is consistently slower than .NET Core 3.1 in almost all cases. Especially in the mandlebrot test case, which is the most intensive, .NET 5 is almost one second slower, so about 10% slower.
I find this very surprising, as .NET 5 is supposed to be much more optimized than .NET Core 3.1.
NOTE: the solution is not open source for now, but I'd be more than happy to give access to the repo to anyone in the team wanting to take a look, just ping me and I'll send you the invite.
Pinging @EgorBo as you seemed interested in this when I shared some older benchmarks about this on Twitter (here) back when .NET 5 preview 1 had just been released, and cc. @tannergooding.
Configuration
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19619
Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.100-preview.4.20258.7
[Host] : .NET Core 5.0.0 (CoreCLR 5.0.20.25106, CoreFX 5.0.20.25106), X64 RyuJIT
.NET Core 3.1 : .NET Core 3.1.4 (CoreCLR 4.700.20.20201, CoreFX 4.700.20.22101), X64 RyuJIT
.NET Core 5.0 : .NET Core 5.0.0 (CoreCLR 5.0.20.25106, CoreFX 5.0.20.25106), X64 RyuJIT
Regression?
This is a regression in speed from .NET Core 3.1 to .NET 5 Preview 4, on the same machine.
Data
| Runtime | Name | Mean | Error | StdDev |
|---|---|---|---|---|
| .NET Core 2.1 | Division | 56.82 us | 0.810 us | 0.758 us |
| .NET Core 3.1 | Division | 41.06 us | 0.588 us | 0.521 us |
| .NET Core 5.0 | Division | 42.95 us | 0.511 us | 0.453 us |
| .NET Core 2.1 | Fibonacci | 21,298.13 us | 398.437 us | 372.698 us |
| .NET Core 3.1 | Fibonacci | 13,197.50 us | 65.613 us | 58.164 us |
| .NET Core 5.0 | Fibonacci | 15,302.14 us | 83.934 us | 78.512 us |
| .NET Core 2.1 | HelloWorld | 17.61 us | 0.296 us | 0.341 us |
| .NET Core 3.1 | HelloWorld | 16.09 us | 0.311 us | 0.394 us |
| .NET Core 5.0 | HelloWorld | 15.68 us | 0.305 us | 0.510 us |
| .NET Core 2.1 | Mandelbrot | 10,221,974.39 us | 131,372.509 us | 116,458.307 us |
| .NET Core 3.1 | Mandelbrot | 7,799,742.74 us | 65,822.677 us | 58,350.088 us |
| .NET Core 5.0 | Mandelbrot | 8,544,272.25 us | 8,136.804 us | 7,611.172 us |
| .NET Core 2.1 | Multiply | 1,204.57 us | 14.566 us | 12.912 us |
| .NET Core 3.1 | Multiply | 792.31 us | 5.899 us | 4.926 us |
| .NET Core 5.0 | Multiply | 790.11 us | 1.301 us | 1.087 us |
| .NET Core 2.1 | Sum | 52.86 us | 0.590 us | 0.493 us |
| .NET Core 3.1 | Sum | 39.53 us | 0.620 us | 0.580 us |
| .NET Core 5.0 | Sum | 37.39 us | 0.470 us | 0.417 us |
category:cq
theme:jit-block-layout
skill-level:expert
cost:large
Activity
Lxiamail commentedon May 28, 2020
@Sergio0694 thank you for reporting the issue. From your reported results, besides looks like Division, Fibonacci, and Mandelbrot may have potential regression on .NET5, the other cases have improvements. We can repro the Fibonacci regression on .NET5 and will investigate the root cause. For the other cases, we don't have repro showing regressions on .NET5. Can you share your microbenchmark tests with me so that we can do further investigation?
Sergio0694 commentedon May 28, 2020
Hey @Lxiamail - thank you for chiming in!
I'm very confused by what you said here:
What do you mean with "we can repo the Fibonacci regression"?
All these tests were done specifically with my interpreter for that language as I mentioned, that "Fibonacci" test was not just the classic Fibonacci method written in C# 🤔
Anyway, I've sent you an invite to be added as a collaborato to my private repo
Brainf_ckSharp.To repro the issue, you need to switch to the
repro/net5-regressionbranch.There you'll find the
Brainf_ckSharp.Profilerproject (in the "Profiling") folder in the solution.It's already configured to run with both .NET Core 3.1 and .NET 5.
Let me know if there's anything else I can help you with, thanks again! 😊
AndyAyersMS commentedon May 28, 2020
We got our Fibonaccis crossed. We have an internal benchmark with that name that looks like it might have regressed.
Have you done any profiling to try and pin down where things got slower?
You can add me as a collaborator.
Sergio0694 commentedon May 28, 2020
Ah, that explains it, sorry for the confusion! 😄
@AndyAyersMS I've added you to a collaborator as well!
As mentioned before, the repro is in the
repro/net5-regressionbranch, in theBrainf_ckSharp.Profilerproject (under the "Profiling" folder).I can say that 99.8% of the CPU time is spent in this function:
It's in the main
Brainf_ckSharpproject in the solution.Hope this helps, let me know if there's anything else I can do on my end!
Thank you again for your time to look into this!
Lxiamail commentedon May 28, 2020
Sorry for the typo. I meant repro. :)
AndyAyersMS commentedon May 28, 2020
Here's what I get locally for preview3 vs 3.1.4.
So Fibonacci seems to have a clear regression; the other two are less definite.
I'll also try this on my coffee lake machine with preview 4.
Sergio0694 commentedon May 28, 2020
It's really interesting that the result seems to be different compared than my notebook with the i7 8750H, and between tests as well. The code is pretty much always the same, the only difference is just the script being executed. Very curious to know what the cause of this regression is! 😊
Also if it helps, I've run the benchmark on my desktop as well:
Here it looks like both Fibonacci and Mandelbrot have a regression on .NET 5 🤔
Lxiamail commentedon May 28, 2020
v5.0 Preview4 on my desktop machine:
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.836 (1909/November2018Update/19H2)
Intel Xeon CPU E5-1650 v3 3.50GHz, 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.100-preview.4.20258.7
[Host] : .NET Core 5.0.0 (CoreCLR 5.0.20.25106, CoreFX 5.0.20.25106), X64 RyuJIT
.NET Core 3.1 : .NET Core 3.1.4 (CoreCLR 4.700.20.20201, CoreFX 4.700.20.22101), X64 RyuJIT
.NET Core 5.0 : .NET Core 5.0.0 (CoreCLR 5.0.20.25106, CoreFX 5.0.20.25106), X64 RyuJIT
Lxiamail commentedon May 28, 2020
v5.0 preview4 on my laptop:
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.836 (1909/November2018Update/19H2)
Intel Core i7-8650U CPU 1.90GHz (Kaby Lake R), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.100-preview.4.20258.7
[Host] : .NET Core 5.0.0 (CoreCLR 5.0.20.25106, CoreFX 5.0.20.25106), X64 RyuJIT
.NET Core 3.1 : .NET Core 3.1.2 (CoreCLR 4.700.20.6602, CoreFX 4.700.20.6702), X64 RyuJIT
.NET Core 5.0 : .NET Core 5.0.0 (CoreCLR 5.0.20.25106, CoreFX 5.0.20.25106), X64 RyuJIT
AndyAyersMS commentedon May 28, 2020
On a lot of these run the noise level is fairly high so getting a clear picture of what is going on may be a challenge. My kaby lake seems to have low noise levels so I'm going to start drilling in there.
12 remaining items
Sergio0694 commentedon Jul 14, 2020
Oh that makes sense, I thought that counter only referred to conditional branches, so I was confused as I didn't get why .NET 5 should've had more of them, thanks! Could this hypothesis also explain that increased number of cache misses on .NET 5? I'm thinking, if it does have more jumps, the code could generate more cache misses in the instruction cache?
I've re-run the benchmarks ading the "instructions retired" counter as you asked, and... I'm more confused than I was before 😶
I'm seeing the .NET 5 runs have 10x or less the number of intructions retired per op, I'm really not sure what to make of this 🤔
Again this is the first time I try out this sort of performance investigation, so if you have time feel free to share any thoughts or comments you might have, I'm really interested in hearing your take on all this and learning more about the topic 😊
EDIT: I've also run the same benchmarks again using the "monitoring" strategy, just to gather more info.
A few points:
I mean, I'm not sure how to interpret these results but I'm sure they might make more sense to you, hope this helps!
Sergio0694 commentedon Oct 2, 2020
Wanted to provide an update for this.
I've tested this again, this time with .NET 5 RC1, and unfortunatey it seems the regression is still there and also actually worse than before - in my Mandelbrot test case I'm now seeing a 25% performance delta between .NET Core 3.1 and .NET 5 😥
You can see the Mandelbrot test case goes from about 9.1s to over 11.7s when switching from .NET Core 3.1 to .NET 5 RC1.
All the other benchmarks got worse as well, though with less of a dramatic difference.
That might also be because they're much shorter in general, so the difference is less apparent, not sure.
Sergio0694 commentedon Oct 7, 2020
Small update - I made the repo open source, it's at https://github.com/Sergio0694/Brainf_ckSharp, so anyone can just clone it and run the benchmarks if interested. Those are in the
Brainf_ckSharp.Profilerproject, and I recommend to comment out theDebugmethod in theBrainf_ckBenchmarkBaseclass (here), as that benchmark takes a long time and it's not the one used to report the performance regression in this issue. All the previous benchmarks shown here were just for theReleasemethod in that class.EDIT: in case it's useful, I prepared a gist here with the full disassembly of the
Runmethod (the hot path for the interpreter with the regression) both with .NET Core 3.1 (done through BenchmarkDotNet with[DisassemblyDiagnoser]) and with .NET 5 (done with disasmo with a local checked build of .NET 5 from thedotnet/runtime release/5.0branch). Hope this helps! 😄AndyAyersMS commentedon Nov 9, 2020
It's possible the work @kunalspathak is doing might help by aligning of some of those internal branches.
Sergio0694 commentedon Dec 19, 2020
As mentioned to @AndyAyersMS on Discord, leaving here a repro branch and some repro steps in case it helps 🙂
dotnet-issue-36907branchBrainf_ckSharp_Net5RegressionRepro.slnffile (no UWP stuff and tests)Brainf_ckSharp.Profilerproject in theProfilingsolution directoryadnan-kamili commentedon Jan 8, 2021
After upgrading to ASP .NET 5.0 from 3.1 our Web API response time almost doubled from ~40ms to >80ms. The only change we made was that we upgraded the packages. I am surprised when the internet is flooded with posts about the improved performance of .NET 5.0, we saw a degraded performance.
danmoseley commentedon Jan 8, 2021
@adnan-kamili if I'm not mistaken your issue is not connected with this one. Could you please open an issue in dotnet/aspnetcore where they can best help you? You should not see regressed perf and we would like to figure out why.
cjlotz commentedon Jan 30, 2021
@adnan-kamili did you open an issue in dotnet/aspnetcore? if you so, can you please provide the link so that we can follow the detail. thx
adnan-kamili commentedon Feb 2, 2021
@cjlotz Our application is very big, I am not sure if it would help as we can't share any sample code to replicate the issue.
cjlotz commentedon Feb 2, 2021
@adnan-kamili I'm more interested to know whether you managed to work around the performance regression and what you did?
danmoseley commentedon Feb 3, 2021
@adnan-kamili an issue over there could still be helpful even if you can't share repro code, eg., they can ask about your configuration, what changed, what you're seeing, etc. Others may notice the same problem, and a picture emerge. Either way, it's probably not connected with the codegen issue here (although nothing's impossible 🙂
In short though -- we do'nt want 5.0 to be slower for you, we want it to be faster. So we do want to hear about it and they're the right people to help you.
adnan-kamili commentedon Feb 3, 2021
We didn't do anything to work around the performance regression. Just waiting for .NET 6.0, maybe that will improve the performance.
I have opened an issue for the same:
dotnet/aspnetcore#29866
LeszekKalibrate commentedon Mar 10, 2023
Is this fixed yet?