Recent

Author Topic: Please Verify:Does not generate a COPY  (Read 655 times)

jamie

  • Hero Member
  • *****
  • Posts: 7302
Please Verify:Does not generate a COPY
« on: October 17, 2025, 12:22:26 am »
In my search for perfection and the dream of having a real reference option for the Result value, I came up with this.

Does this not create a copy of a Record?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode Delphi}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27. {$R *.lfm}
  28. Type
  29.   TTestRec = Record
  30.     A:Array[0..1000] of Double;
  31.   end;
  32.  
  33. Function ClearIt<T>:T;
  34.    Procedure InnerTest(Out A:T);
  35.     Begin
  36.       FillByte(A, SizeOF(T), 0);
  37.     end;
  38.  Begin
  39.    InnerTest(result);
  40.  end;
  41.  
  42. { TForm1 }
  43.  
  44. procedure TForm1.Button1Click(Sender: TObject);
  45. Var
  46.   P:TTestRec;
  47. begin
  48.   P.A[999]:=1000.10;
  49.  P:= ClearIt<TTestRec>;
  50.   Caption := P.A[999].Tostring;
  51. end;
  52.  
  53. end.
  54.  
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7302
Re: Please Verify:Does not generate a COPY
« Reply #1 on: October 17, 2025, 12:41:16 am »
I just rearranged it and now I got that to inline. It generates a direct FillByye!

Code: Pascal  [Select][+][-]
  1. Procedure InnerTest<T>(Out A:T); inline;
  2.     Begin
  3.       FillByte(A, SizeOF(T), 0);
  4.     end;
  5. Function ClearIt<T>:T;  Inline;
  6.  Begin
  7.    InnerTest<T>(result);
  8.  end;
  9.  
  10. { TForm1 }
  11.  
  12. procedure TForm1.Button1Click(Sender: TObject);
  13. Var
  14.   P:TTestRec;
  15. begin
  16.   P.A[999]:=1000.10;
  17.  P:= ClearIt<TTestRec>;
  18.   Caption := P.A[999].Tostring;
  19. end;                          
  20.  
  21.  

Quote
unit1.pas:48  P:= ClearIt<TTestRec>;
000000010002F4C5 488D8DA8E0FFFF           lea rcx,[rbp-$00001F58]
000000010002F4CC 4531C0                         xor r8d,r8d
000000010002F4CF BA481F0000                  mov edx,$00001F48
000000010002F4D4 E84741FDFF                  call -$0002BEB9    # $0000000100003620   SYSTEM_$$_FILLBYTE$formal$INT64$BYT
unit1.pas:49  Caption := P.A[999].Tostring;
000000010002F4D9 4C8D0510161600           lea r8,[rip+$00161610]    # $0000000100190AF0

The only true wisdom is knowing you know nothing

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 283
  • I use FPC [main] 💪🐯💪
Re: Please Verify:Does not generate a COPY
« Reply #2 on: October 17, 2025, 05:06:06 am »
Yes, it would be good if it worked in such cases, but unfortunately, it does not work in this particular case:

Code: Pascal  [Select][+][-]
  1. program app;
  2. {$ifdef FPC}{$mode delphi}{$endif}
  3.  
  4. type
  5.   TRec = record
  6.     A,B,C,D,E,F,G: Double;
  7.     class procedure TRecAdd(const L, R: TRec; out O: TRec); static; inline;
  8.     class operator Add(const L, R: TRec): TRec; inline;
  9.   end;
  10.  
  11. class procedure TRec.TRecAdd(const L, R: TRec; out O: TRec);
  12. begin
  13.   O.A := L.A + R.A;
  14.   O.B := L.B + R.B;
  15.   O.C := L.C + R.C;
  16.   O.D := L.D + R.D;
  17.   O.E := L.E + R.E;
  18.   O.F := L.F + R.F;
  19.   O.G := L.G + R.G;
  20. end;
  21.  
  22. class operator TRec.Add(const L, R: TRec): TRec;
  23. begin
  24.   TRecAdd(L, R, Result);
  25. end;
  26.  
  27. var
  28.   R1, R2, R3: TRec;
  29.  
  30. begin
  31.   R1 := R2 + R3;
  32. end.
« Last Edit: October 17, 2025, 05:09:54 am by ALLIGATOR »
I may seem rude - please don't take it personally

jamie

  • Hero Member
  • *****
  • Posts: 7302
Re: Please Verify:Does not generate a COPY
« Reply #3 on: October 17, 2025, 11:49:58 am »
Its a procedure what do you expect?

It does exactly what i was shooting for to replace the use of default.

Now i can use this as the way default(type) works without a copy being made and then a move afterwards 

Jamie
The only true wisdom is knowing you know nothing

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 283
  • I use FPC [main] 💪🐯💪
Re: Please Verify:Does not generate a COPY
« Reply #4 on: October 17, 2025, 01:48:37 pm »
for to replace the use of default
Oh, sorry, now I understand
I may seem rude - please don't take it personally

jamie

  • Hero Member
  • *****
  • Posts: 7302
Re: Please Verify:Does not generate a COPY
« Reply #5 on: October 19, 2025, 03:06:14 pm »
I wanted to let you know that I ran that code of yours in the 64 bit version and it does seem to work, with the exception of the last copy move which I don't know why its there but its only one move which seems not to be relevent to it.

Code: Pascal  [Select][+][-]
  1. 00000001000015DC F20F10056C0A0100         movsd xmm0,qword ptr [rip+$00010A6C]    # $0000000100012050
  2. 00000001000015E4 F20F5805A40A0100         addsd xmm0,[rip+$00010AA4]    # $0000000100012090
  3. 00000001000015EC F20F11442420             movsd [rsp+$20],xmm0
  4. 00000001000015F2 F20F10055E0A0100         movsd xmm0,qword ptr [rip+$00010A5E]    # $0000000100012058
  5. 00000001000015FA F20F5805960A0100         addsd xmm0,[rip+$00010A96]    # $0000000100012098
  6. 0000000100001602 F20F11442428             movsd [rsp+$28],xmm0
  7. 0000000100001608 F20F1005500A0100         movsd xmm0,qword ptr [rip+$00010A50]    # $0000000100012060
  8. 0000000100001610 F20F5805880A0100         addsd xmm0,[rip+$00010A88]    # $00000001000120A0
  9. 0000000100001618 F20F11442430             movsd [rsp+$30],xmm0
  10. 000000010000161E F20F1005420A0100         movsd xmm0,qword ptr [rip+$00010A42]    # $0000000100012068
  11. 0000000100001626 F20F58057A0A0100         addsd xmm0,[rip+$00010A7A]    # $00000001000120A8
  12. 000000010000162E F20F11442438             movsd [rsp+$38],xmm0
  13. 0000000100001634 F20F1005340A0100         movsd xmm0,qword ptr [rip+$00010A34]    # $0000000100012070
  14. 000000010000163C F20F58056C0A0100         addsd xmm0,[rip+$00010A6C]    # $00000001000120B0
  15. 0000000100001644 F20F11442440             movsd [rsp+$40],xmm0
  16. 000000010000164A F20F1005260A0100         movsd xmm0,qword ptr [rip+$00010A26]    # $0000000100012078
  17. 0000000100001652 F20F58055E0A0100         addsd xmm0,[rip+$00010A5E]    # $00000001000120B8
  18. 000000010000165A F20F11442448             movsd [rsp+$48],xmm0
  19. 0000000100001660 F20F1005180A0100         movsd xmm0,qword ptr [rip+$00010A18]    # $0000000100012080
  20. 0000000100001668 F20F5805500A0100         addsd xmm0,[rip+$00010A50]    # $00000001000120C0
  21. 0000000100001670 F20F11442450             movsd [rsp+$50],xmm0
  22. 0000000100001676 488D3D93090100           lea rdi,[rip+$00010993]    # $0000000100012010
  23. 000000010000167D 488D742420               lea rsi,[rsp+$20]
  24. 0000000100001682 B907000000               mov ecx,$00000007
  25. 0000000100001687 F348A5                   rep movsq
  26.  
  27.  

Those are all the operations you did in the add.

I'll look into it later for a possible solution.

jamie

« Last Edit: October 19, 2025, 03:08:41 pm by jamie »
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 6184
  • Compiler Developer
Re: Please Verify:Does not generate a COPY
« Reply #6 on: October 20, 2025, 09:24:40 pm »
I just rearranged it and now I got that to inline. It generates a direct FillByye!

This is in no way guaranteed. This is an implementation detail that could change any time some change in the compiler happens or if the compiler decides not to inline the code.

 

TinyPortal © 2005-2018