Recent

Author Topic: Inline: does it work in this case?  (Read 3217 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2386
    • UVviewsoft
Inline: does it work in this case?
« on: October 16, 2018, 07:07:35 pm »
unit BB with class T1.

Code: Pascal  [Select][+][-]
  1. type T1 = class
  2. private
  3.   FList: TList;
  4. public
  5.   function Count: integer; inline;
  6.   ...
  7. end;
  8.  
  9. function T1.Count: integer; inline;
  10. begin
  11.   Result:= FList.Count;
  12. end;
  13.  

now t:T1 object is created from another unit AA. t.Count is called.
will this t.Count call be "inline" or not? if yes, how?
« Last Edit: October 16, 2018, 07:23:13 pm by Alextp »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Inline: does it work in this case?
« Reply #1 on: October 16, 2018, 07:17:04 pm »
IMO it also depends on complexity of the function. Anyway, compiler (3.3.1) gives hints during compilation, if some inline function is not inlined.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

AlexTP

  • Hero Member
  • *****
  • Posts: 2386
    • UVviewsoft
Re: Inline: does it work in this case?
« Reply #2 on: October 16, 2018, 07:24:20 pm »
I posted more complete example (same post). Count reads private var FList. I use FPC 3.0.4, no hints for inline.

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Inline: does it work in this case?
« Reply #3 on: October 16, 2018, 08:00:17 pm »
unit BB with class T1.

Code: Pascal  [Select][+][-]
  1. type T1 = class
  2. private
  3.   FList: TList;
  4. public
  5.   function Count: integer; inline;
  6.   ...
  7. end;
  8.  
  9. function T1.Count: integer; inline;
  10. begin
  11.   Result:= FList.Count;
  12. end;
  13.  

now t:T1 object is created from another unit AA. t.Count is called.
will this t.Count call be "inline" or not? if yes, how?

The last time I tried trunk, such cases like this don't inline because FList is private, although 3.0.4 doesn't warn about that.
But making FList public will allows inlining to work.
Calling t.Count from unit BB will inline though whether private or not.
« Last Edit: October 16, 2018, 08:03:40 pm by Xor-el »

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Inline: does it work in this case?
« Reply #4 on: October 16, 2018, 08:14:36 pm »
Indeed. To be more precise: inlining for private is not possible outside unit scope.
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Inline: does it work in this case?
« Reply #5 on: October 16, 2018, 08:35:41 pm »
Simple example
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. unit Unit1;
  3.  
  4. interface
  5.  
  6. uses Classes;
  7.  
  8. type
  9.   T1 = class(TObject)
  10.   private
  11.     FList: TList;
  12.     function GetCount: Integer; inline;
  13.   public
  14.     constructor Create;
  15.     destructor Destroy; override;
  16.     property Count: Integer read GetCount;
  17.   end;
  18.  
  19.  
  20. implementation
  21.  
  22. constructor T1.Create;
  23. begin
  24.   inherited;
  25.   FList := TList.Create;
  26.   FList.Add(nil);
  27. end;
  28.  
  29. destructor T1.Destroy;
  30. begin
  31.   FList.Free;
  32.   inherited;
  33. end;
  34.  
  35. function T1.GetCount: Integer;
  36. begin
  37.   Result := FList.Count;
  38. end;
  39.  
  40. end.

Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3. program Project1;
  4.  
  5. uses unit1;
  6.  
  7. var
  8.   C1: T1;
  9.   Cnt: Integer;
  10. begin
  11.   C1 := T1.Create;
  12.   try
  13.     Cnt := C1.Count;
  14.     Writeln(Cnt);
  15.   finally
  16.     C1.Free;
  17.   end;
  18.   Readln;
  19. end.
Even with the optimization turned off, the source code on the assembler:
Code: ASM  [Select][+][-]
  1. # [13] Cnt := C1.Count;
  2.         movq    U_$P$PROJECT1_$$_C1(%rip),%rax
  3.         movq    8(%rax),%rcx
  4.         call    CLASSES$_$TLIST_$__$$_GETCOUNT$$LONGINT
  5.         movl    %eax,U_$P$PROJECT1_$$_CNT(%rip)
That is, the code is inlined.
FPC 3.0.4, Win64.

By the way, Delphi inlining this code only if there is "uses Classes" in the place of the call.

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: Inline: does it work in this case?
« Reply #6 on: October 19, 2018, 01:01:32 am »
You can always also check with https://godbolt.org, just select Pascal as language.

 

TinyPortal © 2005-2018