Difference between revisions of "Inline"

From Lazarus wiki
Jump to navigationJump to search
(→‎disadvantages: no chance to fine-tune; supposedly add new external links [spam detection false positive])
Line 32: Line 32:
 
* Recursive routines cannot be inlined.
 
* Recursive routines cannot be inlined.
 
* Regardless of the <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight> request ''there is'' one instance the routine exists in memory like usual.
 
* Regardless of the <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight> request ''there is'' one instance the routine exists in memory like usual.
 +
 +
== application ==
 +
# ''Generally'' speaking, ''never'' use <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight>. According to Pascal’s “dogma” the programmer should not be concerned about such “internal” details. Any provision regarding memory or the generated code unnecessarily increases the complexity of your source code.
 +
# If you want to optimize for speed, consider [[Optimization|<syntaxhighlight lang="delphi" inline>{$optimization autoInline}</syntaxhighlight>]] first. In some decisions the compiler is smarter than you, because it has more information available. The automatic <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight> optimization also relieves you from the burden of making that decision and manually maintaining it.
 +
# “Only” if ''you'' have ''more'' information than the compiler, you are “allowed” to insert <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight>.
  
 
== see also ==
 
== see also ==
 
* [https://www.freepascal.org/docs-html/ref/refsu77.html <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight>] in the FPC Reference Guide
 
* [https://www.freepascal.org/docs-html/ref/refsu77.html <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight>] in the FPC Reference Guide
 
* [https://www.freepascal.org/docs-html/prog/progsu36.html <syntaxhighlight lang="text" inline>$INLINE</syntaxhighlight>: Allow inline code] in the FPC Programmers’ Guide
 
* [https://www.freepascal.org/docs-html/prog/progsu36.html <syntaxhighlight lang="text" inline>$INLINE</syntaxhighlight>: Allow inline code] in the FPC Programmers’ Guide

Revision as of 08:58, 19 August 2021

Deutsch (de) English (en)

The modifier inline requests the FPC to consider copying the definition of a routine to the call site.

use

The use of inline routines is switched off by default. You can enable it with the ‑Si compiler switch or the {$inline on} local compiler directive. The inline directive is placed after a routine’s signature at its defining point. Example:

function cube(const x: ALUSInt): ALUSInt; inline;
begin
	cube := sqr(x) * x;
end;

implications

Inlining means that a routine’s implementation exists at multiple places in the final executable file. There is no single address the program jumps to, but every time you invoke that routine there is dedicated copy in the source code.

advantages

  • Avoid call overhead for frequently invoked routines. This could increase the speed of the program.
  • Elimination of an extra level of indirection in the case of parameters passed by reference.

disadvantages

  • More difficult to debug: There is no extra frame on the stack indicating the subroutine.
  • Inlining requires space. You will necessarily have numerous copies of the same code at many places.
  • It is not possible to “fine tune” the use of inline: You cannot ask for inlining just at specific places (e. g. in a loop).

caveats

  • inline is a compiler hint. The compiler can ignore it. If the compiler warns you it has not inlined a certain code part marked as inline, you should remove the inline directive. This is not a bug; it is about code complexity.
  • Recursive routines cannot be inlined.
  • Regardless of the inline request there is one instance the routine exists in memory like usual.

application

  1. Generally speaking, never use inline. According to Pascal’s “dogma” the programmer should not be concerned about such “internal” details. Any provision regarding memory or the generated code unnecessarily increases the complexity of your source code.
  2. If you want to optimize for speed, consider {$optimization autoInline} first. In some decisions the compiler is smarter than you, because it has more information available. The automatic inline optimization also relieves you from the burden of making that decision and manually maintaining it.
  3. “Only” if you have more information than the compiler, you are “allowed” to insert inline.

see also