Can I check if the C# compiler inlined a method ca

2019-01-18 02:21发布

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.

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?

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

9条回答
对你真心纯属浪费
2楼-- · 2019-01-18 02:53

Nope, you can't.

Basically, you can't do that in most modern C++ compilers either. inline is just an offer to the compiler. It's free to take it or not.

The C# compiler does not do any special inlining at the IL level. JIT optimizer is the one that will do it.

查看更多
Lonely孤独者°
3楼-- · 2019-01-18 02:53

why not use unsafe code (inline c as its known) and make use of c/c++ style pointers, this is safe from the GC (ie not affected by collection) but comes with its own security implications (cant use for internet zone apps) but is excellent for the kind of thing it appears you are trying to achieve especially with performance and even more so with arrays and bitwise operations?

to summarise, you want performance for a small part of your app? use unsafe code and make use of pointers etc seems the best option to me

EDIT: a bit of a starter ? http://msdn.microsoft.com/en-us/library/aa288474(VS.71).aspx

查看更多
萌系小妹纸
4楼-- · 2019-01-18 03:01

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 it is cheaper to inline the function, it will. So don't worry about it unless your profiler says that it actually is a problem.

For more information

JIT Enhancements in .NET 3.5 SP1

查看更多
登录 后发表回答