Changing array key text

Mark Wieder ahsoftware at sonic.net
Tue Jun 25 18:34:32 EDT 2019


On 6/25/19 1:25 AM, Mark Waddingham via use-livecode wrote:
> 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.

Thanks. That helps. Mostly.
If no copies are made then I'm still a bit weirded out by

put xArray[item 1 of pPath] into tSubArray
delete variable xArray[item 1 of pPath]
put xArray[item 1 of pPath] into limbo
put tSubArray into xArray[pKey]

where limbo is empty and tSubArray still retains the contents.
The quantum effect jokes write themselves.

Is it the case the tSubArray retains a pointer to the contents but the 
entry in the hash is otherwise invalidated but not garbage collected 
until the reference count goes to zero?

-- 
  Mark Wieder
  ahsoftware at gmail.com




More information about the use-livecode mailing list