gpt4 book ai didi

delphi - Delphi 什么时候尊重 `inline`,什么时候不尊重?

转载 作者:行者123 更新时间:2023-12-03 15:02:51 24 4
gpt4 key购买 nike

我试图优化一段具有以下构造的代码:

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 指令?

最佳答案

Delphi Documentation枚举发生或不发生内联的条件:

  • Inlining will not occur on any form of late-bound method. This includes virtual, dynamic, and message methods.
  • Routines containing assembly code will not be inlined.
  • Constructors and destructors will not be inlined.
  • The main program block, unit initialization, and unit finalization blocks cannot be inlined.
  • Routines that are not defined before use cannot be inlined.
  • Routines that take open array parameters cannot be inlined.
  • Code can be inlined within packages, however, inlining never occurs across package boundaries.
  • No inlining is done between units that are circularly dependent. This includes indirect circular dependencies, for example, unit A uses unit B, and unit B uses unit C which in turn uses unit A. In this example, when compiling unit A, no code from unit B or unit C will be inlined in unit A.
  • The compiler can inline code when a unit is in a circular dependency, as long as the code to be inlined comes from a unit outside the circular relationship. In the above example, if unit A also used unit D, code from unit D could be inlined in A, since it is not involved in the circular dependency.
  • If a routine is defined in the interface section and it accesses symbols defined in the implementation section, that routine cannot be inlined.
  • If a routine marked with inline uses external symbols from other units, all of those units must be listed in the uses statement, otherwise the routine cannot be inlined.
  • Procedures and functions used in conditional expressions in while-do and repeat-until statements cannot be expanded inline.
  • Within a unit, the body for an inline function should be defined before calls to the function are made. Otherwise, the body of the function, which is not known to the compiler when it reaches the call site, cannot be expanded inline.

根据您的情况,请检查此条件:

Procedures and functions used in conditional expressions in while-do and repeat-until statements cannot be expanded inline.

关于delphi - Delphi 什么时候尊重 `inline`,什么时候不尊重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6401764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com