Skip to content

Commit da2f551

Browse files
author
Ignacio Bonafonte
authored
Improve URLSessionInstrumentation loading time by injecting the methods concurrently (#325)
Improve URLSessionInstrumentation loading time by injecting the methods concurrently. As this method is usually going to happen at app start it reduces the time without hurting other processes running
1 parent 4855809 commit da2f551

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,19 @@ public class URLSessionInstrumentation {
6666
]
6767
#endif
6868
let classes = InstrumentationUtils.objc_getClassList()
69-
classes.forEach {
70-
guard $0 != Self.self else { return }
69+
let selectorsCount = selectors.count
70+
DispatchQueue.concurrentPerform(iterations: classes.count) { iteration in
71+
let theClass: AnyClass = classes[iteration]
72+
guard theClass != Self.self else { return }
7173
var selectorFound = false
7274
var methodCount: UInt32 = 0
73-
guard let methodList = class_copyMethodList($0, &methodCount) else { return }
75+
guard let methodList = class_copyMethodList(theClass, &methodCount) else { return }
7476

75-
for i in 0..<Int(methodCount) {
76-
for j in 0..<selectors.count {
77+
for j in 0..<selectorsCount {
78+
for i in 0..<Int(methodCount) {
7779
if method_getName(methodList[i]) == selectors[j] {
7880
selectorFound = true
79-
injectIntoDelegateClass(cls: $0)
81+
injectIntoDelegateClass(cls: theClass)
8082
break
8183
}
8284
}
@@ -85,6 +87,7 @@ public class URLSessionInstrumentation {
8587
}
8688
}
8789
}
90+
8891
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
8992
injectIntoNSURLSessionCreateTaskMethods()
9093
}

0 commit comments

Comments
 (0)