Generics.Collections – TList / TListHelper grows list size too aggressively

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.

15 thoughts on “Generics.Collections – TList / TListHelper grows list size too aggressively

  1. 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.

  2. 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

Leave a Reply