Difference between revisions of "Inline"

From Lazarus wiki
Jump to navigationJump to search
(inline is not a reserved word, expand article)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{inline}}
 
{{inline}}
  
The [[modifier]] <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight> requests the [[FPC]] to consider copying the definition of a [[Routine|routine]] to the call site.
+
The [[modifier]] <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight> requests the [[FPC]] to consider copying the definition of a [[Routine|routine]] to the call site (“inlining”).
 +
The modifier <syntaxhighlight lang="pascal" inline>noinline</syntaxhighlight> prevents the FPC from ever inlining a routine, even automatically (since {{gitlab|commit|FPC|503ea604f33b5a7dd72d7a6417f9a38774f19263|SVN revision 41198}}, as of 2022 only available in Trunk).
  
 
== use ==
 
== use ==
Line 26: Line 27:
 
* More difficult to debug: There is no extra frame on the stack indicating the subroutine.
 
* 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.
 
* 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 <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight>: You cannot ask for inlining ''just'' at specific places (e.&#8239;g. in a loop).
  
 
== caveats ==
 
== caveats ==
Line 31: Line 33:
 
* 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 ==
 +
If you want to optimize for speed, consider trying [[Optimization|<syntaxhighlight lang="delphi" inline>{$optimization autoInline}</syntaxhighlight>]] first, before manually adding <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight> hints.
  
 
== 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

Latest revision as of 14:22, 8 November 2023

Deutsch (de) English (en)

The modifier inline requests the FPC to consider copying the definition of a routine to the call site (“inlining”). The modifier noinline prevents the FPC from ever inlining a routine, even automatically (since SVN revision 41198, as of 2022 only available in Trunk).

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

If you want to optimize for speed, consider trying {$optimization autoInline} first, before manually adding inline hints.

see also