Skip to content

Commit 2e194e6

Browse files
committed
Add a benchmark for bridging date components
1 parent 92416e7 commit 2e194e6

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

benchmark/single-source/ObjectiveCBridging.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public let ObjectiveCBridging = [
3232
BenchmarkInfo(name: "ObjectiveCBridgeToNSSet", runFunction: run_ObjectiveCBridgeToNSSet, tags: [.validation, .bridging]),
3333
BenchmarkInfo(name: "ObjectiveCBridgeFromNSSetAnyObjectToString", runFunction: run_ObjectiveCBridgeFromNSSetAnyObjectToString, tags: [.validation, .bridging, .String]),
3434
BenchmarkInfo(name: "ObjectiveCBridgeFromNSSetAnyObjectToStringForced", runFunction: run_ObjectiveCBridgeFromNSSetAnyObjectToStringForced, tags: [.validation, .bridging, .String, .unstable]),
35+
BenchmarkInfo(name: "ObjectiveCBridgeFromNSDateComponents", runFunction: run_ObjectiveCBridgeFromNSDateComponents, tags: [.validation, .bridging], setUpFunction: setup_dateComponents)
3536
]
3637

3738
#if _runtime(_ObjC)
@@ -609,3 +610,62 @@ public func run_ObjectiveCBridgeFromNSSetAnyObjectToStringForced(_ N: Int) {
609610
}
610611
#endif
611612
}
613+
614+
#if _runtime(_ObjC)
615+
616+
//translated directly from an objc part of the original testcase
617+
@objc class DictionaryContainer : NSObject {
618+
@objc var _dictionary:NSDictionary
619+
620+
//simulate an objc property being imported via bridging
621+
@objc var dictionary:Dictionary<DateComponents, String> {
622+
@inline(never)
623+
get {
624+
return _dictionary as! Dictionary<DateComponents, String>
625+
}
626+
}
627+
628+
override init() {
629+
_dictionary = NSDictionary()
630+
super.init()
631+
let dict = NSMutableDictionary()
632+
let calendar = NSCalendar(calendarIdentifier: .gregorian)!
633+
let now = Date()
634+
for day in 0..<36 {
635+
let date = calendar.date(byAdding: .day, value: -day, to: now, options: NSCalendar.Options())
636+
let components = calendar.components([.year, .month, .day], from: date!)
637+
dict[components as NSDateComponents] = "TESTING"
638+
}
639+
_dictionary = NSDictionary(dictionary: dict)
640+
}
641+
}
642+
643+
var componentsContainer: DictionaryContainer? = nil
644+
var componentsArray:[DateComponents]? = nil
645+
#endif
646+
647+
public func setup_dateComponents() {
648+
#if _runtime(_ObjC)
649+
componentsContainer = DictionaryContainer()
650+
let now = Date()
651+
let calendar = Calendar(identifier: .gregorian)
652+
componentsArray = (0..<10).compactMap { (day) -> DateComponents? in
653+
guard let date = calendar.date(byAdding: .day, value: -day, to: now) else {
654+
return nil
655+
}
656+
657+
return calendar.dateComponents([.year, .month, .day], from: date)
658+
}
659+
#endif
660+
}
661+
662+
@inline(never)
663+
public func run_ObjectiveCBridgeFromNSDateComponents(_ N: Int) {
664+
#if _runtime(_ObjC)
665+
for _ in 0 ..< N {
666+
for components in componentsArray! {
667+
let _ = componentsContainer!.dictionary[components]
668+
}
669+
}
670+
#endif
671+
}

0 commit comments

Comments
 (0)