JIT拒绝内联的小方法内联、方法、JIT

2023-09-05 23:48:52 作者:北诗

我失去了严重的优化,这是因为JIT不会内嵌了很多我的方法。

I'm missing serious optimizations because the JIT won't inline a lot of my methods.

例如,您有以下code:

For example lets have the following code:

static void Main(string[] args)
{
    IsControl('\0');
}

public static bool IsControl(char c)
{
    return ((c >= 0 && c <= 31) || (c >= 127 && c <= 159));
}

JIT编译后生成以下:

Produces the following after JIT compilation:

0000001f  xor         ecx,ecx 
00000021  call        FFFFFFFFFFEC9760 
00000026  mov         byte ptr [rsp+20h],al 
0000002a  nop 
0000002b  jmp         000000000000002D 
0000002d  add         rsp,38h 
00000031  rep ret 

注意 0000001f 是我设置的断点。正如你可以看到有一个电话 00000021 ,这是绝对错误的。为什么会这样一个微小的方法不能胜任内联?对于音符,这是与优化的编译。

Note that 0000001f is where I set the breakpoint. As you can see there is a call at 00000021, that is absolutely wrong. Why would such a tiny method not be qualified for inlining? For the note, this was compiled with optimization on.

推荐答案

有没有办法内嵌要求JIT编译器的方法,除了使用提前 - - 时间源或字节code转换到内联说明之前,他们曾经达到JIT。

There is no way to require the JIT compiler inline your methods, aside from using a ahead-of-time source or bytecode transformation to inline the instructions before they ever reach the JIT.

如果你的算法是微优化,去除调用指令导致显着的性能优势非常敏感,那么你可以考虑改写code中的性能关键部分在不同的语言,提供了更广泛的设备,用于控制该行为。根据你的问题的措辞,看来你正在试图迫使C#一个问题的空间,它的设计完全避免。

If your algorithm is so sensitive to micro-optimizations that removing call instructions results in a substantial performance advantage, then you might consider rewriting the performance-critical sections of code in a different language that provides more extensive facilities for controlling that behavior. Based on the wording of your question, it appears that you are trying to force C# into a problem space which it was designed to avoid altogether.