你用过ngen.exe吗?

发布于 2024-07-16 21:45:59 字数 56 浏览 5 评论 0原文

这里有人用过ngen吗? 在哪里? 为什么? 性能有任何改进吗? 何时何地使用它才有意义?

Has anybody here ever used ngen? Where? why? Was there any performance improvement? when and where does it make sense to use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

三人与歌 2024-07-23 21:45:59

我并不日常使用它,但它被那些想要提高性能的工具所使用; 例如,Paint.NET 在安装过程中(或者可能是第一次使用)使用 NGEN。 有些 MS 工具也有可能(尽管我不确定)。

基本上,NGEN 预先执行装配的大部分 JIT,因此冷启动时几乎没有延迟。 当然,在大多数典型用法中,不会达到 100% 的代码,因此在某些方面,这会执行许多不必要的工作 - 但它无法提前告知这一点。

IMO 的缺点是您需要使用 GAC 才能使用 NGEN; 我尝试尽可能避免 GAC,以便可以使用 robocopy-deployment(到服务器)和 ClickOnce(到客户端)。

I don't use it day-to-day, but it is used by tools that want to boost performance; for example, Paint.NET uses NGEN during the installer (or maybe first use). It is possible (although I don't know for sure) that some of the MS tools do, too.

Basically, NGEN performs much of the JIT for an assembly up front, so that there is very little delay on a cold start. Of course, in most typical usage, not 100% of the code is ever reached, so in some ways this does a lot of unnecessary work - but it can't tell that ahead of time.

The downside, IMO, is that you need to use the GAC to use NGEN; I try to avoid the GAC as much as possible, so that I can use robocopy-deployment (to servers) and ClickOnce (to clients).

如何视而不见 2024-07-23 21:45:59

是的,我已经看到了性能的改进。 我的测量表明,如果我也将我的程序集放入 GAC 中,它确实会提高启动性能,因为我的程序集都是强命名的。 如果您的程序集是强命名的,那么在不使用 GAC 的情况下,NGen 不会产生任何影响。 原因是,如果您的强命名程序集不在 GAC 中,则 .NET 运行时会通过从磁盘加载整个托管程序集来验证您的强命名程序集是否未被篡改,以便验证它是否绕过NGen 的主要优势之一。

对于我的应用程序来说,这不是一个很好的选择,因为我们依赖于我们公司的通用程序集(也是强命名的)。 通用程序集被许多使用许多不同版本的产品所使用,将它们放入 GAC 中意味着如果我们的应用程序之一没有说通用程序集之一的“使用特定版本”,则无论什么版本都会加载 GAC 版本版本位于其执行目录中。 我们认为 NGen 的好处不值得冒风险。

Yes, I've seen performance improvements. My measurements indicated that it did improve startup performance if I also put my assemblies into the GAC since my assemblies are all strong named. If your assemblies are strong named, NGen won't make any difference without using the GAC. The reason for this is that if you have strong named assemblies that are not in the GAC, then the .NET runtime validates that your strong named assembly hasn't been tampered with by loading the whole managed assembly from disk so it can validate it circumventing one of the major benefits of NGen.

This wasn't a very good option for my application since we rely on common assemblies from our company (that are also strong named). The common assemblies are used by many products that use many different versions, putting them in the GAC meant that if one of our applications didn't say "use specific version" of one of the common assemblies it would load the GAC version regardless of what version was in its executing directory. We decided that the benefits of NGen weren't worth the risks.

夢归不見 2024-07-23 21:45:59

Ngen主要减少.NET应用程序和应用程序工作集的启动时间。 但它有一些缺点(来自 Jeffrey Richter 的 CLR Via C#):

没有知识产权保护

NGen 的文件可能会不同步

加载时性能较差(变基/绑定)

执行时性能较差

由于所有问题列出,您在考虑使用时应该非常谨慎
NGen.exe。 对于服务器端应用程序,NGen.exe 几乎没有任何意义,因为只有
第一个客户端请求遇到性能影响; 未来的客户端请求会高速运行。 在
另外,对于大多数服务器应用程序来说,只需要一个代码实例,因此没有
工作集的好处。

对于客户端应用程序,NGen.exe 可能有助于缩短启动时间或减少启动时间
如果程序集同时被多个应用程序使用,则为工作集。 即使在一个案例中
一个程序集不被多个应用程序使用,NGen'ing 程序集可以改进
工作集。 此外,如果 NGen.exe 用于客户端应用程序的所有程序集,则 CLR
根本不需要加载 JIT 编译器,进一步减少工作集。 当然,如果
只有一个程序集未经过 NGen 处理,或者如果无法使用程序集的 NGen 处理文件,则 JIT 编译器
将加载,并且应用程序的工作集会增加。

Ngen mainly reduces the start-up time of .NET app and application's working set. But it's have some disadvantages (from CLR Via C# of Jeffrey Richter):

No Intellectual Property Protection

NGen'd files can get out of sync

Inferior Load-Time Performance (Rebasing/Binding)

Inferior Execution-Time Performance

Due to all of the issues just listed, you should be very cautious when considering the use of
NGen.exe. For server-side applications, NGen.exe makes little or no sense because only the
first client request experiences a performance hit; future client requests run at high speed. In
addition, for most server applications, only one instance of the code is required, so there is no
working set benefit.

For client applications, NGen.exe might make sense to improve startup time or to reduce
working set if an assembly is used by multiple applications simultaneously. Even in a case in
which an assembly is not used by multiple applications, NGen'ing an assembly could improve
working set. Moreover, if NGen.exe is used for all of a client application's assemblies, the CLR
will not need to load the JIT compiler at all, reducing working set even further. Of course, if
just one assembly isn't NGen'd or if an assembly's NGen'd file can't be used, the JIT compiler
will load, and the application's working set increases.

淡看悲欢离合 2024-07-23 21:45:59

ngen 主要以缩短启动时间(通过消除 JIT 编译)而闻名。 它可能会提高(通过减少 JIT 时间)或降低应用程序的整体性能(因为某些 JIT 优化不可用)。

.NET Framework 本身在安装时对许多程序集使用 ngen

ngen is mostly known for improving startup time (by eliminating JIT compilation). It might improve (by reducing JIT time) or decrease overall performance of the application (since some JIT optimizations won't be available).

.NET Framework itself uses ngen for many assemblies upon installation.

迷乱花海 2024-07-23 21:45:59

我已经使用过它,但仅用于研究目的。 仅当您确定部署环境的 cpu 架构(它不会改变)时才使用它,

但让我告诉您 JIT 编译还不错,并且如果您跨多个 cpu 环境进行部署(例如更新的 Windows 客户端应用程序)经常)那么就不要使用 NGEN。 那是因为有效的 ngen 缓存取决于许多属性。 如果其中一个失败,您的程序集将再次回退到 jit。

在这种情况下,JIT 显然是赢家,因为它根据运行的 cpu 架构动态优化代码。 (例如,它可以检测是否有超过 1 个 cpu)

并且 clr 在每个版本中都变得更好,因此简而言之,除非您非常确定您的部署环境,否则请坚持使用 JIT - 即使这样您的性能提升也很难证明使用 ngen 是合理的。 exe(可能收益会在几百毫秒内) - 恕我直言 - 它不值得付出努力,

还请检查有关此主题的这个真正好的链接 - JIT 编译和性能 - 到 NGen 还是不到 NGen?

i have used it but just for research purpose. use it ONLY if you are sure about the cpu architecture of your deployment environment (it wont change)

but let me tell you JIT compilation is not too bad and if you have deployments across multiple cpu environments (for example a windows client application which is updated often) THEN DO NOT USE NGEN. thats coz a valid ngen cache depends upon many attributes. if one of these fail, your assembly falls back to jit again

JIT is a clear winner in such cases, as it optimizes code on the fly based on the cpu architecture its running on. (for eg it can detect if there are more then 1 cpu)

and clr is getting better with every release, so in short stick with JIT unless you are dead sure of your deployment environment - even then your performance gains would hardly justify using ngen.exe (probably gains would be in few hundred ms) - imho - its not worth the efforts

also check this real nice link on this topic - JIT Compilation and Performance - To NGen or Not to NGen?

与酒说心事 2024-07-23 21:45:59

是的。 用于 WPF 应用程序以加快启动时间。 启动时间从 9 秒缩短到 5 秒。 在我的博客中阅读相关内容:

我最近发现 NGEN 的性能有多么出色。 这
我目前正在开发的应用程序有一个数据访问层(DAL)
生成的。 数据库schema相当大,我们还生成
一些数据(值列表)直接进入 DAL。 结果:很多
具有许多字段和许多方法的类。 JIT 开销经常表现出来
在分析应用程序时启动,但在搜索 JIT 编译之后
和 NGEN 我认为这不值得。 安装时间开销,
管理是我最关心的问题,让我忽略了迹象并专注于
相反,向应用程序添加更多功能。 当我们改变了
架构到在 64 位机器上运行的“任何 CPU”,情况变得更糟:
我们遇到了应用程序挂起长达 10 秒的情况
单个语句,分析器仅显示 JIT 开销
问题区域。 NGEN 解决了​​问题:声明从 10
秒到 1 毫秒。 该声明不属于
启动程序,所以我很想知道 NGEN 的整个过程
应用程序可能会影响启动时间。 从 8 秒变成
3.5秒。

结论:我真的建议您尝试一下 NGEN!

Yes. Used on a WPF application to speed up startup time. Startup time went from 9 seconds to 5 seconds. Read about it in my blog :

I recently discovered how great NGEN can be for performance. The
application I currently work on has a data access layer (DAL) that is
generated. The database schema is quite large, and we also generate
some of the data (list of values) directly into the DAL. Result: many
classes with many fields, and many methods. JIT overhead often showed
up when profiling the application, but after a search on JIT compiling
and NGEN I though it wasn’t worth it. Install-time overhead, with
management my major concern, made me ignore the signs and focus on
adding more functionality to the application instead. When we changed
architecture to “Any CPU” running on 64 bit machines things got worse:
We experienced hang in our application for up to 10 seconds on a
single statement, with the profiler showing only JIT overhead on the
problem-area. NGEN solved the problem: the statement went from 10
seconds to 1 millisecond. This statement was not part of the
startup-procedure, so I was eager to find out what NGEN’ing the whole
application could do to the startup time. It went from 8 seconds to
3.5 seconds.

Conclusion: I really recommend giving NGEN a try on your application!

不必在意 2024-07-23 21:45:59

作为 Mehrdad Afshari 关于 JIT 编译的评论的补充。 如果通过 XmlSerializer 序列化具有许多属性的类,并且在 64 位系统上,SGEN、NGEN 组合具有潜在的巨大(在我们的例子中为千兆字节和分钟)效果。

更多信息请点击这里:
XmlSerializer 在 64 位系统上启动巨大的性能损失请参阅 Nick Martyshchenko 的回答尤其。

As an addition to Mehrdad Afshari's comment about JIT compilation. If serializing a class with many properties via the XmlSerializer and on a 64-bit system a SGEN, NGEN combo has a potentially huge (in our case gigabytes and minutes) effect.

More info here:
XmlSerializer startup HUGE performance loss on 64bit systems see Nick Martyshchenko's answer especially.

心房的律动 2024-07-23 21:45:59

是的,我用一个小型的 CPU 密集型 exe 进行了尝试,但使用 ngen 时速度稍微慢一些!

我多次安装和卸载 ngen 映像并运行基准测试。

我总是得到以下可重现的时间 +/- 0.1 秒:
33.9s 无,
35.3秒与

Yes, I tried it with a small single CPU-intensive exe and with ngen it was slightly slower!

I installed and uninstalled the ngen image multiple times and ran a benchmark.

I always got the following times reproducable +/- 0.1s:
33.9s without,
35.3s with

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文