[fpc-devel] Question on constref

Adriaan van Os adriaan at adriaan.biz
Wed Feb 1 14:16:41 CET 2023


Hairy Pixels via fpc-devel wrote:
> 
>> On Feb 1, 2023, at 5:56 PM, Adriaan van Os via fpc-devel <fpc-devel at lists.freepascal.org> wrote:
>>
>> "A const parameter is be passed by reference or (for small-sized parameters) by value, whatever is most efficient. A constref parameter is guaranteed to be passed by reference in all cases. The latter is therefore typically used for interfacing with external code or when writing assembler routines."
> 
> I’ve been confused by constref versus const. I use constref when passing large records as parameters so I know it will not make needless copies but why doesn’t const function this way too? If the parameter is constant why would it ever make sense for the compiler to copy the entire record when it knows the function can’t change it?

Because, if e.g. the byte-size of a parameter is such that it fits into a CPU register, then 
passing the parameter itself is more efficient than passing a reference to it. For large byte-size 
parameters, const and constref function the same. The difference is with small byte-size parameters.

Regards,

Adriaan van Os


More information about the fpc-devel mailing list