Skip to content

[RFC][DNM] Add isIdentical Method for Quick Comparisons to Data #1384

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vanvoorden
Copy link
Contributor

@vanvoorden vanvoorden commented Jun 26, 2025

Background

#1383

We propose new isIdentical instance methods to the following concrete types for determining in constant-time if two instances must be equal by-value:

  • AttributedString
  • AttributedSubstring
  • Data

Instead of “one big diff”… we can try and keep the diffs grouped together by similar functionality:

  • AttributedString, AttributedSubstring
  • Data

Changes

Most of our clues to check identity equality come from the new "fast path" we landed on Data.1

extension Data {
  @_alwaysEmitIntoClient
  public func isIdentical(to other: Self) -> Bool {
    // See if both are empty
    ...

    // Unequal length data can never be equal
    ...
    
    if length1 > 0 {
        return self.withUnsafeBytes { (b1: UnsafeRawBufferPointer) in
            return other.withUnsafeBytes { (b2: UnsafeRawBufferPointer) in
                // If they have the same base address and same count, it is equal
                ...
            }
        }
    }
    return true
  }
}

Test Plan

TODO

Benchmarks

TODO

Footnotes

  1. https://github.com/swiftlang/swift-foundation/pull/1322

@vanvoorden vanvoorden marked this pull request as draft June 26, 2025 23:42
@vanvoorden vanvoorden changed the title [WIP][DNM] data identical [RFC][DNM] data identical Jun 27, 2025
@vanvoorden vanvoorden changed the title [RFC][DNM] data identical [RFC][DNM] Add isIdentical Method for Quick Comparisons to Data Jun 27, 2025
@vanvoorden vanvoorden force-pushed the data-identical branch 2 times, most recently from c94082e to 2f3103b Compare June 30, 2025 20:12
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.

1 participant