Skip to content

[RFC][DNM] Add isIdentical Methods for Quick Comparisons to String and Substring #82055

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 3 commits into
base: main
Choose a base branch
from

Conversation

vanvoorden
Copy link
Contributor

@vanvoorden vanvoorden commented Jun 6, 2025

Background

swiftlang/swift-evolution#2875

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

  • String
  • Substring
  • Array
  • ArraySlice
  • ContiguousArray
  • Dictionary
  • Set

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

  • String, Substring
  • Array, ArraySlice, ContiguousArray
  • Dictionary, Set

Let’s start with a diff to add isIdentical to String and Substring. These types share some implementation details that might not apply to Array or Dictionary, but we might also see some general patterns and opinions that we want to influence the remaining implementation diffs.

Changes

String

We already expose a public-but-underscored method on String to return identity equality.1 We can add a new public-and-supported method:

@available(SwiftStdlib 6.3, *)
extension String {
  @available(SwiftStdlib 6.3, *)
  public func isIdentical(to other: Self) -> Bool {
    self._isIdentical(to: other)
  }
}

And just call the existing underscored method.

What happens to the existing underscored method?

  • Delete it?
  • Deprecate it?
  • Leave it?

For now… we don't need to have a strong opinion about the underscored method. The underscored method is alwaysEmitIntoClient and already deploys back to earlier releases. Deleting the underscored method would then mean a breaking change for apps that already depend on the underscored method unless we then back-deploy the new isIdentical method.

These implementation diffs will unblock a formal proposal review. My understanding is we don't need to block a proposal review on deciding the fate of the underscored method or a strategy about back-deployment on the new method. We can keep that conversation going and either update this diff with a new strategy on back-deployment or land some cleanup in a follow up diff before the next branch cut.

Substring

We do not currently expose identity equality on Substring. We can follow a similar pattern to String:

extension Substring {
  @_alwaysEmitIntoClient
  public func _isIdentical(to other: Self) -> Bool {
    self._wholeGuts.rawBits == other._wholeGuts.rawBits && self._offsetRange == other._offsetRange
  }
}

@available(SwiftStdlib 6.3, *)
extension Substring {
  @available(SwiftStdlib 6.3, *)
  public func isIdentical(to other: Self) -> Bool {
    self._isIdentical(to: other)
  }
}

We can add a public-but-underscored method that back-deploys and a public-and-supported method that calls the underscored method.

If we decide to go with a different strategy with String to remove or deprecate the existing underscored method we can also decide not to ship an underscored method on Substring.

Test Plan

New tests were added for String and Substring.

Benchmarks

New benchmarks were added for String and Substring.

It looks like there might not be easy support to write benchmarks for symbols that have not yet shipped to prod:

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *)
public func run_StringIdentical(_ n: Int) {
  let str1 = "..."
  let str2 = str1
  for _ in 0 ..< n {
    for _ in 0 ..< 100_000 {
      check(str1.isIdentical(to: str2))
    }
  }
}

AFAIK we do not have access to SwiftStdlib 6.3 from benchmarks… so we guard with 9999. My understanding is we then clean this up with a follow up diff once we know the exact OS versions that SwiftStdlib 6.3 maps to.2

Footnotes

  1. https://github.com/swiftlang/swift/blob/swift-6.1.2-RELEASE/stdlib/public/core/String.swift#L397-L415

  2. https://github.com/swiftlang/swift/pull/74366/files

@vanvoorden vanvoorden force-pushed the string-identical branch 3 times, most recently from 0e4eedc to d174d92 Compare June 10, 2025 18:10
@vanvoorden vanvoorden changed the title [WIP][DNM] Add isIdentical Methods for Quick Comparisons to String and Substring [RFC][DNM] Add isIdentical Methods for Quick Comparisons to String and Substring Jun 12, 2025
@vanvoorden
Copy link
Contributor Author

@lorentey No big rush… but if you had any advice about these implementations that would be a big help. I'm planning to post another pitch to swift forums on monday. It's just a pitch thread… so it's not blocked on implementation diffs.

I would expect at least one more week before I would have some more implementations for the rest of the types to unblock a proposal review thread.

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