Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

darkdrag00nv2
Copy link
Contributor

@darkdrag00nv2 darkdrag00nv2 commented Jul 1, 2023

Work towards #2605

Description

Introduce reverseInplace function for in-place reversal of a Variable/Fixed size Array value.


  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@codecov
Copy link

codecov bot commented Jul 1, 2023

Codecov Report

Merging #2626 (5056603) into master (5f84e78) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           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           
Flag Coverage Δ
unittests 78.61% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
runtime/interpreter/value.go 68.85% <100.00%> (+0.01%) ⬆️
runtime/sema/type.go 89.57% <100.00%> (+0.03%) ⬆️

... and 2 files with indirect coverage changes

Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

runtime/interpreter/value.go Outdated Show resolved Hide resolved
runtime/interpreter/value.go Outdated Show resolved Hide resolved
runtime/sema/type.go Outdated Show resolved Hide resolved
runtime/interpreter/value.go Outdated Show resolved Hide resolved
@@ -10395,6 +10395,67 @@ func TestInterpretArrayFirstIndexDoesNotExist(t *testing.T) {
)
}

func TestInterpretArrayReverse(t *testing.T) {
Copy link
Member

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?

Copy link
Contributor Author

@darkdrag00nv2 darkdrag00nv2 Jul 7, 2023

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.

Copy link
Contributor Author

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.

Copy link
Member

@SupunS SupunS Jul 10, 2023

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)

Copy link
Member

@SupunS SupunS Jul 10, 2023

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SupunS Makes sense, opened up #2654 for copy-based reversal.

Copy link
Member

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.

darkdrag00nv2 and others added 5 commits July 6, 2023 01:28
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>
@darkdrag00nv2 darkdrag00nv2 changed the title Introduce reverse in Variable/Fixed size Array Introduce reverseInPlace in Variable/Fixed size Array Jul 11, 2023
@darkdrag00nv2
Copy link
Contributor Author

Have decided to update the function name to reverseInPlace based on the suggestion on #2654.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants