- 
                Notifications
    You must be signed in to change notification settings 
- Fork 10.6k
          [SE-0494][StdLib] Add isTriviallyIdentical(to:) Methods to Dictionary and Set
          #82439
        
          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
      
      
            vanvoorden
  wants to merge
  4
  commits into
  swiftlang:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
vanvoorden:dictionary-identical
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| //===--- DictionaryIdentical.swift ----------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|  | ||
| import TestsUtils | ||
|  | ||
| public let benchmarks = [ | ||
| BenchmarkInfo(name: "DictionaryEqualUnique", runFunction: run_DictionaryEqualUnique, tags: [.validation, .api, .Dictionary]), | ||
| BenchmarkInfo(name: "DictionaryEqualShared", runFunction: run_DictionaryEqualShared, tags: [.validation, .api, .Dictionary]), | ||
| BenchmarkInfo(name: "DictionaryIdentical", runFunction: run_DictionaryIdentical, tags: [.validation, .api, .Dictionary]), | ||
| ] | ||
|  | ||
| @inline(never) | ||
| public func run_DictionaryEqualUnique(_ n: Int) { | ||
| let d1 = Dictionary(uniqueKeysWithValues: zip(1...n, 1...n)) | ||
| let d2 = Dictionary(uniqueKeysWithValues: zip(1...n, 1...n)) | ||
| for _ in 0 ..< 100_000 { | ||
| check(d1 == d2) | ||
| } | ||
| } | ||
|  | ||
| @inline(never) | ||
| public func run_DictionaryEqualShared(_ n: Int) { | ||
| let d1 = Dictionary(uniqueKeysWithValues: zip(1...n, 1...n)) | ||
| let d2 = d1 | ||
| for _ in 0 ..< 100_000 { | ||
| check(d1 == d2) | ||
| } | ||
| } | ||
|  | ||
| @inline(never) | ||
| public func run_DictionaryIdentical(_ n: Int) { | ||
| let d1 = Dictionary(uniqueKeysWithValues: zip(1...n, 1...n)) | ||
| let d2 = d1 | ||
| for _ in 0 ..< 100_000 { | ||
| check(d1.isTriviallyIdentical(to: d2)) | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| //===--- SetIdentical.swift -----------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|  | ||
| import TestsUtils | ||
|  | ||
| public let benchmarks = [ | ||
| BenchmarkInfo(name: "SetEqualUnique", runFunction: run_SetEqualUnique, tags: [.validation, .api, .Set]), | ||
| BenchmarkInfo(name: "SetEqualShared", runFunction: run_SetEqualShared, tags: [.validation, .api, .Set]), | ||
| BenchmarkInfo(name: "SetIdentical", runFunction: run_SetIdentical, tags: [.validation, .api, .Set]), | ||
| ] | ||
|  | ||
| @inline(never) | ||
| public func run_SetEqualUnique(_ n: Int) { | ||
| let s1 = Set(1...n) | ||
| let s2 = Set(1...n) | ||
| for _ in 0 ..< 100_000 { | ||
| check(s1 == s2) | ||
| } | ||
| } | ||
|  | ||
| @inline(never) | ||
| public func run_SetEqualShared(_ n: Int) { | ||
| let s1 = Set(1...n) | ||
| let s2 = s1 | ||
| for _ in 0 ..< 100_000 { | ||
| check(s1 == s2) | ||
| } | ||
| } | ||
|  | ||
| @inline(never) | ||
| public func run_SetIdentical(_ n: Int) { | ||
| let s1 = Set(1...n) | ||
| let s2 = s1 | ||
| for _ in 0 ..< 100_000 { | ||
| check(s1.isTriviallyIdentical(to: s2)) | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // RUN: %target-run-simple-swift(-parse-as-library) | ||
| // REQUIRES: executable_test | ||
| // END. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|  | ||
| import StdlibUnittest | ||
|  | ||
| @main | ||
| enum DictionaryTests { | ||
| static func main() { | ||
| let testSuite = TestSuite("DictionaryTests") | ||
| testSuite.test("Identical", testIdentical) | ||
| runAllTests() | ||
| } | ||
|  | ||
| static func testIdentical() { | ||
| let d1: Dictionary = ["a": 1, "b": 2, "c": 3] | ||
| expectTrue(d1.isTriviallyIdentical(to: d1)) | ||
|  | ||
| let d2: Dictionary = d1 | ||
| expectTrue(d1.isTriviallyIdentical(to: d2)) | ||
|  | ||
| var d3: Dictionary = d2 | ||
| d3.reserveCapacity(0) | ||
| expectFalse(d1.isTriviallyIdentical(to: d3)) | ||
|  | ||
| let d4: Dictionary = ["a": 1, "b": 2, "c": 3] | ||
| expectFalse(d1.isTriviallyIdentical(to: d4)) | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // RUN: %target-run-simple-swift(-parse-as-library) | ||
| // REQUIRES: executable_test | ||
| // END. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|  | ||
| import StdlibUnittest | ||
|  | ||
| @main | ||
| enum SetTests { | ||
| static func main() { | ||
| let testSuite = TestSuite("SetTests") | ||
| testSuite.test("Identical", testIdentical) | ||
| runAllTests() | ||
| } | ||
|  | ||
| static func testIdentical() { | ||
| let s1: Set = [0, 1, 2, 3] | ||
| expectTrue(s1.isTriviallyIdentical(to: s1)) | ||
|  | ||
| let s2: Set = s1 | ||
| expectTrue(s1.isTriviallyIdentical(to: s2)) | ||
|  | ||
| var s3: Set = s2 | ||
| s3.reserveCapacity(0) | ||
| expectFalse(s1.isTriviallyIdentical(to: s3)) | ||
|  | ||
| let s4: Set = [0, 1, 2, 3] | ||
| expectFalse(s1.isTriviallyIdentical(to: s4)) | ||
| } | ||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lorentey Opaque here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type is generic, so sadly the answer is no -- we need this to be at least
@inlinable.@_aeicis fine.