Skip to content

[StdLib][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 2 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.

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:

extension String {
  @_alwaysEmitIntoClient
  public func isIdentical(to other: Self) -> Bool {
    self._guts.rawBits == other._guts.rawBits
  }
}

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.

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. We can keep that conversation going and either update this diff with a new strategy 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
  }
}

Test Plan

New tests were added for String and Substring.

Benchmarks

New benchmarks were added for String and Substring.

Footnotes

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

@vanvoorden vanvoorden force-pushed the string-identical branch 4 times, most recently from b140d00 to 7634c46 Compare June 12, 2025 03:35
@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.

@vanvoorden vanvoorden force-pushed the string-identical branch 5 times, most recently from 7462596 to e6db03f Compare June 16, 2025 18:43
@vanvoorden vanvoorden changed the title [RFC][DNM] Add isIdentical Methods for Quick Comparisons to String and Substring [StdLib][RFC][DNM] Add isIdentical Methods for Quick Comparisons to String and Substring Jun 23, 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