Recent

Author Topic: How to avoid copying  (Read 779 times)

Amir61

  • New Member
  • *
  • Posts: 28
    • http://Amir.Aavani.net
How to avoid copying
« on: January 17, 2025, 07:08:50 pm »
Hi,

 I have a record of records data structure, like, the following:
Code: Pascal  [Select][+][-]
  1. type TTuple = record
  2.   Entry: TEntry; // TEntry is a record
  3.   Other fields
  4. end;
  5.  
and I have a function that gets a 'TTuple' and does some stuff with its Entry. For my case, it does not make sense to define a separate function that gets 'Entry' as its input.

Code: Pascal  [Select][+][-]
  1. function F(constref Tuple: TTuple): Boolean;
  2.  

Since this function is going to be called a lot, in my code, I am looking for an efficient (and readable) implementation to access 'Tuple.Entry'.

Here are my few options (none of them is great!).
Code: Pascal  [Select][+][-]
  1. function F(constref Tuple: TTuple): Boolean;
  2. var
  3.   Entry: TEntry;
  4. begin
  5.   Entry := Tuple.Entry;
  6.   // Work with Entry rather than Tuple.Entry
  7.  

This requires memory allocation as well as run time overhead of  copying of Tuple.Entry to Entry.

The other option is to use Tuple.Entry through out the function (which is not really a nice solution).

Another option is use EntryPtr := @Tuple.Entry; , but this requires to use EntryPtr^ in the function. I believe this is the best option.

Wondering if there is any other solution? 

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: How to avoid copying
« Reply #1 on: January 17, 2025, 07:19:31 pm »
Hi
I'd stay with the pointer-solution and then at the top of the unit add this:
Code: Pascal  [Select][+][-]
  1. {$modeswitch autoderef}
That way you don't have to write a lot of 'hats' ^
HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10791
  • Debugger - SynEdit - and more
    • wiki
Re: How to avoid copying
« Reply #2 on: January 17, 2025, 07:25:38 pm »
Code: Pascal  [Select][+][-]
  1.     function F(constref Tuple: TTuple): Boolean;
  2.     var
  3.       Entry: TEntry absolute Tuple.Entry;
  4.     begin
  5.  

Amir61

  • New Member
  • *
  • Posts: 28
    • http://Amir.Aavani.net
Re: How to avoid copying
« Reply #3 on: January 17, 2025, 07:55:16 pm »
Thanks!

PascalDragon

  • Hero Member
  • *****
  • Posts: 5851
  • Compiler Developer
Re: How to avoid copying
« Reply #4 on: January 18, 2025, 05:02:08 pm »
Code: Pascal  [Select][+][-]
  1. function F(constref Tuple: TTuple): Boolean;
  2.  

Not related to your question, but I'd suggest you to use const instead of constref. While for this specific example it does not make a difference there are cases where you don't want to enforce a reference, because the compiler could instead pass it in a register or two (e.g. if you have a record containing two 32-bit values). Use constref only if you really need a constant reference.

 

TinyPortal © 2005-2018