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

Make asyncMap to prevent from race-condition #1492

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
18 changes: 8 additions & 10 deletions Sources/ProjectSpec/Decoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ extension Dictionary where Key: JSONKey {
let defaultError = NSError(domain: "Unspecified error", code: 0, userInfo: nil)
let keys = Array(dictionary.keys)
var itemResults: [Result<T, Error>] = Array(repeating: .failure(defaultError), count: keys.count)
itemResults.withUnsafeMutableBufferPointer { buffer in
DispatchQueue.concurrentPerform(iterations: dictionary.count) { idx in
do {
let key = keys[idx]
let jsonDictionary: JSONDictionary = try dictionary.json(atKeyPath: .key(key))
let item = try T(name: key, jsonDictionary: jsonDictionary)
buffer[idx] = .success(item)
} catch {
buffer[idx] = .failure(error)
}
DispatchQueue.concurrentPerform(iterations: dictionary.count) { idx in
do {
let key = keys[idx]
let jsonDictionary: JSONDictionary = try dictionary.json(atKeyPath: .key(key))
let item = try T(name: key, jsonDictionary: jsonDictionary)
itemResults[idx] = .success(item)
} catch {
itemResults[idx] = .failure(error)
}
}
return try itemResults.map { try $0.get() }
Expand Down
12 changes: 5 additions & 7 deletions Sources/XcodeGenCore/ArrayExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import Foundation

public extension Array {

func parallelMap<T>(transform: (Element) -> T) -> [T] {
var result = ContiguousArray<T?>(repeating: nil, count: count)
return result.withUnsafeMutableBufferPointer { buffer in
DispatchQueue.concurrentPerform(iterations: buffer.count) { idx in
buffer[idx] = transform(self[idx])
}
return buffer.map { $0! }
func parallelMap<T>(transform: @Sendable (Element) -> T) -> [T] {
var buffer: [T] = []
DispatchQueue.concurrentPerform(iterations: count) { idx in
buffer[idx] = transform(self[idx])
}
return buffer
}
}

Expand Down
7 changes: 4 additions & 3 deletions Sources/XcodeGenCore/Glob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ public class Glob: Collection {
}

let patterns = behavior.supportsGlobstar ? expandGlobstar(pattern: adjustedPattern) : [adjustedPattern]


let isIncludingFiles = includeFiles
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This closure made @Sendable closure, so it's better to avoid passing mutable variables

#if os(macOS)
paths = patterns.parallelMap { paths(usingPattern: $0, includeFiles: includeFiles) }.flatMap { $0 }
paths = patterns.parallelMap { paths(usingPattern: $0, includeFiles: isIncludingFiles) }.flatMap { $0 }
#else
// Parallel invocations of Glob on Linux seems to be causing unexpected crashes
paths = patterns.map { paths(usingPattern: $0, includeFiles: includeFiles) }.flatMap { $0 }
paths = patterns.map { paths(usingPattern: $0, includeFiles: isIncludingFiles) }.flatMap { $0 }
#endif

paths = Array(Set(paths)).sorted { lhs, rhs in
Expand Down
Loading