[fpc-devel] Management operators AddRef and Copy vs Copy

Maciej Izak hnb.code at gmail.com
Sun Apr 10 13:06:28 CEST 2016


Sven proposed that we need to discuss AddRef operator (important part of
smart pointers implementation).

AddRef is used when record is passed as parameter to method/function by
value (for records to large to copy (only the address is pushed)). AddRef
is used also for dynamic array operations (temporary for SetLength
operation and for Copy operation for already copied data by move).

AddRef is much faster than Copy (all data already exist, so we don't need
to copy anything). AddRef is used to increment ref count for managed types.

IMO the best solution is to implement two operators:

    class operator AddRef(var aFoo: TFoo);
    class operator Copy(constref aSrc: TFoo; var aDst: TFoo);

also is possible to implement single operator:

    class operator Copy(aSrc: PFoo; var aDst: TFoo);

aSrc in that case can be nil (for AddRef operation). Anyway that is
inelegant solution...

Any other solution means big performance loss for RTL operations for
dynamic arrays.

Best regards,
Maciej Izak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160410/1ffd9eac/attachment.html>

More information about the fpc-devel mailing list