Skip to content

Commit c8940cc

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 d2e3fa2 commit c8940cc

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

Sources/swift-format/main.swift

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,34 @@ fileprivate func main(_ arguments: [String]) -> Int32 {
8080
/// - transform: A closure that performs a transformation on a specific source file.
8181
fileprivate func processSources(
8282
from paths: [String], configurationPath: String?, diagnosticEngine: DiagnosticEngine,
83-
transform: (FileHandle, String, Configuration) -> Int
83+
transform: @escaping (FileHandle, String, Configuration) -> Int
8484
) -> Int32 {
8585
var result = 0
86+
let concurrentQueue = DispatchQueue(label: "com.queue.Concurrent", qos: .userInitiated, attributes: .concurrent)
87+
let group = DispatchGroup()
88+
let lock = NSLock()
8689
for path in FileIterator(paths: paths) {
87-
guard let sourceFile = FileHandle(forReadingAtPath: path) else {
88-
diagnosticEngine.diagnose(
89-
Diagnostic.Message(.error, "Unable to create a file handle for source from \(path)."))
90-
return 1
90+
concurrentQueue.async(group: group) {
91+
guard let sourceFile = FileHandle(forReadingAtPath: path) else {
92+
diagnosticEngine.diagnose(
93+
Diagnostic.Message(.error, "Unable to create a file handle for source from \(path)."))
94+
lock.lock()
95+
result |= 1
96+
lock.unlock()
97+
return
98+
}
99+
100+
let configuration = loadConfiguration(forSwiftFile: path, configFilePath: configurationPath)
101+
let transformResult = transform(sourceFile, path, configuration)
102+
if transformResult > 0 {
103+
lock.lock()
104+
result |= transformResult
105+
lock.unlock()
106+
}
91107
}
92-
let configuration = loadConfiguration(forSwiftFile: path, configFilePath: configurationPath)
93-
result |= transform(sourceFile, path, configuration)
94108
}
109+
110+
group.wait()
95111
return Int32(result)
96112
}
97113

0 commit comments

Comments
 (0)