Delphi 什么时候尊重“内联”,什么时候不尊重?
我试图优化一段具有以下构造的代码:
while (i > 0) do begin
Dec(i);
这看起来效率低下,所以我尝试这样做:
while (Dec(i) >= 0) do begin
这不起作用,因为 Dec 是一个过程而不是函数。
所以我将其重写为:
procedure Withloop;
var
....
function Decr(var a: integer): integer; inline;
begin
Dec(a);
Result:= a;
end;
...
while (Decr(i) >= 0) do begin
但这被编译为:
SDIMAIN.pas.448: while (Decr(i) >= 0) do begin
00468EE5 8BC4 mov eax,esp
00468EE7 E8D0FEFFFF call Decr <<--- A call??
00468EEC 85C0 test eax,eax
00468EEE 0F8D12FFFFFF jnl $00468e06
00468EF4 EB01 jmp $00468ef7
但是在程序的另一部分,它内联了一个函数就好了。
我可以使用什么经验法则(或硬性规则)来知道 Delphi 将遵守 inline
指令?
I was tying to optimize a piece of code that has this construct:
while (i > 0) do begin
Dec(i);
This looks inefficient, so I tried to do this:
while (Dec(i) >= 0) do begin
That doesn't work because Dec is a procedure and not a function.
So I rewrite it to:
procedure Withloop;
var
....
function Decr(var a: integer): integer; inline;
begin
Dec(a);
Result:= a;
end;
...
while (Decr(i) >= 0) do begin
But this gets compiled into:
SDIMAIN.pas.448: while (Decr(i) >= 0) do begin
00468EE5 8BC4 mov eax,esp
00468EE7 E8D0FEFFFF call Decr <<--- A call??
00468EEC 85C0 test eax,eax
00468EEE 0F8D12FFFFFF jnl $00468e06
00468EF4 EB01 jmp $00468ef7
However in another part of the program, it inlines a function just fine.
What rule of thumb (or hard rule) can I use to know to Delphi will honor the inline
directive?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Delphi 文档
枚举了以下条件:内联发生或不发生:对于您的情况,请检查此条件:
The
Delphi Documentation
enumerates the conditions under which inlining does or does not occur:In your case check this condition:
由于某种原因,编译器不会内联
while
循环控制表达式。 Hallvard Vassbotn 不久前讨论过这个问题(阅读文章结尾)。For some reason the compiler does not inline
while
loop control expressions. Hallvard Vassbotn discussed the problem some time ago (read the end of the article).