是否有可能使用分支prediction提示在C#中? [英] Is it possible to use branch prediction hinting in C#?

查看:148
本文介绍了是否有可能使用分支prediction提示在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我知道它被定义为海湾合作委员会,并在Linux内核中使用的:

 #定义可能(X)__builtin_expect((X),1)
的#define不大可能(x)的__builtin_expect((X),0)
 

如果这样的事是可能在C#中,是手动重新排序的最佳替代if语句,将最有可能的情况下,第一个?是否有任何其他方式基于这种外部知识的?

优化

在一个相关的说明,CLR知道如何识别后卫条款,并假定另一个支路将采取,使这种优化不恰当的防范clases使用,是否正确?

(请注意,我意识到这可能是一个微型的优化,我只是为学术目的,有兴趣的。)

解决方案

简短的回答:没有

再回应:你没的真正的需要在大多数情况下。您的可以的改变逻辑的语句给予提示。这是容易做到与性能的工具,像一个建立在更高(更贵)版本的Visual Studio,因为你可以捕捉到错误predicted分行柜台。我意识到这是学术的目的,但它是很好的知道,JITer是的非常的善于优化你的code为您服务。为

为例(通过C#采取pretty的从 CLR多少逐字)

这code:

 公共静态无效的主要(){
    的Int32 [] =一个新的Int32 [5];
    对于(的Int32指数= 0;指数<则为a.length;指数++){
        //做一些事情了[索引]
    }
}
 

看起来可能是低效的,因为则为a.length 是一个属性,因为我们知道在C#中,属性实际上是一组的两个方法( get_Length set_Length 在这种情况下)。然而,JIT知道,这是一个财产,无论是存储在你的局部变量的长度,或内联的方式,以prevent的开销。

  
    

...一些开发商低估了能力     JIT编译器和试图在试图帮助JIT写巧code     编译器。但是,你想出任何聪明的企图几乎肯定会产生影响     性能产生不利,让您的code难读,减少其可维护性。

  

除其他事项外,它实际上更进一步并执行边界检查一次的外循环的而不是环,这会降低性能的内部。

我意识到它几乎没有直接与你的问题的事,但我想,我想说明一点的是,微优化,这样真的不帮你多少在C#中,这是因为JIT一般做它更好,因为它恰恰设计用于此。 (有趣的事实,在x86 JIT编译器执行更积极的优化比64对应)

本文解释一些添加在.NET 3.5 SP1,跻身优化他们正在改进矫直分支机构,以提高prediction和缓存位置。

所有这一切都这样说,如果你想读一个伟大的书,进入什么样的编译器生成和CLR的表现,我推荐这本书,我从上面,CLR通过C#引用。

编辑:我要指出,如果这是目前可能在.net中,你会发现无论是在的EMCA-335标准工作草案。有支持此没有标准,并观看像反汇编或CFF Explorer中的元数据显示的任何特殊元数据可以暗示分支predictions迹象。

For example, I know it is defined for gcc and used in the Linux kernel as:

#define likely(x)       __builtin_expect((x),1)
#define unlikely(x)     __builtin_expect((x),0)

If nothing like this is possible in C#, is the best alternative to manually reorder if-statements, putting the most likely case first? Are there any other ways to optimize based on this type of external knowledge?

On a related note, the CLR knows how to identify guard clauses and assumes that the alternate branch will be taken, making this optimization inappropriate to use on guard clases, correct?

(Note that I realize this may be a micro-optimization; I'm only interested for academic purposes.)

解决方案

Short answer: No.

Longer Answer: You don't really need to in most cases. You can give hints by changing the logic in your statements. This is easier to do with a performance tool, like the one built into the higher (and more expensive) versions of Visual Studio, since you can capture the mispredicted branches counter. I realize this is for academic purposes, but it's good to know that the JITer is very good at optimizing your code for you. As an example (taken pretty much verbatim from CLR via C#)

This code:

public static void Main() {
    Int32[] a = new Int32[5];
    for(Int32 index = 0; index < a.Length; index++) {
        // Do something with a[index]
    }
}

may seem to be inefficient, since a.Length is a property and as we know in C#, a property is actually a set of two methods (get_Length and set_Length in this case). However, the JIT knows that it's a property and either stores the length in a local variable for you, or inlines the method, to prevent the overhead.

...some developers have underestimated the abilities of the JIT compiler and have tried to write "clever code" in an attempt to help the JIT compiler. However, any clever attempts that you come up with will almost certainly impact performance negatively and make your code harder to read, reducing its maintainability.

Among other things, it actually goes further and does the bounds checking once outside of the loop instead of inside the loop, which would degrade performance.

I realize it has little to do directly with your question, but I guess the point that I'm trying to make is that micro-optimizations like this don't really help you much in C#, because the JIT generally does it better, as it was designed exactly for this. (Fun fact, the x86 JIT compiler performs more aggressive optimizations than the x64 counterpart)

This article explains some of the optimizations that were added in .NET 3.5 SP1, among them being improvements to straightening branches to improve prediction and cache locality.

All of that being said, if you want to read a great book that goes into what the compiler generates and performance of the CLR, I recommend the book that I quoted from above, CLR via C#.

EDIT: I should mention that if this were currently possible in .NET, you could find the information in either the EMCA-335 standard or working draft. There is no standard that supports this, and viewing the metadata in something like IlDasm or CFF Explorer show no signs of any special metadata that can hint at branch predictions.

这篇关于是否有可能使用分支prediction提示在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆