如何在启动时提高引用的dll的加载性能?

时间:2013-11-21 07:44:47

标签: winforms performance c#-4.0 dll devexpress

我在.NET 4框架上有一个Windows窗体应用程序并使用Devexpress组件。在我的应用程序的第一次运行中,它等待20或30秒来装载组件。之后它像火箭一样运行:)我该如何解决这个问题?我搜索了一些,发现了NGEN.exe。我不知道NGEN是否可用。有没有其他简单的方法来增加装载速度?你喜欢哪个?

3 个答案:

答案 0 :(得分:5)

您有冷启动问题。冷启动时间由磁盘驱动器控制,将组件定位在磁盘上。热启动很快,程序集存在于文件系统缓存中,因此不必位于慢速磁盘上。

使用ngen.exe使冷启动更糟,它需要找到另一个文件。 Microsoft建议您仅在大型程序集上使用ngen.exe的原因。与查找ngen图像文件相比,Jitting实际上可以更快

关于慢速冷启动你可以做的不多,这是一个纯粹的硬件问题,你需要一个更快的磁盘来真正领先。确保它的碎片整理确实有帮助。 SSD很不错。您可以使用ILMerge.exe来减少需要找到的DLL的数量。如果用户在机器启动后经常足够地启动程序,那么这个问题往往会随着时间的推移自行解决。 Windows会学习使用模式和预取可执行文件,以便在需要时它们位于文件系统缓存中。一个名为SuperFetch的功能。

Fwiw,缓慢的冷启动并不是.NET程序所独有的,使用大量DLL的非托管程序也存在这个问题。 Office和Adobe Reader是常见的例子。他们作弊,他们安装了一个登录时运行的“优化器”。它所做的就是将这些DLL放入文件系统缓存中。很烦人。你也可以这样做,当然我不能推荐。

答案 1 :(得分:2)

如果您运行的WinForms项目设置为“任何CPU”,请尝试将其构建为x86(或者更喜欢32位),看看是否会产生影响。

我特意遇到了.NET 4.5.1 JIT编译器的问题,这个问题花费了太长时间来编译几个DevExpress WinForms dll为64位。切换到32位允许我们最重的形式在3秒内冷启动负载,而在它需要35秒之前。

有趣的是,关闭JIT优化也很有帮助,因为显然64位优化非常缓慢。我注意到我们的发布模式构建运行速度远远低于我们的Debug构建。作为一个简单的测试,您可以在应用程序文件夹中放置一个简单的<App Executable Name>.ini来关闭JIT优化,看看是否有所作为:

[.NET Framework Debugging Control]
GenerateTrackingInfo=1
AllowOptimize=0

如果任何DevExpress支持人员正在阅读这篇文章,并且为了后人的缘故,这里是我的NGen输出,当预编译时,一些DevExpress 14.1.3.0 dll为64位。我最终没有使用NGen,但它抛出了一些你可能觉得有用的奇怪错误。

E:\Applications\Pryme>C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install Pryme.exe
Microsoft (R) CLR Native Image Generator - Version 4.0.30319.18408
Copyright (c) Microsoft Corporation.  All rights reserved.
Failed to load dependency DevExpress.XtraMap.v14.1 of assembly DevExpress.Dashboard.v14.1.Win, Version=14.1.3.0, Culture
=neutral, PublicKeyToken=b88d1754d700e49a because of the following error : The system cannot find the file specified. (E
xception from HRESULT: 0x80070002)
Failed to load dependency DevExpress.Map.v14.1.Core of assembly DevExpress.Dashboard.v14.1.Core, Version=14.1.3.0, Cultu
re=neutral, PublicKeyToken=b88d1754d700e49a because of the following error : The system cannot find the file specified.
(Exception from HRESULT: 0x80070002)
Failed to load dependency DevExpress.XtraMap.v14.1 of assembly DevExpress.Dashboard.v14.1.Core, Version=14.1.3.0, Cultur
e=neutral, PublicKeyToken=b88d1754d700e49a because of the following error : The system cannot find the file specified. (
Exception from HRESULT: 0x80070002)
Failed to load dependency DevExpress.XtraCharts.v14.1.Extensions of assembly DevExpress.XtraReports.v14.1.Extensions, Ve
rsion=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a because of the following error : The system cannot find
 the file specified. (Exception from HRESULT: 0x80070002)
3>    Compiling assembly DevExpress.SpellChecker.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d
700e49a (CLR v4.0.30319) ...
2>    Compiling assembly DevExpress.XtraSpellChecker.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d7
00e49a (CLR v4.0.30319) ...
4>    Compiling assembly DevExpress.Data.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a (CLR
v4.0.30319) ...
1>    Compiling assembly E:\Applications\Pryme\Pryme.exe (CLR v4.0.30319) ...
2>    Compiling assembly DevExpress.Utils.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a (CLR
 v4.0.30319) ...
3>    Compiling assembly DevExpress.Printing.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e
49a (CLR v4.0.30319) ...
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
2>    Compiling assembly DevExpress.XtraEditors.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49
a (CLR v4.0.30319) ...
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
4>Cannot embed generic MethodDesc while compiling method PropVariant.GetVector
3>    Compiling assembly DevExpress.Sparkline.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700
e49a (CLR v4.0.30319) ...
3>    Compiling assembly Merydyan.Pryme.SI, Version=3.0.18.34925, Culture=neutral, PublicKeyToken=null (CLR v4.0.30319)
...
4>    Compiling assembly Merydyan.Pryme.Core, Version=3.0.18.34915, Culture=neutral, PublicKeyToken=null (CLR v4.0.30319
) ...
3>    Compiling assembly NodaTime, Version=1.2.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1 (CLR v4.0.30319) ..
.
4>    Compiling assembly AutoMapper, Version=3.2.1.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005 (CLR v4.0.30319)
...
3>    Compiling assembly Autofac, Version=3.5.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da (CLR v4.0.30319) ...

4>    Compiling assembly EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 (CLR v4.0.30
319) ...
3>    Compiling assembly Merydyan.Pryme.Data, Version=3.0.18.34919, Culture=neutral, PublicKeyToken=null (CLR v4.0.30319
) ...
2>    Compiling assembly EntityFramework.Extended, Version=6.0.0.0, Culture=neutral, PublicKeyToken=05b7e29bdd433584 (CL
R v4.0.30319) ...
1>    Compiling assembly Merydyan.Pryme.Services, Version=3.0.18.34923, Culture=neutral, PublicKeyToken=null (CLR v4.0.3
0319) ...
2>    Compiling assembly Humanizer, Version=1.28.0.0, Culture=neutral, PublicKeyToken=979442b78dfc278e (CLR v4.0.30319)
...
3>    Compiling assembly DevExpress.XtraReports.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49
a (CLR v4.0.30319) ...
2>    Compiling assembly DevExpress.Office.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49
a (CLR v4.0.30319) ...
1>    Compiling assembly DevExpress.XtraCharts.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
 (CLR v4.0.30319) ...
2>    Compiling assembly DevExpress.Charts.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49
a (CLR v4.0.30319) ...
3>    Compiling assembly DevExpress.PivotGrid.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700
e49a (CLR v4.0.30319) ...
2>    Compiling assembly DevExpress.RichEdit.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e
49a (CLR v4.0.30319) ...
1>    Compiling assembly DevExpress.XtraGauges.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d70
0e49a (CLR v4.0.30319) ...
3>    Compiling assembly DevExpress.XtraGrid.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a (
CLR v4.0.30319) ...
1>    Compiling assembly DevExpress.XtraLayout.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
 (CLR v4.0.30319) ...
1>    Compiling assembly DevExpress.XtraPrinting.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e4
9a (CLR v4.0.30319) ...
4>    Compiling assembly DevExpress.XtraBars.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a (
CLR v4.0.30319) ...
1>    Compiling assembly DevExpress.XtraTreeList.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e4
9a (CLR v4.0.30319) ...
3>    Compiling assembly DevExpress.XtraScheduler.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e
49a (CLR v4.0.30319) ...
4>Cannot embed generic MethodDesc while compiling method MouseEventSubscriber`1.PreFilterMessage
1>    Compiling assembly DevExpress.XtraScheduler.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754
d700e49a (CLR v4.0.30319) ...
3>    Compiling assembly DevExpress.XtraScheduler.v14.1.Extensions, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b8
8d1754d700e49a (CLR v4.0.30319) ...
3>    Compiling assembly DevExpress.XtraWizard.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
 (CLR v4.0.30319) ...
1>    Compiling assembly DevExpress.Dashboard.v14.1.Win, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e
49a (CLR v4.0.30319) ...
3>    Compiling assembly DevExpress.Dashboard.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700
e49a (CLR v4.0.30319) ...
3>Warning: System.IO.FileNotFoundException: Could not load file or assembly 'DevExpress.XtraMap.v14.1, Version=14.1.3.0,
 Culture=neutral, PublicKeyToken=b88d1754d700e49a' or one of its dependencies. The system cannot find the file specified
.. If this assembly is found during runtime of an application, then the native image currently being generated will not
be used.
3>Warning: System.IO.FileNotFoundException: Could not load file or assembly 'DevExpress.Map.v14.1.Core, Version=14.1.3.0
, Culture=neutral, PublicKeyToken=b88d1754d700e49a' or one of its dependencies. The system cannot find the file specifie
d.. If this assembly is found during runtime of an application, then the native image currently being generated will not
 be used.
1>Warning: System.IO.FileNotFoundException: Could not load file or assembly 'DevExpress.XtraMap.v14.1, Version=14.1.3.0,
 Culture=neutral, PublicKeyToken=b88d1754d700e49a' or one of its dependencies. The system cannot find the file specified
.. If this assembly is found during runtime of an application, then the native image currently being generated will not
be used.
1>Warning: System.IO.FileNotFoundException: Could not load file or assembly 'DevExpress.Map.v14.1.Core, Version=14.1.3.0
, Culture=neutral, PublicKeyToken=b88d1754d700e49a' or one of its dependencies. The system cannot find the file specifie
d.. If this assembly is found during runtime of an application, then the native image currently being generated will not
 be used.
3>Warning: System.IO.FileNotFoundException: Could not load file or assembly 'DevExpress.XtraMap.v14.1, Version=14.1.3.0,
 Culture=neutral, PublicKeyToken=b88d1754d700e49a' or one of its dependencies. The system cannot find the file specified
. (Exception from HRESULT: 0x80070002). If this assembly is found during runtime of an application, then the native imag
e currently being generated will not be used.
3>Warning: System.IO.FileNotFoundException: Could not load file or assembly 'DevExpress.Map.v14.1.Core, Version=14.1.3.0
, Culture=neutral, PublicKeyToken=b88d1754d700e49a' or one of its dependencies. The system cannot find the file specifie
d. (Exception from HRESULT: 0x80070002). If this assembly is found during runtime of an application, then the native ima
ge currently being generated will not be used.
1>Warning: System.IO.FileNotFoundException: Could not load file or assembly 'DevExpress.XtraMap.v14.1, Version=14.1.3.0,
 Culture=neutral, PublicKeyToken=b88d1754d700e49a' or one of its dependencies. The system cannot find the file specified
. (Exception from HRESULT: 0x80070002). If this assembly is found during runtime of an application, then the native imag
e currently being generated will not be used.
4>Cannot embed generic MethodDesc while compiling method MouseEventSubscriber`1.PreFilterMessage
4>    Compiling assembly DevExpress.DataAccess.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
 (CLR v4.0.30319) ...
1>    Compiling assembly DevExpress.Xpo.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a (CLR v
4.0.30319) ...
3>    Compiling assembly DevExpress.XtraGauges.v14.1.Win, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700
e49a (CLR v4.0.30319) ...
4>    Compiling assembly DevExpress.XtraGauges.v14.1.Presets, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754
d700e49a (CLR v4.0.30319) ...
2>    Compiling assembly DevExpress.XtraCharts.v14.1.Wizard, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d
700e49a (CLR v4.0.30319) ...
3>Cannot embed generic MethodDesc while compiling method GaugeDataBindingPage`1.OnSelectedItemChanged
3>Cannot embed generic MethodDesc while compiling method GaugeDataBindingPage`1.OnSelectedItemChanged
3>    Compiling assembly DevExpress.Utils.v14.1.UI, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a (
CLR v4.0.30319) ...
4>    Compiling assembly DevExpress.XtraRichEdit.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e4
9a (CLR v4.0.30319) ...
3>    Compiling assembly DevExpress.XtraVerticalGrid.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d7
00e49a (CLR v4.0.30319) ...
2>    Compiling assembly DevExpress.XtraNavBar.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
 (CLR v4.0.30319) ...
3>    Compiling assembly DevExpress.XtraCharts.v14.1.UI, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e
49a (CLR v4.0.30319) ...
1>    Compiling assembly DevExpress.DataAccess.v14.1.UI, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e
49a (CLR v4.0.30319) ...
3>    Compiling assembly DevExpress.CodeParser.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
 (CLR v4.0.30319) ...
2>    Compiling assembly DevExpress.XtraPivotGrid.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e
49a (CLR v4.0.30319) ...
4>    Compiling assembly DevExpress.XtraRichEdit.v14.1.Extensions, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88
d1754d700e49a (CLR v4.0.30319) ...
1>    Compiling assembly DevExpress.XtraPdfViewer.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e
49a (CLR v4.0.30319) ...
1>    Compiling assembly DevExpress.Pdf.v14.1.Core, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a (
CLR v4.0.30319) ...
4>    Compiling assembly DevExpress.Pdf.v14.1.Drawing, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49
a (CLR v4.0.30319) ...
2>    Compiling assembly DevExpress.XtraReports.v14.1.Extensions, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d
1754d700e49a (CLR v4.0.30319) ...
4>    Compiling assembly WebCam_Capture, Version=1.0.2395.24959, Culture=neutral, PublicKeyToken=null (CLR v4.0.30319) .
..
2>Warning: System.IO.FileNotFoundException: Could not load file or assembly 'DevExpress.XtraCharts.v14.1.Extensions, Ver
sion=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a' or one of its dependencies. The system cannot find the
file specified.. If this assembly is found during runtime of an application, then the native image currently being gener
ated will not be used.
4>    Compiling assembly DevExpress.XtraScheduler.v14.1.Reporting, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88
d1754d700e49a (CLR v4.0.30319) ...
4>    Compiling assembly DevExpress.Xpf.Core.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a (
CLR v4.0.30319) ...
2>Warning: System.IO.FileNotFoundException: Could not load file or assembly 'DevExpress.XtraCharts.v14.1.Extensions, Ver
sion=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a' or one of its dependencies. The system cannot find the
file specified. (Exception from HRESULT: 0x80070002). If this assembly is found during runtime of an application, then t
he native image currently being generated will not be used.
3>    Compiling assembly DevExpress.Mvvm.v14.1, Version=14.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a (CLR
v4.0.30319) ...
1>    Compiling assembly Merydyan.Pryme.UI.Windows, Version=3.0.18.34928, Culture=neutral, PublicKeyToken=null (CLR v4.0
.30319) ...

E:\Applications\Pryme>C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install Pryme.exe

答案 2 :(得分:1)

安装后可能自动运行程序的存根?也就是说,使用在安装的最后一步静默退出的开关自动加载程序。大多数人都希望安装需要几秒钟/分钟,所以大多数人都不会注意到。

更新: 我检查了MSDN C# documentation,它表示按需加载程序集(根据需要),但您可以使用Assembly.ReflectionOnlyLoadFrom方法强制加载它们。如果您在用户输入登录信息时预加载程序集(以及可选的依赖项),那么您将有效地隐藏加载延迟。