Skip to content
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

Optimisation of the parallelCompactMap implementation #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 3 additions & 69 deletions Sources/Carpaccio/Collection+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Swift.Collection where Index == Int {
return []
}

var result: [(Int, T?)] = []
var result: [T?] = Array(repeating: nil, count: self.count)

let group = DispatchGroup()
let lock = DispatchQueue(label: "pcompactmap")
Expand All @@ -33,7 +33,7 @@ extension Swift.Collection where Index == Int {
do {
let t = try transform(self[i])
lock.async(group: group) {
result += [(i, t)]
result[i] = t
}
}
catch {
Expand All @@ -47,72 +47,6 @@ extension Swift.Collection where Index == Int {
throw error
}

return result.sorted { $0.0 < $1.0 }.compactMap { $0.1 }
return result.compactMap { $0 }
}
}

// Commented out, for now, due to unreliable operation: some elements might go missing from the result.
/*
// Inspired by http://moreindirection.blogspot.co.uk/2015/07/gcd-and-parallel-collections-in-swift.html
extension Swift.Sequence {
public func parallelMap<T>(maxParallelism:Int? = nil, _
transform: @escaping ((Iterator.Element) throws -> T)) throws -> [T]
{
return try self.parallelCompactMap(maxParallelism: maxParallelism, transform)
}

public func parallelCompactMap<T>(maxParallelism:Int? = nil, _ transform: @escaping ((Iterator.Element) throws -> T?)) throws -> [T]
{
if let maxParallelism = maxParallelism, maxParallelism == 1 {
return try self.compactMap(transform)
}

var result: [(Int64, T)] = []
let group = DispatchGroup()
let lock = DispatchQueue(label: "pcompactmap")

let parallelism:Int = {
if let maxParallelism = maxParallelism {
precondition(maxParallelism > 0)
return maxParallelism
}
return ProcessInfo.processInfo.activeProcessorCount
}()

let semaphore = DispatchSemaphore(value: parallelism)
var iterator = self.makeIterator()
var index:Int64 = 0
var caughtError: Swift.Error? = nil

repeat {
guard let item = iterator.next() else {
break
}
semaphore.wait()
DispatchQueue.global().async { [index] in
do {
if let mappedElement = try transform(item) {
lock.async {
result += [(index, mappedElement)]
}
}
}
catch {
caughtError = error
}
semaphore.signal()
}
index += 1
} while true

group.wait()

if let error = caughtError {
throw error
}

return result.sorted { $0.0 < $1.0 }
.compactMap { $0.1 }
}
}
*/