[fpc-pascal] Setting record values

Ryan Joseph ryan at thealchemistguild.com
Tue Apr 4 04:54:34 CEST 2017


Thanks for the tips, I appreciate it.

This is all pretty trivial but it’s kind of annoying that using an inline class function is more efficient than a constructor despite having identical functionality. It's tempting to remove the constructors now and replace them with inline functions but it seems like the compiler should be smarter and make this optimization for me. 

> On Apr 2, 2017, at 10:51 PM, Sven Barth via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> 
> The compiler keeps the assembler code with line information around if
> you pass "-al".
> 
> If you are so concerned about the differences in performance regarding
> using a setter and a construction function you should really learn at
> least in principle how typical assembler code generated by the compiler
> looks like (same for your query about dynamic arrays by the way, though
> there you should also take a look at the implementation of the RTL).
> 
> Here is an example compiled without optimizations aside from inline:
> 
> === code begin ===
> 
> # [47] p.&Set(42, 21);
>        movq    $U_$P$TRECFUNCS_$$_P,%rax
>        movl    $21,%edx
>        movl    $42,%esi
>        movq    %rax,%rdi
>        call    P$TRECFUNCS$_$TPOINT_$__$$_SET$LONGINT$LONGINT
> # [48] p := TPoint.Make(42, 21);
>        leaq    -8(%rbp),%rdi
>        movl    $21,%edx
>        movl    $42,%esi
>        call    P$TRECFUNCS$_$TPOINT_$__$$_MAKE$LONGINT$LONGINT$$TPOINT
>        movq    -8(%rbp),%rax
>        movq    %rax,U_$P$TRECFUNCS_$$_P
> # [49] p := TPoint.Make2(42, 21);
>        movl    $42,U_$P$TRECFUNCS_$$_P
>        movl    $21,U_$P$TRECFUNCS_$$_P+4
> # [50] p := MakePoint(42, 21);
>        movl    $21,%esi
>        movl    $42,%edi
>        call    P$TRECFUNCS_$$_MAKEPOINT$LONGINT$LONGINT$$TPOINT
>        movq    %rax,U_$P$TRECFUNCS_$$_P
> # [51] p := MakePoint2(42, 21);
>        movl    $42,U_$P$TRECFUNCS_$$_P
>        movl    $21,U_$P$TRECFUNCS_$$_P+4
> 
> === code end ===
> 
> "&Set" is essentially your "SetPoint" method. "Make" is a constructor.
> "Make2" is a static class function with "inline". "MakePoint" is your
> creation function and "MakePoint2" is the same with an inline modifier.
> 
> As you can see the two inline variants ("Make2" and "MakePoint2") are
> the most effective as there's no call and only the two loads of the values.

Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list