Question

Working on .NET gives me good performance in general for the application itself but the initial load times for my application are quite large. A lot of it goes towards loading assemblies which is difficult to gauge.

Are there any optimizations that I can apply to my assemblies to make them faster to load without involving the GAC or Ngen and those are not available for ClickOnce?

This is a WinForms 2.0 project. I can upgrade it to a 3.5 but that limit my user base. I have .NET Framework 3.5 installed on my machine.

Was it helpful?

Solution

I have created a very small .exe that shows a splash screen asap. After that I initialize everything.

The JIT-compiler loads modules that are called from the method that is being jitted. So you have to take care that the method that shows the splash screen, does not call methods in modules that you do not want loaded yet.

Example:

internal sealed class Startup {

    // Call Startup.Run() from Program.Main to start the application
    internal void Run() {
        this.ShowSplash();
        this.ContinueStartup();
    }

    private void ShowSplash() {
        _splash = new Splash();
        _splash.Show();
        Application.DoEvents();
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private void ContinueStartup() {
        // Do the startup here
    }
}

The [MethodImpl(MethodImplOptions.NoInlining)] is needed so the ContinueStartup() method does not get inlined by the jit, because that will cause the modules to be loaded too early.

OTHER TIPS

You can use ILMerge to merge all your assemblies into one file. Reading one file into memory is faster then reading 10 files. Although I doubt you will see real improvement in load speed there.

You can also remove all the references to other assemblies, load the ui to the user, and while he uses the UI (typing or something else) preload all the needed assemblies using Assembly.Load, get all the needed function pointers using reflection and store them in internal members. This way application will load much faster. But I don't think any sane person will do optimization like this.

Can you describe your how many assemblies you have ?

.NET loads the assembly on the fly when you need them. You can decide to split your app in several assemblies. Assemblies must be divided by means of: deployment strategy, security and performance. Most apps are divided by 3 layered structure (UI, business, data).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top