File: ObjectiveCBridging.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (849 lines) | stat: -rw-r--r-- 25,829 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
//===--- ObjectiveCBridging.swift -----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 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
import Foundation

let t: [BenchmarkCategory] = [.validation, .bridging]
let ts: [BenchmarkCategory] = [.validation, .bridging, .String]

public let benchmarks = [
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSString",
    runFunction: run_ObjectiveCBridgeFromNSString, tags: t,
    legacyFactor: 5),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSStringForced",
    runFunction: run_ObjectiveCBridgeFromNSStringForced, tags: t,
    legacyFactor: 5),
  BenchmarkInfo(name: "ObjectiveCBridgeToNSString",
    runFunction: run_ObjectiveCBridgeToNSString, tags: t),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSArrayAnyObject",
    runFunction: run_ObjectiveCBridgeFromNSArrayAnyObject, tags: t,
    legacyFactor: 100),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSArrayAnyObjectForced",
    runFunction: run_ObjectiveCBridgeFromNSArrayAnyObjectForced, tags: t,
    legacyFactor: 20),
  BenchmarkInfo(name: "ObjectiveCBridgeToNSArray",
    runFunction: run_ObjectiveCBridgeToNSArray, tags: t,
    legacyFactor: 50),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSArrayAnyObjectToString",
    runFunction: run_ObjectiveCBridgeFromNSArrayAnyObjectToString, tags: ts,
    legacyFactor: 100),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSArrayAnyObjectToStringForced",
    runFunction: run_ObjectiveCBridgeFromNSArrayAnyObjectToStringForced,
    tags: ts, legacyFactor: 200),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSDictionaryAnyObject",
    runFunction: run_ObjectiveCBridgeFromNSDictionaryAnyObject, tags: t,
    legacyFactor: 100),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSDictionaryAnyObjectForced",
    runFunction: run_ObjectiveCBridgeFromNSDictionaryAnyObjectForced, tags: t,
    legacyFactor: 50),
  BenchmarkInfo(name: "ObjectiveCBridgeToNSDictionary",
    runFunction: run_ObjectiveCBridgeToNSDictionary, tags: t,
    legacyFactor: 50),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSDictionaryAnyObjectToString",
    runFunction: run_ObjectiveCBridgeFromNSDictionaryAnyObjectToString,
    tags: ts, legacyFactor: 500),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSDictionaryAnyObjectToStringForced",
    runFunction: run_ObjectiveCBridgeFromNSDictionaryAnyObjectToStringForced,
    tags: ts, legacyFactor: 500),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSSetAnyObject",
    runFunction: run_ObjectiveCBridgeFromNSSetAnyObject, tags: t,
    legacyFactor: 200),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSSetAnyObjectForced",
    runFunction: run_ObjectiveCBridgeFromNSSetAnyObjectForced, tags: t,
    legacyFactor: 20),
  BenchmarkInfo(name: "ObjectiveCBridgeToNSSet",
    runFunction: run_ObjectiveCBridgeToNSSet, tags: t,
    legacyFactor: 50),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSSetAnyObjectToString",
    runFunction: run_ObjectiveCBridgeFromNSSetAnyObjectToString, tags: ts,
    legacyFactor: 500),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSSetAnyObjectToStringForced",
    runFunction: run_ObjectiveCBridgeFromNSSetAnyObjectToStringForced, tags: ts,
    legacyFactor: 500),
  BenchmarkInfo(name: "ObjectiveCBridgeFromNSDateComponents",
    runFunction: run_ObjectiveCBridgeFromNSDateComponents, tags: t,
    setUpFunction: setup_dateComponents),
  BenchmarkInfo(name: "ObjectiveCBridgeASCIIStringFromFile",
                runFunction: run_ASCIIStringFromFile, tags: ts,
                setUpFunction: setup_ASCIIStringFromFile),
  BenchmarkInfo(name: "UnicodeStringFromCodable",
                 runFunction: run_UnicodeStringFromCodable, tags: ts,
                 setUpFunction: setup_UnicodeStringFromCodable),
  BenchmarkInfo(name: "NSArray.bridged.objectAtIndex",
                  runFunction: run_BridgedNSArrayObjectAtIndex, tags: t,
                  setUpFunction: setup_bridgedArrays),
  BenchmarkInfo(name: "NSArray.bridged.mutableCopy.objectAtIndex",
                  runFunction: run_BridgedNSArrayMutableCopyObjectAtIndex, tags: t,
                  setUpFunction: setup_bridgedArrays),
  BenchmarkInfo(name: "NSArray.nonbridged.objectAtIndex",
                  runFunction: run_RealNSArrayObjectAtIndex, tags: t,
                  setUpFunction: setup_bridgedArrays),
  BenchmarkInfo(name: "NSArray.nonbridged.mutableCopy.objectAtIndex",
                  runFunction: run_RealNSArrayMutableCopyObjectAtIndex, tags: t,
                  setUpFunction: setup_bridgedArrays),
]

#if _runtime(_ObjC)
@inline(never)
public func forcedCast<NS, T>(_ ns: NS) -> T {
  return ns as! T
}

@inline(never)
public func conditionalCast<NS, T>(_ ns: NS) -> T? {
  return ns as? T
}
#endif


// === String === //

#if _runtime(_ObjC)
func createNSString() -> NSString {
  return NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!
}

@inline(never)
func testObjectiveCBridgeFromNSString() {
  let nsString = createNSString()

  var s: String?
  for _ in 0 ..< 2_000 {
    // Call _conditionallyBridgeFromObjectiveC.
    let n : String? = conditionalCast(nsString)
    if n != nil {
      s = n!
    }
  }
  check(s != nil && s == "NSString that does not fit in tagged pointer")
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSString(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSString()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeFromNSStringForced() {
  let nsString = createNSString()

  var s: String?
  for _ in 0 ..< 2_000 {
    // Call _forceBridgeFromObjectiveC
    s = forcedCast(nsString)
  }
  check(s != nil && s == "NSString that does not fit in tagged pointer")
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSStringForced(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSStringForced()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeToNSString() {
  let nativeString = "Native"

  var s: NSString?
  for _ in 0 ..< 10_000 {
    // Call _BridgedToObjectiveC
    s = nativeString as NSString
  }
  check(s != nil && s == "Native")
}
#endif

@inline(never)
public func run_ObjectiveCBridgeToNSString(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeToNSString()
    }
  }
#endif
}

// === Array === //

#if _runtime(_ObjC)
func createNSArray() -> NSArray {
  let nsMutableArray = NSMutableArray()
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!
  nsMutableArray.add(nsString)
  nsMutableArray.add(nsString)
  nsMutableArray.add(nsString)
  nsMutableArray.add(nsString)
  nsMutableArray.add(nsString)
  nsMutableArray.add(nsString)
  nsMutableArray.add(nsString)
  nsMutableArray.add(nsString)
  nsMutableArray.add(nsString)
  nsMutableArray.add(nsString)
  nsMutableArray.add(nsString)
  return nsMutableArray.copy() as! NSArray
}

@inline(never)
func testObjectiveCBridgeFromNSArrayAnyObject() {
  let nsArray = createNSArray()

  var nativeString : String?
  for _ in 0 ..< 100 {
    if let nativeArray : [NSString] = conditionalCast(nsArray) {
       nativeString = forcedCast(nativeArray[0])
    }
  }
  check(nativeString != nil && nativeString! == "NSString that does not fit in tagged pointer")
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSArrayAnyObject(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSArrayAnyObject()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeFromNSArrayAnyObjectForced() {
  let nsArray = createNSArray()

  var nativeString : String?
  for _ in 0 ..< 500 {
    let nativeArray : [NSString] = forcedCast(nsArray)
    nativeString = forcedCast(nativeArray[0])
  }
  check(nativeString != nil && nativeString! == "NSString that does not fit in tagged pointer")
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSArrayAnyObjectForced(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSArrayAnyObjectForced()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeToNSArray() {
  let nativeArray = ["abcde", "abcde", "abcde", "abcde", "abcde",
    "abcde", "abcde", "abcde", "abcde", "abcde"]

  var nsString : Any?
  for _ in 0 ..< 200 {
    let nsArray = nativeArray as NSArray
    nsString = nsArray.object(at: 0)
  }
  check(nsString != nil && (nsString! as! NSString).isEqual("abcde"))
}
#endif

@inline(never)
public func run_ObjectiveCBridgeToNSArray(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeToNSArray()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeFromNSArrayAnyObjectToString() {
  let nsArray = createNSArray()

  var nativeString : String?
  for _ in 0 ..< 100 {
    if let nativeArray : [String] = conditionalCast(nsArray) {
      nativeString = nativeArray[0]
    }
  }
  check(nativeString != nil && nativeString == "NSString that does not fit in tagged pointer")
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSArrayAnyObjectToString(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSArrayAnyObjectToString()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeFromNSArrayAnyObjectToStringForced() {
  let nsArray = createNSArray()

  var nativeString : String?
  for _ in 0 ..< 50 {
    let nativeArray : [String] = forcedCast(nsArray)
    nativeString = nativeArray[0]
  }
  check(nativeString != nil && nativeString == "NSString that does not fit in tagged pointer")
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSArrayAnyObjectToStringForced(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSArrayAnyObjectToStringForced()
    }
  }
#endif
}

// === Dictionary === //

#if _runtime(_ObjC)
func createNSDictionary() -> NSDictionary {
  let nsMutableDictionary = NSMutableDictionary()
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!
  let nsString2 = NSString(cString: "NSString that does not fit in tagged pointer 2", encoding: String.Encoding.utf8.rawValue)!
  let nsString3 = NSString(cString: "NSString that does not fit in tagged pointer 3", encoding: String.Encoding.utf8.rawValue)!
  let nsString4 = NSString(cString: "NSString that does not fit in tagged pointer 4", encoding: String.Encoding.utf8.rawValue)!
  let nsString5 = NSString(cString: "NSString that does not fit in tagged pointer 5", encoding: String.Encoding.utf8.rawValue)!
  let nsString6 = NSString(cString: "NSString that does not fit in tagged pointer 6", encoding: String.Encoding.utf8.rawValue)!
  let nsString7 = NSString(cString: "NSString that does not fit in tagged pointer 7", encoding: String.Encoding.utf8.rawValue)!
  let nsString8 = NSString(cString: "NSString that does not fit in tagged pointer 8", encoding: String.Encoding.utf8.rawValue)!
  let nsString9 = NSString(cString: "NSString that does not fit in tagged pointer 9", encoding: String.Encoding.utf8.rawValue)!
  let nsString10 = NSString(cString: "NSString that does not fit in tagged pointer 10", encoding: String.Encoding.utf8.rawValue)!
  nsMutableDictionary.setObject(1, forKey: nsString)
  nsMutableDictionary.setObject(2, forKey: nsString2)
  nsMutableDictionary.setObject(3, forKey: nsString3)
  nsMutableDictionary.setObject(4, forKey: nsString4)
  nsMutableDictionary.setObject(5, forKey: nsString5)
  nsMutableDictionary.setObject(6, forKey: nsString6)
  nsMutableDictionary.setObject(7, forKey: nsString7)
  nsMutableDictionary.setObject(8, forKey: nsString8)
  nsMutableDictionary.setObject(9, forKey: nsString9)
  nsMutableDictionary.setObject(10, forKey: nsString10)

  return nsMutableDictionary.copy() as! NSDictionary
}

@inline(never)
func testObjectiveCBridgeFromNSDictionaryAnyObject() {
  let nsDictionary = createNSDictionary()
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!

  var nativeInt : Int?
  for _ in 0 ..< 100 {
    if let nativeDictionary : [NSString : NSNumber] = conditionalCast(nsDictionary) {
       nativeInt = forcedCast(nativeDictionary[nsString])
    }
  }
  check(nativeInt != nil && nativeInt == 1)
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSDictionaryAnyObject(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSDictionaryAnyObject()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeFromNSDictionaryAnyObjectForced() {
  let nsDictionary = createNSDictionary()
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!

  var nativeInt : Int?
  for _ in 0 ..< 200 {
    if let nativeDictionary : [NSString : NSNumber] = forcedCast(nsDictionary) {
       nativeInt = forcedCast(nativeDictionary[nsString])
    }
  }
  check(nativeInt != nil && nativeInt == 1)
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSDictionaryAnyObjectForced(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSDictionaryAnyObjectForced()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeToNSDictionary() {
  let nativeDictionary = ["abcde1": 1, "abcde2": 2, "abcde3": 3, "abcde4": 4,
    "abcde5": 5, "abcde6": 6, "abcde7": 7, "abcde8": 8, "abcde9": 9,
    "abcde10": 10]
  let key = "abcde1" as NSString

  var nsNumber : Any?
  for _ in 0 ..< 200 {
    let nsDict = nativeDictionary as NSDictionary
    nsNumber = nsDict.object(forKey: key)
  }
  check(nsNumber != nil && (nsNumber as! Int) == 1)
}
#endif

@inline(never)
public func run_ObjectiveCBridgeToNSDictionary(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeToNSDictionary()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeFromNSDictionaryAnyObjectToString() {
  let nsDictionary = createNSDictionary()
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!
  let nativeString = nsString as String

  var nativeInt : Int?
  for _ in 0 ..< 20 {
    if let nativeDictionary : [String : Int] = conditionalCast(nsDictionary) {
       nativeInt = nativeDictionary[nativeString]
    }
  }
  check(nativeInt != nil && nativeInt == 1)
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSDictionaryAnyObjectToString(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSDictionaryAnyObjectToString()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeFromNSDictionaryAnyObjectToStringForced() {
  let nsDictionary = createNSDictionary()
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!
  let nativeString = nsString as String

  var nativeInt : Int?
  for _ in 0 ..< 20 {
    if let nativeDictionary : [String : Int] = forcedCast(nsDictionary) {
       nativeInt = nativeDictionary[nativeString]
    }
  }
  check(nativeInt != nil && nativeInt == 1)
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSDictionaryAnyObjectToStringForced(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSDictionaryAnyObjectToStringForced()
    }
  }
#endif
}

// === Set === //

#if _runtime(_ObjC)
func createNSSet() -> NSSet {
  let nsMutableSet = NSMutableSet()
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!
  let nsString2 = NSString(cString: "NSString that does not fit in tagged pointer 2", encoding: String.Encoding.utf8.rawValue)!
  let nsString3 = NSString(cString: "NSString that does not fit in tagged pointer 3", encoding: String.Encoding.utf8.rawValue)!
  let nsString4 = NSString(cString: "NSString that does not fit in tagged pointer 4", encoding: String.Encoding.utf8.rawValue)!
  let nsString5 = NSString(cString: "NSString that does not fit in tagged pointer 5", encoding: String.Encoding.utf8.rawValue)!
  let nsString6 = NSString(cString: "NSString that does not fit in tagged pointer 6", encoding: String.Encoding.utf8.rawValue)!
  let nsString7 = NSString(cString: "NSString that does not fit in tagged pointer 7", encoding: String.Encoding.utf8.rawValue)!
  let nsString8 = NSString(cString: "NSString that does not fit in tagged pointer 8", encoding: String.Encoding.utf8.rawValue)!
  let nsString9 = NSString(cString: "NSString that does not fit in tagged pointer 9", encoding: String.Encoding.utf8.rawValue)!
  let nsString10 = NSString(cString: "NSString that does not fit in tagged pointer 10", encoding: String.Encoding.utf8.rawValue)!
  nsMutableSet.add(nsString)
  nsMutableSet.add(nsString2)
  nsMutableSet.add(nsString3)
  nsMutableSet.add(nsString4)
  nsMutableSet.add(nsString5)
  nsMutableSet.add(nsString6)
  nsMutableSet.add(nsString7)
  nsMutableSet.add(nsString8)
  nsMutableSet.add(nsString9)
  nsMutableSet.add(nsString10)

  return nsMutableSet.copy() as! NSSet
}


@inline(never)
func testObjectiveCBridgeFromNSSetAnyObject() {
  let nsSet = createNSSet()
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!

  var result : Bool?
  for _ in 0 ..< 50 {
    if let nativeSet : Set<NSString> = conditionalCast(nsSet) {
       result = nativeSet.contains(nsString)
    }
  }
  check(result != nil && result!)
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSSetAnyObject(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSSetAnyObject()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeFromNSSetAnyObjectForced() {
  let nsSet = createNSSet()
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!

  var result : Bool?
  for _ in 0 ..< 500 {
    if let nativeSet : Set<NSString> = forcedCast(nsSet) {
       result = nativeSet.contains(nsString)
    }
  }
  check(result != nil && result!)
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSSetAnyObjectForced(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSSetAnyObjectForced()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeToNSSet() {
  let nativeSet = Set<String>(["abcde1", "abcde2", "abcde3", "abcde4", "abcde5",
      "abcde6", "abcde7", "abcde8", "abcde9", "abcde10"])
  let key = "abcde1" as NSString

  var nsString : Any?
  for _ in 0 ..< 200 {
    let nsDict = nativeSet as NSSet
    nsString = nsDict.member(key)
  }
  check(nsString != nil && (nsString as! String) == "abcde1")
}
#endif

@inline(never)
public func run_ObjectiveCBridgeToNSSet(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
      testObjectiveCBridgeToNSSet()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeFromNSSetAnyObjectToString() {
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!
  let nativeString = nsString as String
  let nsSet = createNSSet()

  var result : Bool?
  for _ in 0 ..< 20 {
    if let nativeSet : Set<String> = conditionalCast(nsSet) {
       result = nativeSet.contains(nativeString)
    }
  }
  check(result != nil && result!)
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSSetAnyObjectToString(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSSetAnyObjectToString()
    }
  }
#endif
}

#if _runtime(_ObjC)
@inline(never)
func testObjectiveCBridgeFromNSSetAnyObjectToStringForced() {
  let nsSet = createNSSet()
  let nsString = NSString(cString: "NSString that does not fit in tagged pointer", encoding: String.Encoding.utf8.rawValue)!
  let nativeString = nsString as String

  var result : Bool?
  for _ in 0 ..< 20 {
    if let nativeSet : Set<String> = forcedCast(nsSet) {
       result = nativeSet.contains(nativeString)
    }
  }
  check(result != nil && result!)
}
#endif

@inline(never)
public func run_ObjectiveCBridgeFromNSSetAnyObjectToStringForced(_ n: Int) {
#if _runtime(_ObjC)
  for _ in 0 ..< n {
    autoreleasepool {
    testObjectiveCBridgeFromNSSetAnyObjectToStringForced()
    }
  }
#endif
}

#if _runtime(_ObjC)

//translated directly from an objc part of the original testcase
@objc class DictionaryContainer : NSObject {
  @objc var _dictionary:NSDictionary

  //simulate an objc property being imported via bridging
  @objc var dictionary:Dictionary<DateComponents, String> {
    @inline(never)
    get {
      return _dictionary as! Dictionary<DateComponents, String>
    }
  }

  override init() {
    _dictionary = NSDictionary()
    super.init()
    let dict = NSMutableDictionary()
    let calendar = NSCalendar(calendarIdentifier: .gregorian)!
    let now = Date()
    for day in 0..<36 {
      let date = calendar.date(byAdding: .day, value: -day, to: now, options: NSCalendar.Options())
      let components = calendar.components([.year, .month, .day], from: date!)
      dict[components as NSDateComponents] = "TESTING"
    }
    _dictionary = NSDictionary(dictionary: dict)
  }
}

var componentsContainer: DictionaryContainer? = nil
var componentsArray:[DateComponents]? = nil
#endif

public func setup_dateComponents() {
  #if _runtime(_ObjC)
  componentsContainer = DictionaryContainer()
  let now = Date()
  let calendar = Calendar(identifier: .gregorian)
  componentsArray = (0..<10).compactMap { (day) -> DateComponents? in
    guard let date = calendar.date(byAdding: .day, value: -day, to: now) else {
      return nil
    }

    return calendar.dateComponents([.year, .month, .day], from: date)
  }
  #endif
}

@inline(never)
public func run_ObjectiveCBridgeFromNSDateComponents(_ n: Int) {
  #if _runtime(_ObjC)
  for _ in 0 ..< n {
    for components in componentsArray! {
      let _ = componentsContainer!.dictionary[components]
    }
  }
  #endif
}

var asciiStringFromFile: String? = nil
public func setup_ASCIIStringFromFile() {
  #if _runtime(_ObjC)
  let url:URL
  if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
    url = FileManager.default.temporaryDirectory.appendingPathComponent(
      "sphinx.txt"
    )
  } else {
    url = URL(fileURLWithPath: "/tmp/sphinx.txt")
  }
  var str = "Sphinx of black quartz judge my vow"
  str = Array(repeating: str, count: 100).joined()
  try? str.write(
    to: url,
    atomically: true,
    encoding: .ascii
  )
  asciiStringFromFile = try! String(contentsOf: url, encoding: .ascii)
  #endif
}

@inline(never)
public func run_ASCIIStringFromFile(_ n: Int) {
  #if _runtime(_ObjC)
  for _ in 0 ..< n {
    blackHole((asciiStringFromFile! + "").utf8.count)
  }
  #endif
}

var unicodeStringFromCodable:String? = nil
var unicodeStringFromCodableDict = [String:Void]()
public func setup_UnicodeStringFromCodable() {
  do {
    let jsonString = "[\(String(reflecting: "Nice string which works rather slöw."))]"
    
    let decoded = try JSONDecoder().decode([String].self, from: Data(jsonString.utf8))
    let reEncoded = try JSONEncoder().encode(decoded)
    let desc = try JSONDecoder().decode([String].self, from: reEncoded)
    
    unicodeStringFromCodable = desc[0]
  } catch (_) {
    
  }
}

@inline(never)
public func run_UnicodeStringFromCodable(_ n: Int) {
  #if _runtime(_ObjC)
  for _ in 0 ..< n {
    for _ in 0..<100 {
      unicodeStringFromCodableDict[identity(unicodeStringFromCodable!)] = ()
    }
  }
  #endif
}

#if _runtime(_ObjC)
var bridgedArray:NSArray! = nil
var bridgedArrayMutableCopy:NSMutableArray! = nil
var nsArray:NSArray! = nil
var nsArrayMutableCopy:NSMutableArray! = nil
#endif

public func setup_bridgedArrays() {
  #if _runtime(_ObjC)
  var arr = Array(repeating: NSObject(), count: 100) as [AnyObject]
  bridgedArray = arr as NSArray
  bridgedArrayMutableCopy = (bridgedArray.mutableCopy() as! NSMutableArray)
  nsArray = NSArray(objects: &arr, count: 100)
  nsArrayMutableCopy = (nsArray.mutableCopy() as! NSMutableArray)
  #endif
}

@inline(never)
public func run_BridgedNSArrayObjectAtIndex(_ n: Int) {
  #if _runtime(_ObjC)
  for _ in 0 ..< n * 50 {
    for i in 0..<100 {
      blackHole(bridgedArray[i])
    }
  }
  #endif
}

@inline(never)
public func run_BridgedNSArrayMutableCopyObjectAtIndex(_ n: Int) {
  #if _runtime(_ObjC)
  for _ in 0 ..< n * 100 {
    for i in 0..<100 {
      blackHole(bridgedArrayMutableCopy[i])
    }
  }
  #endif
}

@inline(never)
public func run_RealNSArrayObjectAtIndex(_ n: Int) {
  #if _runtime(_ObjC)
  for _ in 0 ..< n * 100 {
    for i in 0..<100 {
      blackHole(nsArray[i])
    }
  }
  #endif
}

@inline(never)
public func run_RealNSArrayMutableCopyObjectAtIndex(_ n: Int) {
  #if _runtime(_ObjC)
  for _ in 0 ..< n * 100 {
    for i in 0..<100 {
      blackHole(nsArrayMutableCopy[i])
    }
  }
  #endif
}