can any one please say what is use of inline keyword in delphi
5 Answers
It is a hint to the compiler that the function/procedure should be (if possible) inlined, ie when you call it it should be expanded like a macro instead of being called.
This is an optimization for (very) small functions where the overhead of a call would be significant. You will find many examples in for example windows.pas
What actually happens depends on the complexity of the function, public/private access and your Delphi version.

- 263,252
- 30
- 330
- 514
-
1Is it really the compiler that actually takes care of this directive? I've tried to compile a unit using `Borland Delphi Version 15.0` but it couldn't find the inline directive. – Ben Jul 30 '14 at 09:39
-
inline is D2005+ but only works cross unit since D2006 iirc. And yes, the compiler does it. It is also not really like a macro (the inlining does not work on source level, but a level deeper, after parsing, on generated code level) – Marco van de Voort Nov 26 '16 at 15:10
-
1Is inline keyword supported in D7? I got message: [Error] RegExpr.pas(4155): Declaration expected but 'INLINE' found ... when using RegExpr library. – John Boe Dec 24 '18 at 17:46
-
1@user1141649 No. Delphi 7 does not support inline functions. You need Delphi 2005 or newer. – Tom Oct 10 '19 at 16:37
It tells the compiler to generate code for the inline; routine on the place where it is called, instead of jumping to the routine and back.
For procedures that translate to very short assembler, this can be a benefit to performance, because the actual code is relatively short compared to the parameter preparation, the actual calling and the procedure prologue/epilogue.
If the procedure is too long, it can be a brake on performance though, and blow up your code gigantically. The "Auto" setting should make this decision for you, but in specific cases, you can locally set {$inline to on to force it. (e.g. for C macros translated to pascal functions, like the zlib functions to work with bitstreams )

- 25,628
- 5
- 56
- 89
-
So, if I use {$Inline on} inside of a function body does it apply only to that function? Or do I have to call OFF at the end of the function? – Gabriel Mar 23 '21 at 09:35
-
1It sets a flag for the for the rest of the compilation unit(unit or main program) to inline functions that are marked with the inline directive. To turn the flag off, you need to turn it off. – Marco van de Voort Mar 23 '21 at 13:11
Others have answered what inline does, but I just wanted to point out that there is a Compiler option to set inline on, off, or auto. Check out "Calling Procedures and Functions" in the D2009 docs for a very good explanation of the mechanics of inline. Here's the link to the online docs:

- 1,616
- 1
- 17
- 27
-
`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.` The one that saved me. – SAMPro Dec 11 '21 at 20:36
It's borrowed from C in that it tells the compiler that this is a short routine that is frequently called and it recommends that the compiler treats the function as a macro and integrates the function code directly into the program at the point called rather than use a function call.
This gives faster code because there is no function call overhead, but at the expense of a larger program. Note too that like in C this is a recommendation to the compiler, it doesn't actually have to do so and the optimiser may override you.
Where to do this? Well like loop unwinding it's a technique that is very rarely of use nowadays. The classic place to use this is deep in a nested structure that is real-time critical, such as rendering code in a graphics application where a few machine calls shaved on each iteration can increase your screen refresh rate.

- 15,733
- 5
- 59
- 112
-
The D2005 inline is what you describe. The Delphi 2006+ inline can inline cross-compilation unit without declaring the function entirely in a header. That is much harrder and something that C standardly is not capable off (though certain globally optimizing systems try) – Marco van de Voort Apr 10 '17 at 10:32
Press Ctrl + Alt + C (Entire CPU debug window) while debugging in Delphi, before call your inline function. And you will see that inline functions starts without "call" and wo jumping to another address. It's optimization feature.

- 1
- 1