Changing array key text

Mark Waddingham mark at livecode.com
Tue Jun 25 04:25:15 EDT 2019


On 2019-06-25 04:18, Brian Milby via use-livecode wrote:
> My guess is that it has to do with the copy on write property of
> arrays.  If you used your method it may force an actual duplication.
>  The existing method just passes a pointer effectively.  I could be
> totally wrong though.
> 
> On Jun 24, 2019, 10:11 PM -0400, Mark Wieder via use-livecode
> <use-livecode at lists.runrev.com>, wrote:
>> > put xArray[item 1 of pPath] into tSubArray
>> > delete variable xArray[item 1 of pPath]
>> > put tSubArray into xArray[pKey]
>> 
>> Interesting. Wouldn't it be faster to do
>> 
>> put xArray[item 1 of pPath] into xArray[pKey]
>> delete variable xArray[item 1 of pPath]
>> 
>> i.e., why the extra copy?

In terms of copies there are none happening in either case - only xArray 
is being mutated - and referencing counting means that the 'puts' are 
all free.

The first will be 'on average' marginally more efficient but that's 
because of the number of keys...

In method (1) the number of keys goes from N -> N - 1 -> N

In method (2) the number of keys goes from N -> N + 1 -> N

Its much more likely that adding a key will cause the engine to have to 
extend the internal vector of key-value pairs it uses to store the array 
mapping then removing a key will cause it to shrink it - also expansion 
is 'on average' slower than shrinking.

Warmest Regards,

Mark.

-- 
Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
LiveCode: Everyone can create apps




More information about the use-livecode mailing list