我可以检查 C# 编译器是否内联了方法调用吗? [英] Can I check if the C# compiler inlined a method call?

查看:32
本文介绍了我可以检查 C# 编译器是否内联了方法调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 XNA 游戏,我在其中进行逐像素碰撞检查.检查这一点的循环是通过移位整数和按位 ORing 来实现的,并且通常难以阅读和理解.

I'm writing an XNA game where I do per-pixel collision checks. The loop which checks this does so by shifting an int and bitwise ORing and is generally difficult to read and understand.

我想添加诸如 private bool IsTransparent(int pixelColorValue) 之类的私有方法以使循环更具可读性,但我不想要方法调用的开销,因为这对性能非常敏感代码.

I would like to add private methods such as private bool IsTransparent(int pixelColorValue) to make the loop more readable, but I don't want the overhead of method calls since this is very performance sensitive code.

有没有办法强制编译器内联这个调用,还是我只是希望编译器会做这个优化?

Is there a way to force the compiler to inline this call or will I do I just hope that the compiler will do this optimization?

如果没有办法强制执行此操作,是否有办法检查该方法是否已内联,而无需阅读反汇编?如果该方法是内联的并且不存在其他调用者,它会在反射中显示吗?

If there isn't a way to force this, is there a way to check if the method was inlined, short of reading the disassembly? Will the method show up in reflection if it was inlined and no other callers exist?

我无法强制它,所以我可以检测到它吗?

I can't force it, so can I detect it?

推荐答案

不,你不能.更重要的是,决定内联的不是将代码转换为 IL 的 VS 编译器,而是将 IL 转换为机器码的 JIT 编译器.这是因为只有 JIT 编译器对处理器架构有足够的了解才能决定将方法内联是否合适,因为这是指令流水线和缓存大小之间的权衡.

No you can't. Even more, the one who decides on inlining isn't VS compiler that takes you code and converts it into IL, but JIT compiler that takes IL and converts it to machine code. This is because only the JIT compiler knows enough about the processor architecture to decide if putting a method inline is appropriate as it’s a tradeoff between instruction pipelining and cache size.

因此,即使在 .NET Reflector 中查找也无济于事.

So even looking in .NET Reflector will not help you.

这篇关于我可以检查 C# 编译器是否内联了方法调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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