[fpc-devel] Question on constref

Hairy Pixels genericptr at gmail.com
Thu Feb 2 02:09:04 CET 2023



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

Regards,
Ryan Joseph



More information about the fpc-devel mailing list