What are the advantages and disadvantages of pre-jitting assemblies in .NET?

流过昼夜 提交于 2019-12-08 19:18:24

问题


What are the advantages and disadvantages of pre-jitting assemblies in .NET?

I heard that pre-jitting will improve performance. When should I pre-jit and when shouldn't I pre-jit?


回答1:


"Pre-jitting" or pre-compiling will improve performance, at start up, because you would be skipping that step. The reason that .NET JITs every time an app and its libraries load is so that it can run on many platforms and architectures with the best possible optimizations without the need for managing your builds.

So you have to weigh whether it's worth the admin headaches to save a few seconds on app start up and library loads. I think the most common use case for doing this is for server installs where you tend to manage few machines and the environment is very stable. E.g. you would not pre-compile for client apps because the target environments are much less predictable.




回答2:


"PRE-JIT" is done through NGen (the process of precompiling from CIL to a native image). It will convert the compiled .NET code from the platform-independent intermediate state to a platform specific stage. In plain English, it converts the .NET application that can run on both Windows, Mac and Linux 32-bit and 64-bit to an old-school EXE file that can only run on one of these.

.NET applications are compiled into a intermediate binary format called MSIL that is platform independent. This means that the application can be run by any CPU on any platform as long as the platform supports .NET. What .NET does during execution is called JIT. JIT will compile the code once per execution just before it is actually used. This also means that only the code used will be compiled.

NGen will give your application a performance boost (mostly startup time), sometimes very noticeable. It is safe to NGen just about anything as long as you target the correct platform. For example, if your application uses 32-bit DLL files you should not NGen it to 64-bit, and if your DLL file is in use by other applications you should not NGen it.

I'd recommend running NGen after installation, not before distribution, so that you know the application will work on the target computer.




回答3:


Are you talking about NGen to generate assembly images before execution? Pre-JIT is a contradiction in terms, since "JIT" means just-in-time, as in right before execution. If you precompile something, it is, by definition, not JIT-ing.

The advantage is that you don't have the initial compilation delay that the JITter can introduce when an assembly or type is loaded for the first time in code. For exceedingly (probably unwisely) large assemblies/types, this can be significant.

The disadvantages include things like the inability to optimize out some things that can only be determined based upon runtime conditions and the fact that you have to maintain the image. In addition, all applications and assemblies using pregenerated images (as of .NET 4) require full trust, and CAS is ignored.

For more information on NGen, see http://msdn.microsoft.com/en-us/library/6t9t5wcf.aspx




回答4:


It improves the warm startup time of your program. A warm start is one where the assembly data is already in the file system cache so no time is spent by the disk drive to locate the DLL on the disk. As opposed to a cold start, one you'll get when the assembly has never been loaded before or was loaded long ago, the disk drive has to find the file first. Which is slow. You almost always only care about cold start time because that's the one that's so noticeable to the user.

Which is the rub, ngen.exe creates an extra file that needs to be found by the disk drive. The one that contains the prejitted machine code (.ni.dll). Possibly making the cold start slower. For 'small' assemblies, it actually makes sense to let the JIT compiler jit the code because that can take less time than needed by the disk drive to find the prejitted DLL. What exactly is 'small', the break-even point, depends a great deal on how fast the disk drive can seek and its fragmentation state. You'd have to experiment, but keep in mind that this won't repeat well on another machine. And that experiments like this are difficult in themselves, you'll easily get a warm start.




回答5:


When you say "pre-jitting", you probably mean using ngen.exe to pre-compile your assemblies?

Using ngen.exe comes with no real disadvantages (except some additional bytes of disk usage). However, in order to use it, the assemblies you want to pre-compile have to be located in the GAC (Global Assembly Cache). Yet, you will need administrator privilegs to get them there.
Thus, it is not suitable for applications you want to deploy very easily via Copy&Paste.

Yes, using ngen.exe might improove the application's performance (even at runtime as some optimizations are skipped during JIT-compiling to save compiling time). So use ngen whenever possible especially for long-running applications and applications you actually want to install locally




回答6:


I have seen an advantage where determinism of execution is important. Normally you need to avoid timing sensitive execution like the plague. Sometimes it is unavoidable. One scenario is when interacting with external hardware and some of the behavior is asynchronous especially when there are a lot of steps that are executed serially and not repeatedly. This means each step has to be jitted and the hardware is doing its own thing at the same time (asynchronous communication)

We always ngen our libraries during installation. Our libraries access disparate hardware devices many of which were not designed for automation originally.



来源:https://stackoverflow.com/questions/4830218/what-are-the-advantages-and-disadvantages-of-pre-jitting-assemblies-in-net

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