I have a .NET 8 / C# application that I've compiled using AOT and ReadyToRun (R2R) options to maximize startup speed. The application deals with opening a serial port, writing/reading data from it and then closing it.
After the first compile and run, I consistently see approximately 5 seconds, which I've chalked up to the application extracting some .NET assemblies/dependencies for first run. After the first run, most runs are approximately 200 ms long, but every 4-5 invocations of the application, the runtime is closer to 800 ms. I have timestamped my code inside Main and I don't see any differences between the different invocations, so to me this looks like an issue with some startup time incurred due to .NET (re)loading some assemblies.
How can I ensure the application always consistently runs at the average ~200 ms performance instead of the random jumps to ~800 ms? Is there a way to optimize this away?
dotnet traceto see if any JIT work happens during the slower runsdotnet publish -r win-x64 -c Release), there's no dependency left (in general), so no loading of any assembly (beyond Windows native ones). Is this what you're testing? 5 seconds is a lot. Do you have a reproducing project?