<div dir="auto"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Amir via fpc-pascal <<a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>> schrieb am Sa., 30. Dez. 2023, 08:11:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Dec 29, 2023 9:50 PM, Adriaan van Os <<a href="mailto:adriaan@adriaan.biz" target="_blank" rel="noreferrer">adriaan@adriaan.biz</a>> wrote:<br type="attribution"><blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">Amir--- via fpc-pascal wrote:
<br>
> Hi all,
<br>

<br>
>  I have a List of record, where the record has a WideString field.
<br>
>   I have some code like the following:
<br>

<br>
> function check(constref v: TMyRecord; data: TListOfMyRecord): Boolean;
<br>
> var
<br>
>   r: TMyRecord;
<br>

<br>
> begin
<br>
>   Result := False;
<br>
>   for r in data do
<br>
>     if r.State = v.State then
<br>
>       Exit(True);
<br>
> end;
<br>

<br>
> I call this method a lot and the CPU profiling shows a lot of cpu time 
<br>
> spent on "fpc_copy_proc" (which I assume is doing the deep copy on 
<br>
> records) from "TCustomListEnumerator.GetCurrent".
<br>
> I considered other alternatives like using enumerators but they all need 
<br>
> a to return a record (and hence copying the widestring field).
<br>
> I can think of two solutions to get rid of the wasting(!) so much time 
<br>
> on "fpc_copy_proc":
<br>
> 1) Changing the TMyRecord to TMyClass. But then I need to Create and 
<br>
> Free a "lot" of objects.
<br>
> 2) Update TListOfMyRecord to TListOfPointerToMyRecord. This requires a 
<br>
> "lot" of memory allocation/fragmentation.
<br>

<br>
> Is there a better solution?
<br>

<br>
Pass the data parameter by reference.</p></blockquote></div></div></div><div dir="auto">This means I need to have a ListOfMyRecord and a ListOfConstRefMyRecord, right? </div></div></blockquote></div></div><div dir="auto"><br></div><div dir="auto">No, that's not a thing.</div><div dir="auto"><br></div><div dir="auto">You simply need to declare your "data" parameter as "constref" or "const" as well, just like your "v" parameter. </div><div dir="auto"><br></div><div dir="auto">Note: prefer the use of "const" instead of "constref" unless you really need a reference, because the compiler will then pick the optimal way to pass the value (e.g. small ones will be passed by register instead of as an address).</div><div dir="auto"><br></div><div dir="auto">Regards, </div><div dir="auto">Sven </div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote></div></div></div>