gdeque unit: const params lazarus / 62-General ---------------------------------------------------------------------- AlexTP · 2020-09-08 21:53 https://github.com/graemeg/freepascal/blob/master/packages/fcl- stl/src/gdeque.pp     procedure SetValue(position:SizeUInt; value:T);inline;     procedure PushBack(value:T);inline;     procedure PushFront(value:T);inline;     procedure Insert(Position:SizeUInt; Value:T);inline; My deque elements will be few Kb in size. Isn't it better to use "const value: T" or even "constref value: T"? ---------------------------------------------------------------------- jamie · 2020-09-09 01:51 looks like more vector code... Its like your first hello world program.. Everyone has to write a vector tree just to see if the compiler can do it, code too. at least that's what it looks like... My first vector was C++ ---------------------------------------------------------------------- Akira1364 · 2020-09-09 01:58 Quote from: Alextp on September 08, 2020, 11:53:42 pmIsn't it better to use "const value: T" or even "constref value: T"? Yeah. I'd recommend the Dequeue from LGenerics instead honestly: https://github.com/avk959/LGenerics/blob/master/lgenerics/LGDeque.pas ---------------------------------------------------------------------- AlexTP · 2020-09-09 07:23 From LGenerics? It will get few more dependencies and code bloat. So I use gdeque. ---------------------------------------------------------------------- PascalDragon · 2020-09-09 07:35 Quote from: Alextp on September 08, 2020, 11:53:42 pmIsn't it better to use "const value: T" or even "constref value: T"? It's recommended to use const instead of constref as then the compiler can pick the optimal passing for the type (e.g. constref would pass an UInt32 as reference even if the calling convention would allow to pass it in a register). ---------------------------------------------------------------------- AlexTP · 2020-09-09 08:37 So let's change it to "const" in FPC? ---------------------------------------------------------------------- Thaddy · 2020-09-09 10:23 Yes, but leave it to the author: forum member. ---------------------------------------------------------------------- ASerge · 2020-09-09 15:47 Quote from: Alextp on September 09, 2020, 09:23:07 amIt will get few more dependencies and code bloat. Regarding the code bloat. This module clearly shows the excessive use of the "inline" directive. It is often placed wherever the code "looks" small, but it is not. Let's take a simple example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC}{$APPTYPE CONSOLE} uses gdeque; type  TDequeString = specialize TDeque;var  A: TDequeString;begin  A := TDequeString.Create;  try    A.PushBack('Test');    Writeln(A.Back);  finally    A.Free;  end;end.Let's examine the generated code (FPC 3.2.0, x64, Intel, optimization level 3) for the Create: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [13] A := TDequeString.Create;        movl    $1,%eax        leaq    VMT_$P$PROJECT1_$$_TDEQUE$1$CRC6824777A(%rip),%rcx        movq    %rax,%rdx        call    P$PROJECT1$_$TDEQUE$1$CRC6824777A_$__$$_CREATE$$TDEQUE$1$CRC6824777A      movq    %rax,U_$P$PROJECT1_$$_A(%rip) Small and adequate - because it's not "inline". Now let's look at the PushBack: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [15] A.PushBack('Test');        leaq    -8(%rbp),%rcx        call    fpc_ansistr_decr_ref        leaq    .Ld2(%rip),%rdx        leaq    -8(%rbp),%rcx        call    fpc_ansistr_assign.Ll182:        movq    U_$P$PROJECT1_$$_A(%rip),%rdx.Ll183:        movq    16(%rdx),%rax      cmpq    24(%rdx),%rax.Ll184:        jne     .Lj254.Ll185:        movq    U_$P$PROJECT1_$$_A(%rip),%rax        movq    24(%rax),%rbx      movq    U_$P$PROJECT1_$$_A(%rip),%rax        movq    24(%rax),%rax        shlq    $3,%rax        movq    U_$P$PROJECT1_$$_A(%rip),%rdx      cmpq    $0,24(%rdx)        jne     .Lj257        movq    U_$P$PROJECT1_$$_A(%rip),%rdx        movq    $4,24(%rdx)        jmp   .Lj258.Lj257:        cmpq    $1048576,%rax        jnb     .Lj260.Ll186:        movq    U_$P$PROJECT1_$$_A(%rip),%rcx.Ll187:      shlq    $1,24(%rcx)        jmp     .Lj261.Lj260:        cmpq    $268435456,%rax        jnb     .Lj263.Ll188:        movq    U_$P$PROJECT1_$$_A(%rip),%rcx        movq    24(%rcx),%rdx.Ll189:      movq    %rdx,%rax        shrq    $3,%rax        addq    %rdx,%rax      movq    %rax,24(%rcx)        jmp     .Lj264.Lj263:.Ll190:        movq    U_$P$PROJECT1_$$_A(%rip),%rcx        movq    24(%rcx),%rdx.Ll191:        movq    %rdx,%rax        shrq    $4,%rax      addq    %rdx,%rax        movq    %rax,24(%rcx).Lj264:.Lj261:.Lj258:        movq    U_$P$PROJECT1_$$_A(%rip),%rax        movq    24(%rax),%rax        movq    %rax,-16(%rbp)        leaq    RTTI_$P$PROJECT1_$$_def00000002(%rip),%rdx        movq    U_$P$PROJECT1_$$_A(%rip),%rax        leaq    8(%rax),%rcx        leaq    -16(%rbp),%r9        movl    $1,%r8d        call    fpc_dynarray_setlength        movq    U_$P$PROJECT1_$$_A(%rip),%rax      cmpq    $0,32(%rax)        jna     .Lj266        movq    U_$P$PROJECT1_$$_A(%rip),%rax        movq    32(%rax),%rax        leaq    -1(%rax),%rsi        movq    $-1,%rdi        .balign 8,0x90.Lj267:        addq    $1,%rdi        movq    U_$P$PROJECT1_$$_A(%rip),%rax        movq    8(%rax),%rax        movq    (%rax,%rdi,8),%rdx        movq    U_$P$PROJECT1_$$_A(%rip),%rax      movq    8(%rax),%rcx        leaq    (%rbx,%rdi),%rax        leaq    (%rcx,%rax,8),%rcx        call    fpc_ansistr_assign        cmpq    %rdi,%rsi        jnbe    .Lj267        .balign 4,0x90.Lj266:        .balign 4,0x90.Lj254:.Ll192:        movq    U_$P$PROJECT1_$$_A(%rip),%rcx.Ll193:        movq    8(%rcx),%r8      movq    32(%rcx),%rdx        movq    16(%rcx),%rax        addq    %rdx,%rax        xorl    %edx,%edx        divq    24(%rcx)        leaq    (%r8,%rdx,8),%rcx        movq    -8(%rbp),%rdx        call    fpc_ansistr_assign        movq    U_$P$PROJECT1_$$_A(%rip),%rax      addq    $1,16(%rax)And so it will be for every PushBack. Let me remind: the 3rd level of optimization! Library authors should take a closer look at when to use the inline directive. ---------------------------------------------------------------------- PascalDragon · 2020-09-10 07:28 Quote from: Thaddy on September 09, 2020, 12:23:58 pmYes, but leave it to the author: forum member. It's now mainly maintained by the FPC devs. So if someone provides a patch and reports it it's likely going to be applied. ---------------------------------------------------------------------- marcov · 2020-09-10 09:25 Aserge: how does the same look with a pure value type? This case would need some auto-inline decision making then. (or ignoring the hint in the managed type case because it gets too complex) ---------------------------------------------------------------------- ASerge · 2020-09-10 18:25 Quote from: marcov on September 10, 2020, 11:25:05 amAserge: how does the same look with a pure value type? Almost the same. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC}{$APPTYPE CONSOLE} uses gdeque; type  TDequeInteger = specialize TDeque;var  A: TDequeInteger;begin  A := TDequeInteger.Create;  try    A.PushBack(100);    Writeln(A.Back);  finally    A.Free;  end;end.Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [11] A := TDequeInteger.Create;        movl    $1,%eax      leaq    VMT_$P$PROGRAM_$$_TDEQUE$1$CRC9F312717(%rip),%rcx        movq    %rax,%rdx        call    P$PROGRAM$_$TDEQUE$1$CRC9F312717_$__$$_CREATE$$TDEQUE$1$CRC9F312717      movq    %rax,U_$P$PROGRAM_$$_A(%rip)Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [13] A.PushBack(100);        movq    16(%rdx),%rax        cmpq    24(%rdx),%rax.Ll149:        jne     .Lj207.Ll150:        movq    U_$P$PROGRAM_$$_A(%rip),%rax        movq    24(%rax),%rbx        movq    U_$P$PROGRAM_$$_A(%rip),%rax        movq    24(%rax),%rax        shlq    $2,%rax        movq    U_$P$PROGRAM_$$_A(%rip),%rdx        cmpq    $0,24(%rdx)        jne     .Lj210        movq    U_$P$PROGRAM_$$_A(%rip),%rdx        movq    $4,24(%rdx)        jmp   .Lj211.Lj210:        cmpq    $1048576,%rax        jnb     .Lj213.Ll151:        movq    U_$P$PROGRAM_$$_A(%rip),%rcx.Ll152:      shlq    $1,24(%rcx)        jmp     .Lj214.Lj213:        cmpq    $268435456,%rax        jnb     .Lj216.Ll153:        movq    U_$P$PROGRAM_$$_A(%rip),%rcx        movq    24(%rcx),%rdx.Ll154:      movq    %rdx,%rax        shrq    $3,%rax        addq    %rdx,%rax      movq    %rax,24(%rcx)        jmp     .Lj217.Lj216:.Ll155:        movq    U_$P$PROGRAM_$$_A(%rip),%rcx        movq    24(%rcx),%rdx.Ll156:        movq    %rdx,%rax        shrq    $4,%rax      addq    %rdx,%rax        movq    %rax,24(%rcx).Lj217:.Lj214:.Lj211:        movq    U_$P$PROGRAM_$$_A(%rip),%rax        movq    24(%rax),%rax        movq    %rax,-8(%rbp)        leaq    RTTI_$P$PROGRAM_$$_def00000002(%rip),%rdx        movq    U_$P$PROGRAM_$$_A(%rip),%rax        leaq    8(%rax),%rcx        leaq  -8(%rbp),%r9        movl    $1,%r8d        call    fpc_dynarray_setlength        movq    U_$P$PROGRAM_$$_A(%rip),%rax      cmpq    $0,32(%rax)        jna     .Lj219        movq    U_$P$PROGRAM_$$_A(%rip),%rax        movq    32(%rax),%rax        subq    $1,%rax        movq    $-1,%rdx        .balign 8,0x90.Lj220:        addq    $1,%rdx.Ll157:        movq    U_$P$PROGRAM_$$_A(%rip),%rcx.Ll158:        movq    8(%rcx),%r9        leaq    (%rbx,%rdx),%r8        movq    8(%rcx),%rcx        movl    (%rcx,%rdx,4),%ecx        movl    %ecx,(%r9,%r8,4)        cmpq    %rdx,%rax        jnbe    .Lj220        .balign 4,0x90.Lj219:        .balign 4,0x90.Lj207:.Ll159:        movq    U_$P$PROGRAM_$$_A(%rip),%rcx.Ll160:        movq    8(%rcx),%r8        movq    32(%rcx),%rdx        movq    16(%rcx),%rax        addq    %rdx,%rax        xorl    %edx,%edx        divq    24(%rcx)        movl    $100,(%r8,%rdx,4)        movq    U_$P$PROGRAM_$$_A(%rip),%rax        addq    $1,16(%rax).Ll161: ---------------------------------------------------------------------- Akira1364 · 2020-09-11 14:01 Quote from: ASerge on September 09, 2020, 05:47:27 pmQuote from: Alextp on September 09, 2020, 09:23:07 amIt will get few more dependencies and code bloat. Regarding the code bloat. This module clearly shows the excessive use of the "inline" directive. It is often placed wherever the code "looks" I mean, what they were saying is that they think the TGDeque from LGenerics (a third party library) would bloat their code moreso than using FPC's TDeque. Also, as far the inline, the problem there is the fact that PushBack calls IncreaseCapacity, and IncreaseCapacity is marked as inline when it really shouldn't be. It's definitely fine for PushBack itself to be inline, though.