Skip to content

Commit dd90dde

Browse files
committed
Multithread processing source files
Previously every source file was formatted / linted one by one. On our codebase this took a full project format from ~17 minutes to ~5 minutes.
1 parent 684a3cd commit dd90dde

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

Sources/swift-format/main.swift

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,36 @@ fileprivate func main(_ arguments: [String]) -> Int32 {
6969
/// - transform: A closure that performs a transformation on a specific source file.
7070
private func processSources(
7171
from paths: [String], configurationPath: String?,
72-
transform: (FileHandle, String, Configuration) -> Int
72+
transform: @escaping (FileHandle, String, Configuration) -> Int
7373
) -> Int32 {
7474
var result = 0
75+
let concurrentQueue = DispatchQueue(label: "com.queue.Concurrent", qos: .userInitiated, attributes: .concurrent)
76+
let group = DispatchGroup()
77+
let lock = NSLock()
7578
for path in FileIterator(paths: paths) {
76-
guard let sourceFile = FileHandle(forReadingAtPath: path) else {
77-
stderrStream.write("Unable to create a file handle for source from \(path).\n")
78-
stderrStream.flush()
79-
return 1
79+
group.enter()
80+
concurrentQueue.async {
81+
defer { group.leave() }
82+
guard let sourceFile = FileHandle(forReadingAtPath: path) else {
83+
stderrStream.write("Unable to create a file handle for source from \(path).\n")
84+
stderrStream.flush()
85+
lock.lock()
86+
result |= 1
87+
lock.unlock()
88+
return
89+
}
90+
91+
let configuration = loadConfiguration(forSwiftFile: path, configFilePath: configurationPath)
92+
let transformResult = transform(sourceFile, path, configuration)
93+
if transformResult > 0 {
94+
lock.lock()
95+
result |= transformResult
96+
lock.unlock()
97+
}
8098
}
81-
let configuration = loadConfiguration(forSwiftFile: path, configFilePath: configurationPath)
82-
result |= transform(sourceFile, path, configuration)
8399
}
100+
101+
group.wait()
84102
return Int32(result)
85103
}
86104

0 commit comments

Comments
 (0)