Skip to content

SR-7266: Avoid nesting of ReversedCollection #15815

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

Merged
merged 3 commits into from
Apr 12, 2018

Conversation

Moximillian
Copy link
Contributor

Applying reversed() two times should return original collection.

First round of fixes for the SR-7266, to avoid nesting multiple ReversedCollections and instead return the original collection with two consecutive .reversed() functions.

Does not yet fix the .lazy use case.

Resolves SR-7266.

@danielctull
Copy link
Contributor

Hi there, I started looking at this bug last weekend with a solution to the lazy scenario, which you can find at this gist: https://gist.github.com/danielctull/ec521f3fc2aadabb17d38f716e55c5f7

It seems tricky to constrain LazyCollectionProtocol in a way to overload a version of the reversed() function when the lazy's base is itself a ReversedCollection. I've been meaning to raise this as a PR, to see whether the additional protocol would justified for use only in this scenario.

@Moximillian
Copy link
Contributor Author

Hi, yes, I also ended up with having to create ReversedCollectionProtocol for the lazy case. It's now updated to the PR.

@Moximillian
Copy link
Contributor Author

Moximillian commented Apr 7, 2018

Would
extension LazyCollectionProtocol where Self: BidirectionalCollection, Elements: ReversedCollectionProtocol {}
be preferable over
extension LazyCollection where Base: ReversedCollectionProtocol {}
?

Copy link
Contributor

@moiseev moiseev left a comment

Choose a reason for hiding this comment

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

@Moximillian, thanks for addressing the issue!

AFAIK @danielctull has tests and properly set availability attributes for the old and new reversed overloads. It would make a lot of sense for you two to merge efforts.

Let's concentrate on a non-lazy case first.

@@ -253,6 +253,19 @@ extension ReversedCollection: BidirectionalCollection {

extension ReversedCollection: RandomAccessCollection where Base: RandomAccessCollection { }

extension ReversedCollection {
/// This is optimization to return identity of doubly reversed collection
Copy link
Contributor

Choose a reason for hiding this comment

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

This part of the comment is an implementation detail, I don't think it should be a doc-comment.

// For example [1,2].reversed().reversed() => [1,2]

/// Returns the elements of the original collection (reversal of reversal)
public func reversed() -> Base {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is a more concrete overload of an existing method, we should be careful not to break existing code.

The type of let xs = [1,2,3].reversed().reversed() changes. This new overload should be @available(swift, introduced: 4.2).

@danielctull
Copy link
Contributor

danielctull commented Apr 9, 2018

As @moiseev said, I have a branch which adds tests and availability attributes which you can see in this comparison.

@Moximillian
Copy link
Contributor Author

My first starterbug so please bear with me :)

@moiseev:

  1. I amended the comments to align with @danielctull
  2. added availability and inline
  3. added the testcase for non-lazy variant from @danielctull
  4. I don't know if you meant this, but the PR now only contains the non-lazy case, which means it does not add or use the new ReversedCollectionProtocol at all.

@danielctull
Copy link
Contributor

Hi @Moximillian! I've sent you a more long winded email but to paraphrase here: Because I'm a little vain and I'd like to be able to have recognition for my work, I split my test commit into the regular and lazy checks and rebased that into your branch. You can see this here: https://github.com/danielctull-forks/swift/commits/SR-7266-avoid-nesting-reversed. It keeps all of you other changes, but allows me to be the committer for the test additions.

You could either reset my branch or cherry-pick my commit dbdd74a8a8fcf6f70cc9810fe81b5efaa18bd57e and force push to your fork to update this PR.

Applying reversed() two times should return identity.
@Moximillian Moximillian force-pushed the SR-7266-avoid-nesting-reversed branch from 768c1c3 to b859cdc Compare April 10, 2018 14:06
@Moximillian
Copy link
Contributor Author

Moximillian commented Apr 10, 2018

Thanks @danielctull, squashed my commits and cherry-picked your test commit. Git is amazing tool, interesting to learn more about it :)

So all-in-all, this is now same as before: just adding the non-lazy optimisation for ReversedCollection + tests for it.

@danielctull
Copy link
Contributor

Cool, cheers @Moximillian. I think this line to run the test was lost when I split out the lazy tests earlier, if you want to merge this one in too: 110655befd06952c82f4f28406476da573962d8c

I ran these on the previous branch (before you squashed) and it ran fine! 😁

@Moximillian
Copy link
Contributor Author

Cherry-picked the missing line as well

@@ -84,7 +82,7 @@ extension ReversedCollection {
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: Unnecessary space at the end of the line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so my change removes the unnecessary space from that line. Or would you rather not have those kind of cleanups in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh. I misinterpreted the diff. I would prefer to not mix the cleanups with functional changes, but am guilty of doing it every now and then myself. So let's keep it.

@moiseev
Copy link
Contributor

moiseev commented Apr 11, 2018

@swift-ci Please test

@moiseev
Copy link
Contributor

moiseev commented Apr 11, 2018

@swift-ci Please Test Source Compatibility

@moiseev
Copy link
Contributor

moiseev commented Apr 12, 2018

For the record. The binary size difference is negligible.

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

Successfully merging this pull request may close these issues.

3 participants