Skip to content

Conversation

@vanvoorden
Copy link
Contributor

@vanvoorden vanvoorden commented Jun 6, 2025

Background

SE-0494

We propose new isTriviallyIdentical(to:) 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

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 {
  @available(SwiftStdlib 6.3, *)
  public func isTriviallyIdentical(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.

Substring

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

extension Substring {
  @available(SwiftStdlib 6.3, *)
  public func isTriviallyIdentical(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
@vanvoorden vanvoorden force-pushed the string-identical branch 2 times, most recently from 09d49a8 to 56da8c0 Compare June 24, 2025 07:26
@vanvoorden vanvoorden marked this pull request as ready for review August 12, 2025 07:06
@vanvoorden vanvoorden requested review from a team and eeckstein as code owners August 12, 2025 07:06
@vanvoorden vanvoorden force-pushed the string-identical branch 2 times, most recently from d26fa93 to 753979f Compare August 26, 2025 00:51
@vanvoorden vanvoorden changed the title [StdLib][RFC][DNM] Add isIdentical Methods for Quick Comparisons to String and Substring [SE-0494][StdLib] Add isTriviallyIdentical(to:) Methods for Quick Comparisons to String and Substring Oct 16, 2025
Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(In hindsight, we should've proposed adding the same methods to [Sub]String.UnicodeScalarView as well -- the scalar views are supposed to be fully formed string types. Oh well, there is always tomorrow!)

@vanvoorden
Copy link
Contributor Author

(In hindsight, we should've proposed adding the same methods to [Sub]String.UnicodeScalarView as well -- the scalar views are supposed to be fully formed string types. Oh well, there is always tomorrow!)

https://github.com/swiftlang/swift-evolution/blob/main/proposals/0494-add-is-identical-methods.md#detailed-design

@lorentey No… we did actually add UnicodeScalarView to the proposal. That's been accepted.

vanvoorden and others added 2 commits October 16, 2025 15:58
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
tags: [.validation, .String]))
}

if #available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lorentey I think this is the right way to guard from benchmarks… and then we have to go in and change this after 6.3 goes live to prod?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we have SwiftStdlib 6.3 that you should use instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Azoy I'm pretty sure I tried that before… but SwiftStdlib 6.3 was not available from benchmarks. Was there a recent change to support that?

@vanvoorden
Copy link
Contributor Author

@swift-ci Please smoke test

@vanvoorden vanvoorden changed the title [SE-0494][StdLib] Add isTriviallyIdentical(to:) Methods for Quick Comparisons to String and Substring [SE-0494][StdLib] Add isTriviallyIdentical(to:) Methods to String and Substring Oct 20, 2025
@vanvoorden
Copy link
Contributor Author

@swift-ci Please smoke test

@vanvoorden vanvoorden requested review from Azoy and lorentey October 22, 2025 18:57
@vanvoorden
Copy link
Contributor Author

@swift-ci Please smoke test

@vanvoorden
Copy link
Contributor Author

@lorentey Something that's not totally clear to me is when new test cases should go under test or under validation-test… any ideas?

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.

3 participants