[PATCH] Move management operator
Original Reporter info from Mantis: ryan_joseph @genericptr
-
Reporter name: Ryan Joseph
Original Reporter info from Mantis: ryan_joseph @genericptr
- Reporter name: Ryan Joseph
Description:
Introduces a new "Move" management operator which works as a companion to "Copy" to improve efficiency.
Currently during all assignments the Copy operator is called, even if the record being copied is "temporary" and thus doesn't have a static memory address. With the proposed patch the Move operator will now be called in these situations which gives the programmer the option to simply move memory instead of performing an actual copy. If the Move operator is not present the Copy operator will be called always.
Additional information:
{$mode objfpc}
{$modeswitch advancedrecords}
program test;
type
TMyRecord = record
data: TData;
constructor Create(val: integer);
class operator Copy(constref aSrc: TMyRecord; var aDst: TMyRecord);
class operator Move(constref aSrc: TMyRecord; var aDst: TMyRecord);
end;
constructor TMyRecord.Create(val: integer);
begin
data := TData.Create;
end;
class operator TMyRecord.Copy(constref aSrc: TMyRecord; var aDst: TMyRecord);
begin
{ ...do something... }
aDst.data := TData.Create;
end;
class operator TMyRecord.Move(constref aSrc: TMyRecord; var aDst: TMyRecord);
begin
aSrc.data := ADst.data;
end;
operator := (right: integer): TMyRecord;
begin
{ Move operator called because right side is temporary }
result := TMyRecord.Create(right);
end;
var
a, b: TMyRecord;
begin
{ Move operator is called because the right side from the
:= operator is temporary. In total the move operator is
called twice in this line saving two copies }
a := 1;
{ Copy operator is called because "a" is static memory }
b := a;
end.
Mantis conversion info:
- Mantis ID: 35825
- Version: 3.3.1
- Monitored by: » @genericptr (Ryan Joseph), » @MageSlayer (Denis Golovan)