WPF warm AppDomain startup performance (Application.RunInternal, XamlReader.LoadBaml)

℡╲_俬逩灬. 提交于 2019-12-10 10:44:39

问题


I have relatively simple application, but warm (second, etc.) start-up time is awful 3-5 seconds. Profiler (VS2010, CPU Sampling) shows that more than 80% of time is spent in Application.RunInternal (~40%) and XamlRader.LoadBaml (~40%) functions.

The root of the problem is that Window is created in non-default AppDomain. If I move Window creation to default AppDomain or give AppDomain unrestricted permission set everything is as fast as expected.


I'm testing on:

  • Windows Seven x64
  • .Net 4.0
  • 4Gb RAM
  • GeForce 9800GT 1Gb.

I'm creating AppDomain this way

var permissionSet = new PermissionSet(null);

permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution | SecurityPermissionFlag.SerializationFormatter | SecurityPermissionFlag.UnmanagedCode));
permissionSet.AddPermission(new ReflectionPermission(PermissionState.Unrestricted));
permissionSet.AddPermission(new UIPermission(PermissionState.Unrestricted));
permissionSet.AddPermission(new MediaPermission(PermissionState.Unrestricted));
permissionSet.AddPermission(new FileDialogPermission(PermissionState.Unrestricted));

var appDomainSetup =
    new AppDomainSetup
    {
        ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
        ApplicationName = AppDomain.CurrentDomain.SetupInformation.ApplicationName,
        DisallowApplicationBaseProbing = false,
        DisallowBindingRedirects = true,
        DisallowCodeDownload = true,
        DisallowPublisherPolicy = true,
        LoaderOptimization = LoaderOptimization.MultiDomainHost
    };

_appDomain =
    AppDomain.CreateDomain(
        name,
        null,
        appDomainSetup,
        permissionSet,
        new[]
    {
        // a few types I need
        typeof(...).Assembly.Evidence.GetHostEvidence<StrongName>(),
    });

The behavior remains the same even if I strip down XAML to empty window

<Window
    x:Class="Rosmurta.Extensibility.WpfUI.RosmurtaWindow"
    x:ClassModifier="internal"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test"
    Height="480"
    Width="640"
    WindowStyle="SingleBorderWindow">
    <Grid>
    </Grid>
</Window>

Not too much to parse by XamlRader.LoadBaml, but it spends more than 30% of startup time event for empty window.


I've tried (and it did not help)

  • Adding <generatePublisherEvidence enabled="false"/> to App.config.
  • Adding [LoaderOptimization(LoaderOptimization.MultiDomainHost)] atribute to Main method.
  • Adding signatures to all assemblies.

What else can be done?

来源:https://stackoverflow.com/questions/7610009/wpf-warm-appdomain-startup-performance-application-runinternal-xamlreader-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!