[fpc-pascal] Array clearing
Ryan Joseph
ryan at thealchemistguild.com
Tue Apr 4 16:27:10 CEST 2017
> On Apr 4, 2017, at 10:07 PM, Jürgen Hestermann <juergen.hestermann at gmx.de> wrote:
>
> Am 2017-04-04 um 15:40 schrieb Ryan Joseph:
> > I’m glad I asked because of arrays of pointers is bad news for performance.
>
> I don't think that you will notice a performance issue with dynamic arrays (though it highly depends on the sizes and levels you use...)
As I understand it accessing continuous blocks of memory in tight loops increases the chance that it will be in cache L1/L2 cache. It’s probably trivial for my use but I’m making some game code which is highly performance sensitive and I’m trying to learn some best practices when writing data types. I watched a couple presentations from a compiler engineer from Google (works on clang and LLVM) that explained how this worked and it’s something worth learning and paying attention to imo.
>
> > Does SetLength on a single level dynamic array not even allocate a continuous block of memory?
>
> Yes, it does (as explained in all the other mails).
> A (dynamic) array of integer will be allocated as a single block by SetLength.
> So if you only have one level of a dynamic array as in
>
> var MyArray : array of integer;
>
> then SetLength(MyArray,1000) will allocate a single block of 1000 integers.
> But with
>
> var MyArray : array of array of integer;
>
> SetLength(MyArray,1000);
>
> will allocate a single block of 1000 pointers (to an array of integer each).
> Then, SetLength(MyArray[0],1000) will allocate one (!) block of 1000 integers.
> SetLength(MyArray[1],1000) will allocate another block of 1000 integers and so on….
I was allocating using SetLength(MyArray, 3, 3, 3) for a 3x3x3 matrix for example. Maybe it depends on the memory manager and the state of heap but if you called early in the programs execution it should be allocate all that memory in one block I would think.
Another bottle neck of not using arrays of pointers is you need to perform some math if you want to know which element 2,2,2 is. I’m probably getting in over my head now. :)
Regards,
Ryan Joseph
More information about the fpc-pascal
mailing list