Recent

Author Topic: fpFD_ISSET const parameter  (Read 2757 times)

jorgicor

  • New Member
  • *
  • Posts: 11
fpFD_ISSET const parameter
« on: May 27, 2015, 11:40:03 pm »
Hello, reading the RTL unit BaseUnix, I have seen that the function fpFD_ISSET takes its second parameter as a const array:

function fpFD_ISSET(fdno: cint;  const nset: TFDSet): cint;

TFDSet is a fixed array of 256 bytes. I'm a beginner with free pascal, so at first I guessed that passing a const parameter was like passing a VAR parameter (that is, by address) but the compiler wouldn't allow to change it. But the language reference documentation says:

Quote
Remark: 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.

If this is so, that function should be changed to take a pointer to the array or VAR parameter, because if not, a copy of the array is made every time the function is called.

My question is: is this function correct and the array is passed by reference (not copying) when the parameter is a const array? Or on the contrary the array is copied (in which case I think the function should be changed because as it is will be very inefficient)?

Thank you very much,

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11445
  • FPC developer.
Re: fpFD_ISSET const parameter
« Reply #1 on: May 28, 2015, 09:00:32 am »
The remark is about it not being a guarantee that it is by ref. Not that it is never by ref. IOW if the compiler it thinks it is more worthwhile to pass a small sized struct in register ( by value) that will be done, but in all other cases it still will be by ref.

But in this case it will probably be passed by ref

jorgicor

  • New Member
  • *
  • Posts: 11
Re: fpFD_ISSET const parameter
« Reply #2 on: May 28, 2015, 11:27:11 am »
Thank you markov.

But the remark says: "the assumption that parameters with large size are passed by reference is not correct".

Here TFDSet is an array of 256 bytes. I need a strong guarantee that the parameter is passed by reference, if not my code can pass from being O(n) to be O(n^2). I cannot rely on what the compiler decides. Note that normally this function is used like this:

Code: [Select]
for all sockets
      if fpFD_ISSET(socket, fdset) then
          etc
      end
end

If the compiler decides not to pass fdset by ref, the code suddenly will become very slow.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11445
  • FPC developer.
Re: fpFD_ISSET const parameter
« Reply #3 on: May 28, 2015, 11:32:36 am »
I think you worry too much about nothing. That exception was made to let CONST follow the ABI closely, and ABIs don't care about 256 byte structures and Pascal by value.

It is mostly about by reference or in registers, not by reference or pascal by value by copying it on the local stack.

jorgicor

  • New Member
  • *
  • Posts: 11
Re: fpFD_ISSET const parameter
« Reply #4 on: May 29, 2015, 08:59:06 am »
Ok, I understand what you mean.

Thank you markov.

 

TinyPortal © 2005-2018