[fpc-devel] Question on constref
Adriaan van Os
adriaan at adriaan.biz
Thu Feb 2 02:54:46 CET 2023
Hairy Pixels via fpc-devel wrote:
>
>> On Feb 2, 2023, at 4:38 AM, Sven Barth <pascaldragon at googlemail.com> wrote:
>>
>> Which types are passed by-value or by-reference when using const is determined by the size of the record and the types of the fields based on whatever the corresponding ABI defines (e.g. the x86_64 Sys V ABI is rather explicit about some field combinations). The compiler will however not switch between passing a specific type once by-value and another time by-reference.
>
> So if the compiler is making the choice using const which is more efficient then we should be using const always I would think? What problem does constref solve then?
>
> You go on to show this:
>
> === doc begin ===
>
> Contrary to Delphi, no assumptions should be made about how const parameters are passed to the underlying routine. In particular, the assumption that parameters with large size are passed by reference is not correct. For this the constref parameter type should be used, which is available as of version 2.5.1 of the compiler.
>
> === doc end ===
>
> Is there anytime a large record should be passed by value if the function is not altering the it? This is why I started using constref, i.e. "In particular, the assumption that parameters with large size are passed by reference is not correct” which makes me think if I have a record of say 64k if I don’t use constref it could be passed by value to a function which is called in a tight loop.
Yes, to me this notion in the docs lacks all logic. How could copying large parameters ever be
efficient ? It invites developers to use constref for a purpose it wasn't meant for. For a debate,
see FPC Issue 17442. Jonas Maebe writes there
"We are adding support for a "constref" parameter to the compiler that will
allow you to specify that a parameter is const and must be passed by
reference, but I would strongly suggest to use it only for compatibility
with external APIs (that e.g. use "const struct *" parameters) and very
sparingly for optimization, because in many cases you may well slow down
your code rather than speeding it up."
but also
"Maybe another developer believes that for non-cdecl, we could modify the
behaviour of const so that very large records are passed by reference,
which is why I leave this bug open."
If my memory is correct, that this was changed for macpas mode (passing very large records by
reference) but can't find it back in the bug database.
Regards,
Adraan van Os
More information about the fpc-devel
mailing list