[fpc-devel] Question on constref

Sven Barth pascaldragon at googlemail.com
Thu Feb 2 07:42:19 CET 2023


Am 02.02.2023 um 02:09 schrieb Hairy Pixels:
>
>> 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?

The case when you *need* a constant reference. Case in point: the 
passing of TGuid in IInterface.QueryInterface. Delphi code relies on it 
being a reference, but “const” does not guarantee that for all platforms.

>
> 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.

I doubt that records larger than say 4 register widths are ever passed 
by-value. But I definitely can't exclude it, cause I don't know every 
ABI of every platform by heart. So if you want details there then study 
the ABIs of the platforms you use.

Regards,
Sven


More information about the fpc-devel mailing list