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 ...
随机推荐
- ASP.NET静态页生成
由于业务需要,得把页面按照模板页生成静态页面,所以自己就琢磨了下,写些思路,以备日后需要的时候用. 静态页生成用到最多的就是匹配跟替换了,首先得读取模板页的html内容,然后进行你自己定义的标签匹配, ...
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- JAVA CAS原理、unsafe、AQS
concurrent包的实现 由于java的CAS同时具有 volatile 读和volatile写的内存语义,因此Java线程之间的通信现在有了下面四种方式: A线程写volatile变量,随后B线 ...
- Spring配置数据库固定代码
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" > &l ...
- cocos2d-x CCEditBox 字符不能显示完全的bug
cocos2d-x CCEditBox 字符不能显示完全的bug (cocos2dx版本 2.2.0)用CCEditBox制作帐号输入框,当输入的内容超过框的宽度时,框里面不会显示当前输入的字符,显示 ...
- 关于python中的unicode字符串的使用
基于python2.7中的字符串: unicode-->编码encode('utf-8')-->写入文件 读出文件-->解码decode('utf-8')-->unicode ...
- 【LeetCode】100 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- DOM笔记(七):开发JQuery插件
在上一篇笔记本中,讲解了如何利用jQuery扩展全局函数和对象:DOM笔记(六):怎么进行JQuery扩展? 在这篇笔记本中,将开发一个简单的动画插件,名称是example-plugin,用其实现一个 ...
- 数往知来 ASP.NET 表单的提交_url传值_重定向 <十八>
一.表单提交时注意 如果是以get方式提交数据的时候,在接收时是用QueryString方式进行接收 如果是以post请求数据时在接收时是用Form进行接收 为什么么要这样做呢?我们用Request[ ...
- 数往知来C#之 正则表达式 委托 XML<六>
C# 正则表达式篇 一.正则表达式 正则表达式就是一个字符串,不要想着一下子可以写出一个通用的表达式,先写,不正确再改 写正则表达式就是在找规律 关键字:Regex -->引入命名空间 ...