lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


An array that doesn’t automatically but intentionally resize is easily extended

into one, with the automatic/current behaviour, and it needs very little additional

code.

But given the current version, you can’t do it the other way around, you’re

forced to waste the resources while there could be a version where it’s optional

and you, as the developer, should be the one knowing which one better fits your

current needs.

Also besides the waste of memory it also can trigger a series of reallocs while you

initially fill your array.

 

And yes, this is one of the very few parts where I think the language could

and should be extended, as it’s such a fundamental part and in (of course

my personal experience) way more than 50% of the situations where an

array is the data struct of choice, it’s actual (max)size is/can be known upfront.

 

 

Von: lua-l-bounces@lists.lua.org <lua-l-bounces@lists.lua.org> Im Auftrag von Gé Weijers
Gesendet: Samstag, 20. Oktober 2018 02:18
An: lua-l@lists.lua.org
Betreff: Re: Array subtype is conceptually simplest

 

 

On Thu, Oct 18, 2018 at 1:16 AM <michaelflad@clawdesign.com> wrote:

In addition, (3) would also allow to only allocate the actual required
amount of memory for the array instead of, worst case, use almost
twice of that.

 

Doubling the size keeps the amount of work required to append an element to the array part of a table from becoming quadratic. Appending an element takes O(1) works amortized