-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce reverseInPlace
in Variable/Fixed size Array
#2626
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2626 +/- ##
=======================================
Coverage 78.60% 78.61%
=======================================
Files 336 337 +1
Lines 77869 77901 +32
=======================================
+ Hits 61207 61239 +32
Misses 14379 14379
Partials 2283 2283
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
@@ -10395,6 +10395,67 @@ func TestInterpretArrayFirstIndexDoesNotExist(t *testing.T) { | |||
) | |||
} | |||
|
|||
func TestInterpretArrayReverse(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please also add test cases where the elements are structs and resources?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also no need for an additional "Transfer" because Remove
and Insert
already do a transfer underneath.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
reverseInplace
function for in-place reversal of a Variable/Fixed size Array value.master
branchFiles changed
in the Github PR explorer