Difference between revisions of "Inline"

From Lazarus wiki
Jump to navigationJump to search
(inline is not a reserved word, expand article)
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.
  
Return to [[Reserved words]].
+
== use ==
 
+
The use of <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight> routines is switched off by default.
 +
You can enable it with the <syntaxhighlight lang="bash" inline>‑Si</syntaxhighlight> compiler switch or the <syntaxhighlight lang="delphi" inline>{$inline on}</syntaxhighlight> [[local compiler directives|local compiler directive]].
 +
The <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight> directive is placed after a routine’s signature at its defining point.
 +
Example:
 +
<syntaxhighlight lang="pascal" highlight="1">
 +
function cube(const x: ALUSInt): ALUSInt; inline;
 +
begin
 +
cube := sqr(x) * x;
 +
end;
 +
</syntaxhighlight>
  
The reserved word '''inline''' allows the compiler to copy a function or procedure in place of its call.
+
== implications ==
 +
Inlining means that a routine’s implementation exists at ''multiple'' places in the final [[Executable program|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.
  
If the inline function or procedure is used frequently, this increases the speed of the program since the program does not have to branch to the subroutine.
+
=== 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 [[Variable parameter|parameters passed by reference]].
  
The use of inline functions and inline procedures is switched off as standard. The compiler switch '''-Si''' or '''{$inline on}''' enables the use of inline functions and procedures.
+
=== 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.
  
{{Warning| inline is a compiler hint and 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}}
+
== caveats ==
 +
* <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight> is a compiler ''hint''. The compiler can ignore it. If the compiler warns you it has not inlined a certain code part marked as <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight>, you should remove the <syntaxhighlight lang="pascal" inline>inline</syntaxhighlight> directive. This is not a bug; it is about code complexity.
 +
* 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.
  
 
+
== see also ==
Example:
+
* [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
<syntaxhighlight lang=pascal>
 
procedure subDemo(); inline;
 
  begin
 
  ...
 
  end;
 
</syntaxhighlight>
 

Revision as of 11:50, 18 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.

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.

see also