Skip to content

[RFC][DNM] Add isIdentical Methods for Quick Comparisons to AttributedString and AttributedSubstring #1385

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

AttributedString

We can look for some clues in the existing == operator on AttributedString.1 This forwards to methods on Guts.234 A simpler approach for our diff would be to just check directly over guts without an extra transformation:

extension AttributedString {
  public func isIdentical(to other: Self) -> Bool {
    self._guts === other._guts
  }
}

An orthogonal diff could also choose to check over guts as a fast-path in our == operator.

Because _guts is defined as internal without usableFromInline we do not have an easy way to make isIdentical back deploy with @_alwaysEmitIntoClient. I am open to discussing what our options might be at this point might be to back deploy and what we choose for availability before shipping. It looks like we do have the option to add usableFromInline to our internal variable declarations without breaking ABI.5 The tradeoff is we then ship guts as ABI from now on.

AttributedSubstring

We already have a fast path in the existing == operator on AttributedSubstring.6 We can implement a similar logic for isIdentical:

extension AttributedSubstring {
  public func isIdentical(to other: Self) -> Bool {
    self._guts === other._guts &&
    self._range == other._range
  }
}

We can have a similar conversation at this point about what our options for back deployment look like.

Test Plan

TODO

Benchmarks

TODO

Footnotes

  1. https://github.com/swiftlang/swift-foundation/blob/swift-6.1.2-RELEASE/Sources/FoundationEssentials/AttributedString/AttributedString.swift#L153

  2. https://github.com/swiftlang/swift-foundation/blob/swift-6.1.2-RELEASE/Sources/FoundationEssentials/AttributedString/AttributedString%2BGuts.swift#L77

  3. https://github.com/swiftlang/swift-foundation/blob/swift-6.1.2-RELEASE/Sources/FoundationEssentials/AttributedString/AttributedString%2BGuts.swift#L84-L86

  4. https://github.com/swiftlang/swift-foundation/blob/swift-6.1.2-RELEASE/Sources/FoundationEssentials/AttributedString/AttributedString%2BGuts.swift#L105

  5. https://github.com/swiftlang/swift-evolution/blob/main/proposals/0193-cross-module-inlining-and-specialization.md

  6. https://github.com/swiftlang/swift-foundation/blob/swift-6.1.2-RELEASE/Sources/FoundationEssentials/AttributedString/AttributedSubstring.swift#L79-L81

@vanvoorden vanvoorden marked this pull request as draft June 26, 2025 23:42
@vanvoorden vanvoorden changed the title [WIP][DNM] string identical [RFC][DNM] string identical Jun 27, 2025
@vanvoorden vanvoorden changed the title [RFC][DNM] string identical [RFC][DNM] Add isIdentical Methods for Quick Comparisons to AttributedString and AttributedSubstring Jun 27, 2025
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