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

Make ArrayOverBuffer behave like an Array/Array.sort no longer mutates the Array #4022

Merged
merged 8 commits into from
Jan 9, 2023

Conversation

hubertp
Copy link
Collaborator

@hubertp hubertp commented Jan 4, 2023

Pull Request Description

Most of the problems with accessing ArrayOverBuffer have been resolved by using CoerceArrayNode (#3817). In Array.sort we still however specialized on Array which wasn't compatible with ArrayOverBuffer. Similarly sorting JS or Python arrays wouldn't work.

Added a specialization to Array.sort to deal with that case. A generic specialization (with hasArrayElements) not only handles ArrayOverBuffer but also polyglot arrays coming from JS or Python. We could have an additional specialization for ArrayOverBuffer only (removed in the last commit) that returns ArrayOverBuffer rather than Array although that adds additional complexity which so far is unnecessary.

Also fixed an example in Array.enso by providing a default argument.

Checklist

Please include the following checklist in your PR:

  • The documentation has been updated if necessary.
  • All code conforms to the
    Scala,
    Java,
    and
    Rust
    style guides.
  • All code has been tested:
    • Unit tests have been written where possible.

Copy link
Member

@JaroslavTulach JaroslavTulach left a comment

Choose a reason for hiding this comment

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

I don't like Array.sort as it breaks referential transparency. But we have got it - e.g. good job on making the sort work on various types of arrays.

@hubertp hubertp changed the title Make ArrayOverBuffer behave like an Array Make ArrayOverBuffer behave like an Array/Array.sort no longer mutates the Array Jan 5, 2023
@hubertp hubertp force-pushed the wip/hubert/arrayoverbuffer-fixes-183058716 branch 3 times, most recently from 7a547df to 0de2ac7 Compare January 9, 2023 08:49
@hubertp hubertp requested a review from radeusgd January 9, 2023 09:50
Comment on lines +95 to +96
arr = vec.map .x . to_array
sorted = arr.sort
Copy link
Member

Choose a reason for hiding this comment

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

This is not a Python array anymore, right? After a Vector.map operation, this will be an Enso allocated Java array, no?

Still, a good test but this part is not really testing what it says - only the part below is actually invoking our sort on a Python array instance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed, yes.

Copy link
Member

@radeusgd radeusgd left a comment

Choose a reason for hiding this comment

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

Looks good, just the comment should be updated before the merge as it can be misleading (we do not do in-place sort anymore, comment is suggesting that)

Most of the problems with accessing `ArrayOverBuffer` have been resolved
by using `CoerceArrayNode` (#3817).
In `Array.sort` we still however specialized on Array which wasn't
compatible with `ArrayOverBuffer`.

Added a specialization to `Array.sort` to deal with that case. Because
one cannot use a custom comparator with primitive (Java) arrays there is
a conversion needed. It's a necessary penalty if we want to keep
`ArrayOverBuffer` around.

Also fixed an example in `Array.enso` by providing a default argument.
Sorting in place would be impractical to support for Arrays coming from
JS or Python.
That is why `Array.sort` now returns a copy of the original Array(-like)
structure, sorted.
This eliminates the need for createing a copy of Array for
`Vector.sort`.

Applied DRY to displaying Array-like structures.
Skip ArrayOverBuffer specialization as it is now handled by
a generic `hasArrayElements` one.
@hubertp hubertp force-pushed the wip/hubert/arrayoverbuffer-fixes-183058716 branch from 0de2ac7 to 99134ff Compare January 9, 2023 14:54
@hubertp hubertp added the CI: Ready to merge This PR is eligible for automatic merge label Jan 9, 2023
@mergify mergify bot merged commit ae0889e into develop Jan 9, 2023
@mergify mergify bot deleted the wip/hubert/arrayoverbuffer-fixes-183058716 branch January 9, 2023 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: Ready to merge This PR is eligible for automatic merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants