Re: [fpc-devel] Question on constref

2023-02-02 Thread Ondrej Pokorny via fpc-devel
On 02.02.2023 14:22, Sven Barth via fpc-devel wrote: Only because you can't see a reason for it doesn't mean that it doesn't exist. That is exactly why I asked :) Ondrej ___ fpc-devel maillist - fpc-devel@lists.freepascal.org

Re: [fpc-devel] Question on constref

2023-02-02 Thread Sven Barth via fpc-devel
Ondrej Pokorny via fpc-devel schrieb am Do., 2. Feb. 2023, 12:09: > Delphi cannot do it. FPC rocks! > > The only disadvantage is that you get a FreeAndNil copy for every type > you pass into the parameter (?) > > Do we want to change it in SysUtils? :) > As you might have noticed you need to

Re: [fpc-devel] Question on constref

2023-02-02 Thread Sven Barth via fpc-devel
Ondrej Pokorny via fpc-devel schrieb am Do., 2. Feb. 2023, 10:06: > On 02.02.2023 07:42, Sven Barth via fpc-devel wrote: > > 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

Re: [fpc-devel] Question on constref

2023-02-02 Thread Ondrej Pokorny via fpc-devel
On 02.02.2023 12:19, Marco van de Voort via fpc-devel wrote: On 2-2-2023 12:00, Ondrej Pokorny via fpc-devel wrote: The only disadvantage is that you get a FreeAndNil copy for every type you pass into the parameter (?) Are they actually made global ? Will two freeandnil in different units use

Re: [fpc-devel] Question on constref

2023-02-02 Thread Ondrej Pokorny via fpc-devel
On 02.02.2023 12:25, Mattias Gaertner via fpc-devel wrote: On Thu, 2 Feb 2023 12:00:31 +0100 Ondrej Pokorny via fpc-devel wrote: [...] Delphi cannot do it. FPC rocks! +1 The only disadvantage is that you get a FreeAndNil copy for every type you pass into the parameter (?) Do we want to

Re: [fpc-devel] Question on constref

2023-02-02 Thread Mattias Gaertner via fpc-devel
On Thu, 2 Feb 2023 12:19:46 +0100 Marco van de Voort via fpc-devel wrote: > On 2-2-2023 12:00, Ondrej Pokorny via fpc-devel wrote: > > > > > > The only disadvantage is that you get a FreeAndNil copy for every > > type you pass into the parameter (?) > > Are they actually made global ? yes >

Re: [fpc-devel] Question on constref

2023-02-02 Thread Marco van de Voort via fpc-devel
On 2-2-2023 12:00, Ondrej Pokorny via fpc-devel wrote: The only disadvantage is that you get a FreeAndNil copy for every type you pass into the parameter (?) Are they actually made global ? Will two freeandnil in different units use the same? But that is just curiousity, IMHO this

Re: [fpc-devel] Question on constref

2023-02-02 Thread Mattias Gaertner via fpc-devel
On Thu, 2 Feb 2023 12:00:31 +0100 Ondrej Pokorny via fpc-devel wrote: >[...] > Delphi cannot do it. FPC rocks! +1 > The only disadvantage is that you get a FreeAndNil copy for every > type you pass into the parameter (?) > > Do we want to change it in SysUtils? :) I got a feeling this solves

Re: [fpc-devel] Question on constref

2023-02-02 Thread Ondrej Pokorny via fpc-devel
On 02.02.2023 11:00, Michael Van Canneyt via fpc-devel wrote: In userspace, the best seems Function FreeAndNil(Obj : T) : T; begin   Obj.Free;   Result:=Nil; end; With automatic type inference for generics, this allows you to do A:=FreeAndNil(A); Michael, bringing your idea to the next

Re: [fpc-devel] Question on constref

2023-02-02 Thread Sven Barth via fpc-devel
Adriaan van Os via fpc-devel schrieb am Do., 2. Feb. 2023, 09:57: > Sven Barth wrote: > > Adriaan van Os via fpc-devel > > schrieb am Do., 2. Feb. 2023, > > 02:47: > > > > Sven Barth via fpc-devel wrote: > > > > > There is no full documentation

Re: [fpc-devel] Question on constref

2023-02-02 Thread Michael Van Canneyt via fpc-devel
On Thu, 2 Feb 2023, Ondrej Pokorny via fpc-devel wrote: On 02.02.2023 10:22, Michael Van Canneyt via fpc-devel wrote: On Thu, 2 Feb 2023, Ondrej Pokorny via fpc-devel wrote: On 02.02.2023 10:15, Michael Van Canneyt via fpc-devel wrote: On Thu, 2 Feb 2023, Ondrej Pokorny via fpc-devel

Re: [fpc-devel] Question on constref

2023-02-02 Thread Ondrej Pokorny via fpc-devel
On 02.02.2023 10:22, Michael Van Canneyt via fpc-devel wrote: On Thu, 2 Feb 2023, Ondrej Pokorny via fpc-devel wrote: On 02.02.2023 10:15, Michael Van Canneyt via fpc-devel wrote: On Thu, 2 Feb 2023, Ondrej Pokorny via fpc-devel wrote: I myself cannot think of any real use case of constref

Re: [fpc-devel] Question on constref

2023-02-02 Thread Michael Van Canneyt via fpc-devel
On Thu, 2 Feb 2023, Ondrej Pokorny via fpc-devel wrote: On 02.02.2023 10:15, Michael Van Canneyt via fpc-devel wrote: On Thu, 2 Feb 2023, Ondrej Pokorny via fpc-devel wrote: I myself cannot think of any real use case of constref other than hacks like the FreeAndNil in recent Delphi

Re: [fpc-devel] Question on constref

2023-02-02 Thread Michael Van Canneyt via fpc-devel
On Thu, 2 Feb 2023, Hairy Pixels via fpc-devel wrote: On Feb 2, 2023, at 3:57 PM, Adriaan van Os via fpc-devel wrote: - under what circumstances (and in what compiler mode) does FPC pass large (say 1 MB or 1 GB) "const" parameters by value (which is extremely inefficient) ? - for what

Re: [fpc-devel] Question on constref

2023-02-02 Thread Ondrej Pokorny via fpc-devel
On 02.02.2023 10:15, Michael Van Canneyt via fpc-devel wrote: On Thu, 2 Feb 2023, Ondrej Pokorny via fpc-devel wrote: I myself cannot think of any real use case of constref other than hacks like the FreeAndNil in recent Delphi versions: procedure FreeAndNil(const [ref] Obj: TObject); that

Re: [fpc-devel] Question on constref

2023-02-02 Thread Michael Van Canneyt via fpc-devel
On Thu, 2 Feb 2023, Ondrej Pokorny via fpc-devel wrote: On 02.02.2023 07:42, Sven Barth via fpc-devel wrote: 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

Re: [fpc-devel] Question on constref

2023-02-02 Thread Hairy Pixels via fpc-devel
> On Feb 2, 2023, at 3:57 PM, Adriaan van Os via fpc-devel > wrote: > > - under what circumstances (and in what compiler mode) does FPC pass large > (say 1 MB or 1 GB) "const" parameters by value (which is extremely > inefficient) ? > - for what reasons ? > > As long as these questions are

Re: [fpc-devel] Question on constref

2023-02-02 Thread Ondrej Pokorny via fpc-devel
On 02.02.2023 07:42, Sven Barth via fpc-devel wrote: 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. Maybe I am missing

Re: [fpc-devel] Question on constref

2023-02-02 Thread Adriaan van Os via fpc-devel
Sven Barth wrote: Adriaan van Os via fpc-devel > schrieb am Do., 2. Feb. 2023, 02:47: Sven Barth via fpc-devel wrote: > There is no full documentation for that parameter modifier (someone > might want to file a bug report for that), but the

Re: [fpc-devel] Question on constref

2023-02-02 Thread Michael Van Canneyt via fpc-devel
On Thu, 2 Feb 2023, Sven Barth via fpc-devel wrote: Am 02.02.2023 um 02:09 schrieb Hairy Pixels: On Feb 2, 2023, at 4:38 AM, Sven Barth 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

Re: [fpc-devel] Question on constref

2023-02-02 Thread Steve Litt via fpc-devel
Bart via fpc-devel said on Wed, 1 Feb 2023 11:30:37 +0100 >Hi, > >The function is not supposed to alter the contents of this >datastructure. So, I need to pass this structure by reference. >(I've spent hours and hours looking at unexpected results in my >program because I passed the

Re: [fpc-devel] Question on constref

2023-02-01 Thread Adriaan van Os via fpc-devel
Sven Barth via fpc-devel wrote: 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. I doubt

Re: [fpc-devel] Question on constref

2023-02-01 Thread Sven Barth via fpc-devel
Adriaan van Os via fpc-devel schrieb am Do., 2. Feb. 2023, 02:47: > Sven Barth via fpc-devel wrote: > > > There is no full documentation for that parameter modifier (someone > > might want to file a bug report for that), but the documentation for > > “const” ( > > >

Re: [fpc-devel] Question on constref

2023-02-01 Thread Sven Barth via fpc-devel
Am 02.02.2023 um 02:09 schrieb Hairy Pixels: On Feb 2, 2023, at 4:38 AM, Sven Barth 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

Re: [fpc-devel] Question on constref

2023-02-01 Thread Hairy Pixels via fpc-devel
> On Feb 2, 2023, at 8:41 AM, Adriaan van Os via fpc-devel > wrote: > > On debate, see FPC issue 17442. https://gitlab.com/freepascal.org/fpc/source/-/issues/17442 Yes this is what I remember learning. What I think should be documented is what you should do for records that over a certain

Re: [fpc-devel] Question on constref

2023-02-01 Thread Adriaan van Os via fpc-devel
Hairy Pixels via fpc-devel wrote: On Feb 2, 2023, at 4:38 AM, Sven Barth 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

Re: [fpc-devel] Question on constref

2023-02-01 Thread Adriaan van Os via fpc-devel
Sven Barth via fpc-devel wrote: There is no full documentation for that parameter modifier (someone might want to file a bug report for that), but the documentation for “const” ( https://www.freepascal.org/docs-html/current/ref/refsu67.html#x183-20700014.4.4 ) contains this: === doc begin

Re: [fpc-devel] Question on constref

2023-02-01 Thread Hairy Pixels via fpc-devel
> On Feb 2, 2023, at 4:38 AM, Sven Barth 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

Re: [fpc-devel] Question on constref

2023-02-01 Thread Michael Van Canneyt via fpc-devel
On Wed, 1 Feb 2023, Sven Barth via fpc-devel wrote: Am 01.02.2023 um 11:30 schrieb Bart via fpc-devel: I thought that constref would be OK for that (the word constref suggests to me tah the paramter will be treated (by me) to be a constant, and that it shall be passed by reference in all

Re: [fpc-devel] Question on constref

2023-02-01 Thread Sven Barth via fpc-devel
Am 01.02.2023 um 11:30 schrieb Bart via fpc-devel: I thought that constref would be OK for that (the word constref suggests to me tah the paramter will be treated (by me) to be a constant, and that it shall be passed by reference in all cases, whereas with a const parameter the compiler decides

Re: [fpc-devel] Question on constref

2023-02-01 Thread Sven Barth via fpc-devel
Am 01.02.2023 um 15:40 schrieb Hairy Pixels via fpc-devel: On Feb 1, 2023, at 8:27 PM, Michael Van Canneyt via fpc-devel wrote: That's exactly what Adriaan is saying. With const the compiler can choose. With constref, you force it not to copy. But this is not so efficient for small

Re: [fpc-devel] Question on constref

2023-02-01 Thread Tomas Hajny via fpc-devel
On 2023-02-01 15:40, Hairy Pixels via fpc-devel wrote: On Feb 1, 2023, at 8:27 PM, Michael Van Canneyt via fpc-devel wrote: That's exactly what Adriaan is saying. With const the compiler can choose. With constref, you force it not to copy. But this is not so efficient for small parameter

Re: [fpc-devel] Question on constref

2023-02-01 Thread Michael Van Canneyt via fpc-devel
On Wed, 1 Feb 2023, Hairy Pixels via fpc-devel wrote: On Feb 1, 2023, at 8:27 PM, Michael Van Canneyt via fpc-devel wrote: That's exactly what Adriaan is saying. With const the compiler can choose. With constref, you force it not to copy. But this is not so efficient for small parameter

Re: [fpc-devel] Question on constref

2023-02-01 Thread Hairy Pixels via fpc-devel
> On Feb 1, 2023, at 8:27 PM, Michael Van Canneyt via fpc-devel > wrote: > > That's exactly what Adriaan is saying. With const the compiler can choose. > With constref, you force it not to copy. But this is not so efficient for > small parameter sizes. So const will always pass records that

Re: [fpc-devel] Question on constref

2023-02-01 Thread Michael Van Canneyt via fpc-devel
On Wed, 1 Feb 2023, Hairy Pixels via fpc-devel wrote: On Feb 1, 2023, at 8:16 PM, Adriaan van Os via fpc-devel wrote: 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

Re: [fpc-devel] Question on constref

2023-02-01 Thread Hairy Pixels via fpc-devel
> On Feb 1, 2023, at 8:16 PM, Adriaan van Os via fpc-devel > wrote: > > 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

Re: [fpc-devel] Question on constref

2023-02-01 Thread Adriaan van Os via fpc-devel
Hairy Pixels via fpc-devel wrote: On Feb 1, 2023, at 5:56 PM, Adriaan van Os via fpc-devel 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.

Re: [fpc-devel] Question on constref

2023-02-01 Thread Hairy Pixels via fpc-devel
> On Feb 1, 2023, at 5:56 PM, Adriaan van Os via fpc-devel > 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

Re: [fpc-devel] Question on constref

2023-02-01 Thread Bart via fpc-devel
On Wed, Feb 1, 2023 at 11:56 AM Adriaan van Os via fpc-devel wrote: > That is wrong. It should read > > "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

Re: [fpc-devel] Question on constref

2023-02-01 Thread Adriaan van Os via fpc-devel
Bart via fpc-devel wrote: "Note that in general, it should only be used for interfacing with external code or when writing assembler routines." That is wrong. It should read "A const parameter is be passed by reference or (for small-sized parameters) by value, whatever is most efficient. A