Pre-compile (pre-JIT) your assembly on the fly, or trigger JIT compilation ahead-of-time (转)
Introduction
All .NET developers know that one of the best features of the CLR is JIT-compilation: Just-In-Time compilation. Its name describes how it works: right before calling your method (just in time), the CLR triggers the JIT-compiler to produce native code from MSIL. JIT compilation is perfect for server-side applications: the JIT compiler produces highly optimized native code based on the execution environment data. The generated native code is stored in the application domain, and is used during consequent calls to this method. Though JIT compilation is not always the best choice for client-side applications with a rich user interface - users are pretty often complaining about lazy start-up of applications. In spite of developer explanations like "guys, it's JITting, we can't do anything here", users continue to complain, making our development managers unhappy. Obviously, unhappy managers tend to be unhappy developers.
So, what is the solution here? Develop the client-side application in C++ or Delphi? Some might say that we could use the Native Image Generation tool in .NET (ngen.exe). ngen.exe is not a silver bullet here, due to the following reasons:
1. MSDN states regarding native images: Performance of native images depends on a number of factors that make analysis difficult, such as code and data access patterns, how many calls are made across module boundaries, and how many dependencies have already been loaded by other applications. The only way to determine whether native images benefit your application is by careful performance measurements in your key deployment scenarios. Let me rephrase this: it means that it does not guarantee you better performance.
2. Native images installation/creation requires administrative privileges. This slightly reduces the set of suitable scenarios.
So, suddenly, I've come to the main point of my article. The main point is how to trigger JIT compilation in advance, how to pre-compile your assembly when it's more preferable for your application.
Let me start from an example:
Consider a scenario: We have a rich desktop client application with a huge number of different forms and and fancy UI controls (like DevExpress or Infragistics). Of course, during the first opening of each form, users are going to experience a delay due to JITting of all these fancy control libraries. How can we avoid this? We can trigger JIT compilation of heavy UI control libraries at some starting point of the application (for instance, during user log-in) in a separate background thread with a low priority. Or, we can pre-JIT all other forms while the user is working with the first one... I can imagine here a lot of Use Cases, but I am sure you can do it better in the scope of your particular project.
The code
We have a very nice namespace in BCL called System.Runtime.CompilerServices
which provides advanced developers with the ability to influence runtime behavior. But, here, we need only one class from this namespace - RuntimeHelpers
, and its method PrepareMethod
, in particular. So, what do we do here: I just load some heavy assembly by using the Assembly.Load
method (you can load it in a different way, of course), then walk through its types, and force JITting of all the methods of each type. That's it. As easy as possible. One more point, I do it in a separate low priority thread: don't worry about it, PrepareMethod
is thread safe.
Thread jitter = new Thread(() => { foreach (var type in Assembly.Load("MyHavyAssembly, Version=1.8.2008.8," + " Culture=neutral, PublicKeyToken=8744b20f8da049e3").GetTypes()) { foreach (var method in type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) { System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod(method.MethodHandle); } } }); jitter.Priority = ThreadPriority.Lowest; jitter.Start();
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Pre-compile (pre-JIT) your assembly on the fly, or trigger JIT compilation ahead-of-time (转)的更多相关文章
- 「BUAA OO Pre」 Pre 2总结回顾概览
「BUAA OO Pre」 Pre 2总结回顾概览 目录 「BUAA OO Pre」 Pre 2总结回顾概览 Part 0 前言 写作背景 定位 您可以在这里期望获得 您在这里无法期望获得 对读者前置 ...
- HTML“计算机输出”标签 <code><kbd><samp><tt><var><pre>
我们并不反对使用它们,但是如果您只是为了达到某种视觉效果而使用这些标签的话,我们建议您使用样式表,那么做会达到更加丰富的效果. <code> 标签-定义计算机代码文本. 定义和用法: &l ...
- js模仿jquery里的几个方法next, pre, nextAll, preAll
/*siblings函数, 选取node的所有兄弟节点*/ function siblings(node){ if(node.nodeType === 1){ node.flag = true; // ...
- css pre如果同时运用了css的border-radius、 overflow两个属性且标签中内容太多时,外部div滚动条在firefox下滚动时很卡
pre如果同时运用了css的border-radius. overflow两个属性且标签中内容太多时,外部div滚动条在firefox下滚动时很卡. 解决方法:去掉css中border-radius. ...
- angularJS绑定数据中对标签转义的处理二 与pre标签的使用
一.问题 默认情况下,angularJS绑定的数据为字符串文本,不会对其中包含的html标签进行转义生成格式化的文本.在实际工作时碰到接口返回的数据带有html格式时该如何处理. 二.解决办法 1.引 ...
- <pre> <textarea> <code>标签区别
这篇文章里面放的大都是我自己写程序的时候遇到的一些小问题,其实都是自己没有掌握的点,别人看起来应该很简单啦,但写下来能提醒自己,也能鼓励一下自己,这条路也不好走哇. <pre> <t ...
- 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree
二叉搜索树是常用的概念,它的定义如下: The left subtree of a node contains only nodes with keys less than the node's ke ...
- controller,link,compile不同
测试案例 .directive('testDirective', function() { return { restrict: 'E', template: '<p>Hello {{nu ...
- JIT编译器
深入理解Java Class文件格式(九) http://blog.csdn.net/zhangjg_blog/article/details/22432599 http://blog.csdn.ne ...
随机推荐
- java 调用 .net webservice
1.首先下载Axis2工具包 2.解压之后用cmd命令进入bin目录WSDL2Java.bat -uri http://192.168.20.42:9999/LoginService.asmx?wsd ...
- 预热buffer pool
mysqldump -u root db_name table_name> /dev/null select * from ...
- cong
Directions: Study the following cartoon carefully and write an essay in which you should 1) descr ...
- [Papers]MHD, $\p_3\pi$, Lebesgue space [Jia-Zhou, JMAA, 2012]
$$\bex \p_3\pi\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=2,\quad 3\leq q\leq \infty. \ee ...
- 什么是SPF?如何设置企业邮箱的SPF呢?(TXT记录)
什么是SPF? (Sender Policy Framework) 的缩写,一种以IP地址认证电子邮件发件人身份的技术,是非常高效的垃圾邮件解决方案. 接收邮件方会首先检查域名的SPF记录,来确定 ...
- Delphi 保存写字板程序, 并进行打印
wDoc := docapp1.Documents.open(ExtractFilePath(Paramstr(0)) + 'abc.doc'); wDoc.SaveAs(ExtractFilePat ...
- 细雨学习笔记:Jmeter集合点
设置集合点的原则 (1) 集合点设置数<=线程组线程数量(因为大于线程组线程数量的话就永远也到不了集合点) (2)线程组线程数量是集合点设置数的整数倍(因为分组有余数的话最后一组永远也到不了集合 ...
- mac 版本的 Google 网盘 走代理
开启surge Mac版 设置系统代理 127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,localhost,*.local 理论上可以使用cow来代 ...
- VC++2010配置使用MySQL5.6
0.前提 安装后的文件概览 编译器: VC++2010 MySQL版本:MySQL5.6.19 for win64 Connector版本:connector c++ 1.1.3 在VS2010 ...
- MVC dropdownlist使用
View中代码 @{ ViewBag.Title = "dropdownlist"; } <h2>dropdownlist</h2> @using (Htm ...