Entity Framework spinup much slower on x64 vs x86

2019-01-17 07:54发布

问题:

My coworker posted this question yesterday: 7-second EF startup time even for tiny DbContext.

After taking his code and moving it to a separate solution to isolate it as much as possible, I found that the containing project's platform target had a profound affect on the runtime of the EF startup process.

When targeting x64, I saw that the test took ~7 seconds to spin up the first DbContext and <1 second to spin up the second DbContext (consistent with my coworker's findings who is also targeting x64). However when I switched the platform target to x86, the first DbContext spin up time was reduced by about 4 seconds down to 3.34633 seconds while the second DbContext took the same amount of time as the x64 case.

Given this, it appears the Entity Framework is going through a much different initialization process when targeting a 64-bit system vs 32-bit system. Does anyone have any insight into what is going on under the hood to explain this?

回答1:

The issue is fully reproducible. I have just run it and used dotTrace Performance profiler to collect snapshots for both x86 and x64 executions. I got mostly same times as you report. But there is really no obvious difference between x64 and x86 traces - except the x64 takes at least twice the time of x86 everywhere.

But that was tracing of NUnit test run. By running the same test just as console applications I get times like this:

x86: 0,6864012, 0,0468001
x64: 1,0608019, 0,0468001

That looks much better, doesn't it? There is still difference between x86 and x64 but the x64 code can be slower in general for some operations.

The problem at this point is not about EF but about NUnit and its test runner.

Edit:

I did some more testing. Both NUnit's and Resharper's task runner has this issue but it affects only the very first test. All other tests run quickly. xUnit shows the same behavior.