apple / swift-collections

Commonly used data structures for Swift
Apache License 2.0
3.55k stars 270 forks source link

[Deque] check deque equality with buffer identity #341

Closed vanvoorden closed 5 months ago

vanvoorden commented 5 months ago

Background

https://github.com/apple/swift-collections/pull/335

Similar to what we added for OrderedDictionary, we can try and optimize our Deque equality checks for the case where two Deque instances point to the same identity.

Unlike OrderedDictionary, our Deque is not implemented on ContiguousArray. We can try and mimic the approach from ContiguousArray here in Deque by checking against the identity of the underlying ManagedBufferPointer instance.


Changes

From:

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

To:

let lhsCount = left.count
if lhsCount != right.count {
  return false
}

// Test referential equality.
if lhsCount == 0 || left._storage.isIdentical(to: right._storage) {
  return true
}

return left.elementsEqual(right)

Test Plan

We leverage the existing DequeTests.


Open Questions

Are there any additional edge-casey behaviors we should be testing for?


Checklist

vanvoorden commented 5 months ago
2023-12-11

Here are benchmarks from running locally.

lorentey commented 5 months ago

Note to self: letting Deque.SubSequence be the default Slice<Deque> means that it can't conform to Equatable/Hashable, and that is probably a defect in the Standard Library.

lorentey commented 5 months ago

@swift-ci test

vanvoorden commented 5 months ago

@lorentey Thanks for reviewing!