Skip to content

Commit 7f8f6bd

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 f40ef5f commit 7f8f6bd

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

Sources/swift-format/Subcommands/Format.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ extension SwiftFormatCommand {
5858
) { sourceFile, path, configuration in
5959
formatMain(
6060
configuration: configuration, sourceFile: sourceFile, assumingFilename: path,
61-
inPlace: inPlace, ignoreUnparsableFiles: formatOptions.ignoreUnparsableFiles,
62-
debugOptions: formatOptions.debugOptions, diagnosticEngine: diagnosticEngine)
61+
inPlace: self.inPlace, ignoreUnparsableFiles: self.formatOptions.ignoreUnparsableFiles,
62+
debugOptions: self.formatOptions.debugOptions, diagnosticEngine: diagnosticEngine)
6363
}
6464
}
6565

Sources/swift-format/Subcommands/Lint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension SwiftFormatCommand {
4343
) { sourceFile, path, configuration in
4444
lintMain(
4545
configuration: configuration, sourceFile: sourceFile, assumingFilename: path,
46-
debugOptions: lintOptions.debugOptions, diagnosticEngine: diagnosticEngine)
46+
debugOptions: self.lintOptions.debugOptions, diagnosticEngine: diagnosticEngine)
4747
}
4848
}
4949

Sources/swift-format/Utilities/Helpers.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,33 @@ func readSource(from fileHandle: FileHandle) -> String? {
4242
func processSources(
4343
from paths: [String], configurationPath: String?,
4444
diagnosticEngine: DiagnosticEngine,
45-
transform: (FileHandle, String, Configuration) -> Void
45+
transform: @escaping (FileHandle, String, Configuration) -> Void
4646
) throws {
47+
let concurrentQueue = DispatchQueue(label: "com.queue.Concurrent", qos: .userInitiated, attributes: .concurrent)
48+
let group = DispatchGroup()
49+
let lock = NSLock()
50+
var lastError: Error?
4751
for path in FileIterator(paths: paths) {
48-
guard let sourceFile = FileHandle(forReadingAtPath: path) else {
49-
diagnosticEngine.diagnose(
50-
Diagnostic.Message(.error, "Unable to create a file handle for source from \(path)."))
51-
return
52+
concurrentQueue.async(group: group) {
53+
guard let sourceFile = FileHandle(forReadingAtPath: path) else {
54+
diagnosticEngine.diagnose(
55+
Diagnostic.Message(.error, "Unable to create a file handle for source from \(path)."))
56+
return
57+
}
58+
do {
59+
let configuration = try loadConfiguration(forSwiftFile: path, configFilePath: configurationPath)
60+
transform(sourceFile, path, configuration)
61+
} catch let error {
62+
lock.lock()
63+
lastError = error
64+
lock.unlock()
65+
}
5266
}
53-
let configuration = try loadConfiguration(forSwiftFile: path, configFilePath: configurationPath)
54-
transform(sourceFile, path, configuration)
67+
}
68+
69+
group.wait()
70+
if let error = lastError {
71+
throw error
5572
}
5673
}
5774

0 commit comments

Comments
 (0)