Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit ac7e049

Browse files
authored
Merge pull request #80 from kean/improve-parallelMap
Improve parallelMap
2 parents c757fff + bc6a51a commit ac7e049

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

Sources/SwiftDoc/Extensions/Array+Parallel.swift

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,23 @@ public extension RandomAccessCollection {
77
}
88

99
let indices = Array(self.indices)
10-
11-
var results = [(index: Index, result: Result<T, Error>)]()
12-
results.reserveCapacity(count)
10+
var results = [Result<T, Error>?](repeating: nil, count: count)
1311

1412
let queue = DispatchQueue(label: #function)
15-
withoutActuallyEscaping(transform) { escapingtransform in
16-
DispatchQueue.concurrentPerform(iterations: count) { (iteration) in
17-
let index = indices[iteration]
18-
19-
do {
20-
let transformed = try escapingtransform(self[index])
21-
queue.sync {
22-
results.append((index, .success(transformed)))
23-
}
24-
} catch {
25-
queue.sync {
26-
results.append((index, .failure(error)))
27-
}
13+
DispatchQueue.concurrentPerform(iterations: count) { (iteration) in
14+
do {
15+
let transformed = try transform(self[indices[iteration]])
16+
queue.sync {
17+
results[iteration] = .success(transformed)
18+
}
19+
} catch {
20+
queue.sync {
21+
results[iteration] = .failure(error)
2822
}
2923
}
3024
}
3125

32-
return try results.sorted { $0.index < $1.index }
33-
.map { try $0.result.get() }
26+
return try results.map { try $0!.get() }
3427
}
3528

3629
func parallelCompactMap<T>(transform: (Element) throws -> T?) throws -> [T] {

0 commit comments

Comments
 (0)