Inline: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Inline
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The [[modifier]] `inline` requests the [[FPC]] to consider copying the definition of a [[Routine|routine]] to the call site (“inlining”). The modifier `noinline` prevents the FPC from ever inlining a routine, even automatically (since [SVN revision 41198](https://gitlab.com/freepascal.org/fpc/source/commit/503ea604f33b5a7dd72d7a6417f9a38774f19263), as of 2022 only available in Trunk). | |
| 2 | + | ||
| 3 | + | ## use | |
| 4 | + | ||
| 5 | + | 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 directives|local compiler directive]]. The `inline` directive is placed after a routine’s signature at its defining point. Example: | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | function cube(const x: ALUSInt): ALUSInt; inline; | |
| 9 | + | begin | |
| 10 | + | cube := sqr(x) * x; | |
| 11 | + | end; | |
| 12 | + | ``` | |
| 13 | + | ||
| 14 | + | ## implications | |
| 15 | + | ||
| 16 | + | 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. | |
| 17 | + | ||
| 18 | + | ### advantages | |
| 19 | + | ||
| 20 | + | - Avoid call overhead for frequently invoked routines. This *could* increase the speed of the program. | |
| 21 | + | - Elimination of an extra level of indirection in the case of [[Variable parameter|parameters passed by reference]]. | |
| 22 | + | ||
| 23 | + | ### disadvantages | |
| 24 | + | ||
| 25 | + | - More difficult to debug: There is no extra frame on the stack indicating the subroutine. | |
| 26 | + | - Inlining requires space. You will necessarily have numerous copies of the same code at many places. | |
| 27 | + | - 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). | |
| 28 | + | ||
| 29 | + | ## caveats | |
| 30 | + | ||
| 31 | + | - `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. | |
| 32 | + | - Recursive routines cannot be inlined. | |
| 33 | + | - Regardless of the `inline` request *there is* one instance the routine exists in memory like usual. | |
| 34 | + | ||
| 35 | + | ## application | |
| 36 | + | ||
| 37 | + | If you want to optimize for speed, consider trying [[Optimization|{$optimization autoInline}]] first, before manually adding `inline` hints. | |
| 38 | + | ||
| 39 | + | ## see also | |
| 40 | + | ||
| 41 | + | - [`inline`](https://www.freepascal.org/docs-html/ref/refsu77.html) in the FPC Reference Guide | |
| 42 | + | - [`$INLINE`: Allow inline code](https://www.freepascal.org/docs-html/prog/progsu36.html) in the FPC Programmers’ Guide | |
| 43 | + | ||
| 44 | + | --- | |
| 45 | + | *From the Free Pascal wiki, page "Inline", as it stood on 2023-11-08 after a change by Alextpp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
