[Lazarus] Questions about SetLength
payl
payl at wp.pl
Fri Sep 12 19:21:45 CEST 2014
> 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?).
More information about the Lazarus
mailing list