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

Open
wants to merge 3 commits 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

New tests were added for Data.

Benchmarks

New benchmarks were added for Data.

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 3 times, most recently from 2f3103b to bc8b0ce Compare June 30, 2025 20:12
@vanvoorden vanvoorden force-pushed the data-identical branch 3 times, most recently from baa372f to 03ba1a9 Compare August 11, 2025 19:21
@vanvoorden vanvoorden marked this pull request as ready for review August 13, 2025 00:25
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