Reputation: 7425
This is a theoretical question intended to generate a look-up list of pros and cons of different data storage ways in Delphi.
Let's say we have a record:
type
TMyRecord = record
X,Y,Z: Single;
IsValid: Boolean;
end;
Basic options of storing an array of such records are:
array of TMyRecord;
TList
with getter/setterTList<TMyRecord>;
I'm specifically interested about #1 vs #3 comparison, how much is the difference between those, especially performance-wise.
Upvotes: 7
Views: 857
Reputation: 3025
TList<T>
pros:
TList<T>
cons:
TList<T>[i]
actually returns a copy of its element. So you can't write something like TList<TMyRec>[idx].SomeField := foo
. Instead, you have to use temporary variable. Array obviously allows such expression.
David Heffernan mentioned TList<T>.List
which eliminates this drawback; however, it appeared only in XE3For myself I wrote TRecordList<T>
class which operates items as pointers (like classic TList does).
Upvotes: 12
Reputation: 613252
tl;dr
TList<T>
. TList<T>
offers many conveniences to the programmer that dynamic arrays do not afford. The implementation of TList<T>
is that the items are stored in a dynamic array. So there are essentially no performance differences between TList<T>
and TArray<T>
.
Of course, the dynamic array gives you direct access to the elements without making copies. You can do the same using the List
property of TList<T>
if that matters. In fact that property means that in performance terms at least TList<T>
is a match for a plain dynamic array.
Beyond performance, using TList<T>
gives you all sorts of convenience over a raw dynamic array. I don't need to enumerate these conveniences. You can see them clearly from the list of public methods and properties.
Since performance is important to you, you should tackle the real performance issue with the code in the question. Namely the fact that you packed your record. That will lead to the majority of instances of the record being mis-aligned. And that has dire performance implications. If you care about performance, you will align structures.
Your middle option, a list of pointers is not really worth considering. It will likely scatter memory all over the address space and be unfriendly to the cache.
Upvotes: 7