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

[OrderedDictionary] forward ordered dictionary values equality to values property #335

Conversation

vanvoorden
Copy link
Contributor

@vanvoorden vanvoorden commented Dec 8, 2023

Background

#334

The current implementation of OrderedDictionary.Values.== forwards to Sequence.elementsEqual, which needs to perform a linear-time (O[N]) comparison over both collections.[1]

Since the "backing store" of our OrderedDictionary.Values is a ContiguousArray, we can forward that equality check through to ContiguousArray. If both Values instances point to the same identity, the ContiguousArray implementation will return early from the comparison.[2]

We perform a similar check (against the ContiguousArray) in our implementation of OrderedDictionary.==.[3]

[1] https://github.com/apple/swift/blob/main/stdlib/public/core/SequenceAlgorithms.swift#L319-L336
[2] https://github.com/apple/swift/blob/main/stdlib/public/core/ContiguousArray.swift#L1318-L1321
[3] https://github.com/apple/swift-collections/blob/main/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary%2BEquatable.swift#L20-L22


Changes

We migrate from:

public static func ==(left: Self, right: Self) -> Bool {
  left.elementsEqual(right)
}

We migrate to:

public static func ==(left: Self, right: Self) -> Bool {
  left._base._values == right._base._values
}

Test Plan

Two new tests are added:

  • OrderedDictionaryValueTests.test_values_getter_equal
  • OrderedDictionaryValueTests.test_values_getter_not_equal

Checklist

  • I've read the Contribution Guidelines
  • My contributions are licensed under the Swift license.
  • I've followed the coding style of the rest of the project.
  • I've added tests covering all new code paths my change adds to the project (if appropriate).
  • I've added benchmarks covering new functionality (if appropriate).
  • I've verified that my change does not break any existing tests or introduce unexplained benchmark regressions.
  • I've updated the documentation if necessary.

@vanvoorden vanvoorden requested a review from lorentey as a code owner December 8, 2023 00:59
@vanvoorden
Copy link
Contributor Author

@swift-ci test

@vanvoorden
Copy link
Contributor Author

@ingoem thanks for suggesting the reference equality check!

Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

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

Nice! 👍

@lorentey
Copy link
Member

lorentey commented Dec 8, 2023

@vanvoorden Can you please rebase this PR on top of the release/1.0 branch, rather than main?

I think it would make sense to ship this in the next patch release, rather than having it linger on main indefinitely.

@lorentey lorentey added this to the 1.0.6 milestone Dec 8, 2023
@vanvoorden vanvoorden changed the base branch from main to release/1.0 December 9, 2023 01:12
@vanvoorden vanvoorden changed the base branch from release/1.0 to main December 9, 2023 01:12
@vanvoorden vanvoorden changed the base branch from main to release/1.0 December 9, 2023 01:30
@vanvoorden vanvoorden changed the base branch from release/1.0 to main December 9, 2023 01:30
@vanvoorden vanvoorden force-pushed the vanvoorden/ordered-dictionary-values-equality branch from 598b5f9 to e42fb86 Compare December 9, 2023 01:42
@vanvoorden vanvoorden changed the base branch from main to release/1.0 December 9, 2023 01:42
@vanvoorden
Copy link
Contributor Author

I think it would make sense to ship this in the next patch release, rather than having it linger on main indefinitely.

@lorentey Sounds good. Thanks!

@vanvoorden
Copy link
Contributor Author

@lorentey Is there a CI job that can run Benchmarks on a diff to detect for regressions or improvements against base?

@vanvoorden
Copy link
Contributor Author

2023-12-11-1 2023-12-11-2

Here are benchmarks running locally. The reason that OrderedDictionary.== is scaling linearly is because we are waiting on #340. Optimizing the OrderedSet equality will optimize this one.

@lorentey lorentey added the OrderedCollections OrderedSet and OrderedDictionary label Dec 12, 2023
Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

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

Thanks for rebasing. Nice work on the benchmarks! 👍

Reviewing #340 made me realize what's been bothering me about expectNotEqualElements; I added new recommendations to remove it and the test that exercises it. (Please see the notes below.)

vanvoorden and others added 3 commits December 12, 2023 11:17
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
@vanvoorden
Copy link
Contributor Author

@lorentey Thanks for reviewing!

@lorentey
Copy link
Member

@swift-ci test

@lorentey
Copy link
Member

(AFAIK, CI runs are still expected to fail on macOS. This is "fine", and it won't block us from landing this.)

@lorentey lorentey merged commit 2a94309 into apple:release/1.0 Dec 12, 2023
1 of 2 checks passed
cgrindel-self-hosted-renovate bot referenced this pull request in cgrindel/rules_swift_package_manager Dec 21, 2023
…v1.0.6 (#822)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
|
[com_github_apple_swift_collections](https://togithub.com/apple/swift-collections)
| http_archive | patch | `1.0.5` -> `1.0.6` |

---

### Release Notes

<details>
<summary>apple/swift-collections
(com_github_apple_swift_collections)</summary>

###
[`v1.0.6`](https://togithub.com/apple/swift-collections/releases/tag/1.0.6):
Swift Collections 1.0.6

[Compare
Source](https://togithub.com/apple/swift-collections/compare/1.0.5...1.0.6)

This bugfix release adds `Sendable` conformances to all public types
(fixing compatibility with Swift's strict concurrency checking), and
speeds up equality checks (`==`) of identical collection values.

##### What's Changed

- Fix typos: OrderedSet Documentation by
[@&#8203;kati-kms](https://togithub.com/kati-kms) in
[https://github.com/apple/swift-collections/pull/322](https://togithub.com/apple/swift-collections/pull/322)
- \[1.0] build: support building in Debug mode on Windows by
[@&#8203;compnerd](https://togithub.com/compnerd) in
[https://github.com/apple/swift-collections/pull/337](https://togithub.com/apple/swift-collections/pull/337)
- build: tweak search path for embedding by
[@&#8203;compnerd](https://togithub.com/compnerd) in
[https://github.com/apple/swift-collections/pull/338](https://togithub.com/apple/swift-collections/pull/338)
- \[OrderedDictionary] forward ordered dictionary values equality to
values property by [@&#8203;vanvoorden](https://togithub.com/vanvoorden)
in
[https://github.com/apple/swift-collections/pull/335](https://togithub.com/apple/swift-collections/pull/335)
- \[OrderedSet] forward ordered set equality to elements property by
[@&#8203;vanvoorden](https://togithub.com/vanvoorden) in
[https://github.com/apple/swift-collections/pull/340](https://togithub.com/apple/swift-collections/pull/340)
- \[Deque] check deque equality with buffer identity by
[@&#8203;vanvoorden](https://togithub.com/vanvoorden) in
[https://github.com/apple/swift-collections/pull/341](https://togithub.com/apple/swift-collections/pull/341)
- \[OrderedDictionary] Fix usage of deprecated API in index(forKey:)
docs by [@&#8203;lorentey](https://togithub.com/lorentey) in
[https://github.com/apple/swift-collections/pull/342](https://togithub.com/apple/swift-collections/pull/342)
- \[1.0] Backport Sendable conformances on all public types by
[@&#8203;lorentey](https://togithub.com/lorentey) in
[https://github.com/apple/swift-collections/pull/343](https://togithub.com/apple/swift-collections/pull/343)
- OrderedSet: Fix sendable conformance on old swifts by
[@&#8203;lorentey](https://togithub.com/lorentey) in
[https://github.com/apple/swift-collections/pull/346](https://togithub.com/apple/swift-collections/pull/346)
- Update CMake configuration by
[@&#8203;lorentey](https://togithub.com/lorentey) in
[https://github.com/apple/swift-collections/pull/347](https://togithub.com/apple/swift-collections/pull/347)

##### New Contributors

- [@&#8203;kati-kms](https://togithub.com/kati-kms) made their first
contribution in
[https://github.com/apple/swift-collections/pull/322](https://togithub.com/apple/swift-collections/pull/322)
- [@&#8203;vanvoorden](https://togithub.com/vanvoorden) made their first
contribution in
[https://github.com/apple/swift-collections/pull/335](https://togithub.com/apple/swift-collections/pull/335)

**Full Changelog**:
apple/swift-collections@1.0.5...1.0.6

Thank you to everyone who contributed to this release!

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4xMDAuMCIsInVwZGF0ZWRJblZlciI6IjM2LjEwMC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: Self-hosted Renovate Bot <361546+cgrindel-self-hosted-renovate[bot]@users.noreply.github.enterprise.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OrderedCollections OrderedSet and OrderedDictionary
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants