[fpc-pascal] How to avoid Copy

Michael Van Canneyt michael at freepascal.org
Mon Jan 1 09:50:03 CET 2024



On Mon, 1 Jan 2024, Hairy Pixels via fpc-pascal wrote:

>
>
>> On Dec 30, 2023, at 3:18 PM, Michael Van Canneyt via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>> 
>> And try to avoid "for r in data" because it will create a copy for each element
>> in the list.
>
> This seems more like bad compiler design than anything.  From the users
> perspective a for loop is a read-only operation and shouldn't be copying
> anything.  I know it's how the enumerator thing is done but that may have
> been a bad design from the start or needs some extra optimization
> somewhere.

You can't optimize that. As said, a generic is convenient but slow.

>
> For OP I would just kill the for-in loop in favor of a normal for-do loop and be done with the problem.

That will not help, since he is using a TList<TMyRecord>, it cannot work without
copy: any read implies a copy. Unless you use a pointer as Marco and I suggested.

Michael.


More information about the fpc-pascal mailing list