2

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?

7
  • 1
    Try using dotnet trace to see if any JIT work happens during the slower runs Commented Oct 10, 2024 at 7:12
  • 2
    Could also be an OS problem, like a anti-virus program, a lack of free RAM, etc. Commented Oct 10, 2024 at 11:36
  • 1
    There can be many dozens of background processes on a modern computer, any of which could be active or sleeping. Or even major programs (Outlook syncing?). Don't see how to get "consistent runs at the average ~200ms performance" in such an environment, you'd need dedicated hardware with no active 'distractions'. Commented Oct 14, 2024 at 22:17
  • 3
    With a "standard" .NET 8 program published as AOT (like dotnet 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? Commented Oct 15, 2024 at 7:04
  • 1
    Maybe some large configuration files are parsed/evaluated on startup? Commented Oct 16, 2024 at 18:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.