Introduce reverseInPlace in Variable/Fixed size Array#2626
Introduce reverseInPlace in Variable/Fixed size Array#2626darkdrag00nv2 wants to merge 7 commits intoonflow:masterfrom
reverseInPlace in Variable/Fixed size Array#2626Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
| ) | ||
| } | ||
|
|
||
| func TestInterpretArrayReverse(t *testing.T) { |
There was a problem hiding this comment.
Could you please also add test cases where the elements are structs and resources?
There was a problem hiding this comment.
Hmm, thanks for pointing it out.
When I added the test case for struct, it doesn't work.
pub struct TestStruct {
pub var test: Int
init(_ t: Int) {
self.test = t
}
}
fun reverseStructArray(): [Int] {
let sa = [TestStruct(1), TestStruct(2), TestStruct(3)]
sa.reverse()
let res: [Int] = [];
for s in sa {
res.append(s.test)
}
return res
}I get error: member test is used before it has been initialized. So it seems like using Get and Set messes up the struct members.
There was a problem hiding this comment.
By adding transfer call, I was able to get it to work for struct.
leftValue := v.Get(interpreter, locationRange, leftIndex)
rightValue := v.Get(interpreter, locationRange, rightIndex)
leftValue = leftValue.Transfer(
interpreter,
locationRange,
v.array.Address(),
true,
nil,
)
rightValue = rightValue.Transfer(
interpreter,
locationRange,
v.array.Address(),
true,
nil,
)
v.Set(interpreter, locationRange, leftIndex, rightValue)
v.Set(interpreter, locationRange, rightIndex, leftValue)It doesn't work for resource because after the first Set call, we end up with two resources at the leftIndex which returns the error // error: two parents are captured for the slab.
I'll look further into how we swap resources atomically. Another option might be to just not support reverse for resource typed arrays. Most of the array functions do that already.
There was a problem hiding this comment.
So the problem is, structs and resources behave differently when "Transferred". Structs are copied, whereas references are just 'moved'. Here what you would want to do is, instead of v.Get, do a v.Remove, which will "remove" the value from the array. Then instead of setting (i.e: v.Set), do a v.Insert, because by removing earlier, we literally remove the value from the array, hence the values are shifted by one index.
Now one thing to note is that, because the remaining values are shifted by one index by removing, if you try to remove the first element (leftValue) first, and then try to remove the last element (rightElement), it will mess-up the indexing for the second/right value removal. (could get an array-out-of-bound error / or could end up removing the wrong value) because after the first removal, the size of the array is (n-1).
So, you'll have to swap the removals, to first remove from the rear-end (right index) first, and then remove from the front (the left index).
i.e:
// Remove the right index (from the rear end) first, because removing the left index (from the front of the array) first could mess up the indexes of the rest of the elements.
rightValue := v.Remove(interpreter, locationRange, rightIndex)
leftValue := v.Remove(interpreter, locationRange, leftIndex)
// Similarly, insert the left index first, before the right index.
v.Insert(interpreter, locationRange, leftIndex, rightValue)
v.Insert(interpreter, locationRange, rightIndex, leftValue)There was a problem hiding this comment.
Also no need for an additional "Transfer" because Remove and Insert already do a transfer underneath.
There was a problem hiding this comment.
This feels too expensive to me; transfers here are not necessary, Why move stuff to stack and then move again to strange with totally new storageID and slab tree?
We are 100% sure that storage address will not change in this operation. Why not add swap to atree.Array ? @fxamacker can confirm, but I think this way, all array is read and written again.
There was a problem hiding this comment.
I thought so as well and asked Bastian about adding support for reverse in atree on #2605 (comment). Didn't ask about swap but that will work nicely as well.
We can also think about adding it using Remove and Insert and later optimizing it using support in atree.
There was a problem hiding this comment.
Alternatively, as it has been briefly discussed in issue #2605, maybe we could start with the function that returns a new array with entries reversed, which should be easy to implement. And then later add this as the "optimized" version which does the same in-place. So the functionality is there, if someone needs it.
There was a problem hiding this comment.
We are 100% sure that storage address will not change in this operation. Why not add swap to atree.Array ?
Good point 👍 . I opened onflow/atree#326 to add Array.Swap.
Co-authored-by: Bastian Müller <bastian@turbolent.com>
Co-authored-by: Bastian Müller <bastian@turbolent.com>
Co-authored-by: Bastian Müller <bastian@turbolent.com>
reverse in Variable/Fixed size ArrayreverseInPlace in Variable/Fixed size Array
|
Have decided to update the function name to |
Work towards #2605
Description
Introduce
reverseInplacefunction for in-place reversal of a Variable/Fixed size Array value.masterbranchFiles changedin the Github PR explorer