什么时候Delphi荣誉`inline`,何时不? [英] When does Delphi honor `inline` and when not?

查看:286
本文介绍了什么时候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

这不行,因为Dec是一个过程而不是一个函数。

That doesn't work because Dec is a procedure and not a function.

所以我将其重写为:

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知道什么经验法则(或硬规则)可以兑现code> inline 指令?

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?

推荐答案

Delphi Documentation 列举了内联行为或不发生的条件:

The Delphi Documentation enumerates the conditions under which inlining does or does not occur:



  • 任何形式的后期绑定方法都不会出现内联。这包括
    虚拟,动态和消息方法。

  • 包含汇编代码的例程不会被内联。

  • 构造函数和析构函数不会

  • 主程序块,单元初始化和单元最终化
    块不能内联。

  • 未定义的例程使用前不能内联。

  • 采用开放数组参数的例程不能内联。

  • 代码可以在程序包内嵌,但是内联不会发生在
    包装边界之间。

  • 在循环依赖的单位之间不进行内联。这个
    包括间接循环
    依赖,例如,单位A使用
    单位B,单位B使用单位C,
    依次使用单位A.在本例中,
    编译单元A时,
    单元B或单元C中的代码不会在
    单元A中内联。

  • 编译器可以在单元内置代码在循环依赖中,作为
    ,只要内联的代码来自圆形
    关系之外的单元
    。在上面的例子中,如果
    单位A也使用单元D,那么在
    单元D中的代码可以在A中被内联,因为
    不涉及循环
    依赖。 / li>
  • 如果在接口部分定义了例程,并且访问
    部分中定义的
    符号,则该例程不能为
    内联。 >
  • 如果标有内联的例程使用其他单位的外部符号,则这些单位的所有
    必须列在
    用途语句中,否则例程
    不能

  • while-do
    和repeat-until语句中的条件表达式中使用的程序和函数不能以
    的形式内联。

  • 在一个单位内,内部函数的主体应该在调用函数之前定义

    否则,函数的正文,
    当编译器
    到达调用站点时不知道,不能
    在内部扩展。

  • 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.

在您的情况下,请检查以下条件:

In your case check this condition:


在while-do和repeat-until语句中的条件表达式中使用的过程和函数不能内联扩展。

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

这篇关于什么时候Delphi荣誉`inline`,何时不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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