Generics.Collections – TList / TListHelper grows list size too aggressively
The current code will double the size of the list, regardless of it’s current size. This is fine for smaller sizes – but very wasteful when growing a large list.
Due to the design having ListHelper as a record which have an instance inside the class – it is not possible to simply make ListHelper.InternalGrow method virtual.
I am not sure what will be the better way to do this. Calling a plugin method (anonymous function) might be too costly and slow down the list at smaller sizes.
Perhaps have a growth strategy setting: Exponential (default), Percentage, Linear – and a growth factor field – which would be the percentage or number of items, depending on the strategy?
Anyways – just doubling a 128K, 256K, 512K, 1M list is not a good strategy – at least not if you are not able to override it.
https://quality.embarcadero.com/browse/RSP-17767
Without looking into the sources, can’t you just add some extra items yourself and delete it to prevent triggering auto growth?
And isn’t the out of memory detection so 32bit?
31 actually
Attila Kovacs You can use SetCapacity – but it will still double whatever number that is, if you happen to pass that limit.
Lars Fosdal Ouch. Voted.
People complaining about TList instance size in 3, 2…
Voted, although the .NET framework does it in a very similar manner: stackoverflow.com – c# – List and ArrayList default capacity – Stack Overflow
Java has 1.5 growth factor.
I never had need to tweak growth factors anywhere, but 2x seems a bit excessive.
When it comes to micro-optimizations, one class cannot fit all purposes. You need to have different classes for different needs. And here we bump into major issue with Delphi relying on classes rather than interfaces and you cannot just plug in another implementation depending on your use case.
Or the whole TList would have to be redesigned into base lite container that can serve as ancestor for more complex implementations.
Dalija Prasnikar If we just had some library that provides interfaces for collection types… 😉
Stefan Glienke Well, if only people would not want absolutely everything out of the box and one size fits all 😉
FWIW: https://plus.google.com/u/0/+StefanGlienke/posts/TLge4cMPrG5
IIRC the capacity is increased only when you’ve filled in all slots, and so, before adding a new one, you can check for that and increase the capacity by 25% or so, far from ideal tho
Search the source for InternalGrowCheck to see the implications.
Python grows its list like so:
0, 4, 8, 16, 25, 35, 46, 58, 72, 88….