Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-09

Questions about SetLength

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Agranov, Gennady

Hi,

Maybe it is not a correct list, but I am going to ask my question anyway :)

Is there a penalty for calling SetLength if array already has the length we need?

And if there is a penalty - is there a way to write "generic" function SmartSetLength(array,length) that will only call SetLength if necessary?

I hope other people find these questions interesting enough :)

Thanks,
Gena

Reply by email

payl
to Agranov, Gennady
Show quoted text 7 lines
Hi, Maybe it is not a correct list, but I am going to ask my question anyway :) Is there a penalty for calling SetLength if array already has the length we need?

Yes, there's always penalty for operations that you don't need. FPC will generate normal call, pass arguments, etc. I checked if SetLength with ansistring detects that requested size is same: It doesn't, it performs whole allocation and moving.

And if there is a penalty - is there a way to write "generic" function SmartSetLength(array,length) that will only call SetLength if necessary?

No. Compiler internally emits information about element size, how are they refcounted etc. as a pointer. Your best bet would be "hacking" FPC to emit such information to your procedure and then pass it to internal fpc setlength.
I would personally like it a lot if there was way to access "secret" information stored along with dynamic arrays/ansistrings, however it's not easy task. I think best bet would be storing secret information pointers along with refcount/length and then making functions that allow editing those data. Then we could have generic "dynamic array" type (I assume?).

Reply by email

Mattias Gaertner
to payl
Show quoted text 7 lines
[...] > Is there a penalty for calling SetLength if array already has the length > we need? Yes, there's always penalty for operations that you don't need. FPC will generate normal call, pass arguments, etc. I checked if SetLength with ansistring detects that requested size is same: It doesn't, it performs whole allocation and moving.

Not always.
Keep in mind that dynamic arrays have references and the array memory itself is a reference counted structure.

SetLength creates a unique copy of the array, that means an array with reference count 1. If it is already unique nothing is changed. For example:

var
  A,B: array of string;
begin
  SetLength(A,10); // this allocates, A points to unique array
  A[0]:='bla'; // the A array is still unique
  SetLength(A,10); // no reallocation
  B:=A; // reference count increased to 2, not unique anymore
  SetLength(A,10); // reallocation to create a second array, now A
                   // and B point to two unique arrays
  SetLength(A,10); // no reallocation
  SetLength(B,10); // no reallocation
end;

Mattias

Reply by email