Files
SwiftLint/Source/SwiftLintFramework/Extensions/Configuration+Cache.swift
T
JP Simard d768897f1d Improve performance of collecting files to lint and lint cache lookups (#2465)
Performance has gotten pretty bad for complex SwiftLint configurations like the one used for Lyft's iOS code base involving lots of files in the directories being linted, large configuration files and many nested configuration files.

Two main areas were particularly ripe for improvement were:

1. Collecting which files to lint
2. Lint cache lookups

### Collecting which files to lint

Improve this by:

* using an NSOrderedSet to remove excluded paths instead of `Array.filter`
* parallelizing calls to `filesToLint` for all paths to lint and exclude
* using `FileManager.subpaths(atPath:)` instead of `enumerator(atPath:)`

|Change|Before|After|Speed up|
|-|-|-|-|
|NSOrderedSet|2.438s|0.917s|2.659x|
|Parallel Flat Map|2.438s|2.248s|1.085x|
|Subpaths|0.939s|0.867s|1.083x|
|**Total**|**2.438s**|**0.720s**|**3.386x**|

### Lint cache lookups

By using an MD5 hash of the Configuration description from CryptoSwift as the cache key instead of instead the full description, we can drastically speed up cache lookups for projects with complex SwiftLint configurations. I think the dictionary lookup for very large string keys doesn't perform very well.

---

* Speed up Configuration.lintablePaths

* Improve cache lookup performance by up to 10x

By using an MD5 hash of the Configuration description from CryptoSwift
as the cache key instead of instead the full description.

* Add changelog entries

* Swift 4.0 & Linux compatibility

* os(Darwin) isn't a thing

* Allow warnings in pod lib lint

SwiftLint supports building with Swift 4.0 to 4.2.

There is no version of CryptoSwift to support both Swift 4.0 and
Swift 4.2.

So allow warnings for now. We'll make one more Swift 4.0 compatible
release, then we'll bump the build requirements to Swift 4.2 and
remove the `--allow-warnings` flag.
2018-11-18 14:39:02 -08:00

76 lines
2.6 KiB
Swift

import CryptoSwift
import Foundation
extension Configuration {
// MARK: Caching Configurations By Path (In-Memory)
private static var cachedConfigurationsByPath = [String: Configuration]()
private static var cachedConfigurationsByPathLock = NSLock()
internal func setCached(atPath path: String) {
Configuration.cachedConfigurationsByPathLock.lock()
Configuration.cachedConfigurationsByPath[path] = self
Configuration.cachedConfigurationsByPathLock.unlock()
}
internal static func getCached(atPath path: String) -> Configuration? {
cachedConfigurationsByPathLock.lock()
defer { cachedConfigurationsByPathLock.unlock() }
return cachedConfigurationsByPath[path]
}
public func withPrecomputedCacheDescription() -> Configuration {
var result = self
result.computedCacheDescription = result.cacheDescription
return result
}
// MARK: SwiftLint Cache (On-Disk)
internal var cacheDescription: String {
if let computedCacheDescription = computedCacheDescription {
return computedCacheDescription
}
let cacheRulesDescriptions = rules
.map { rule in
return [type(of: rule).description.identifier, rule.cacheDescription]
}
.sorted { rule1, rule2 in
return rule1[0] < rule2[0]
}
let jsonObject: [Any] = [
rootPath ?? FileManager.default.currentDirectoryPath,
cacheRulesDescriptions
]
if let jsonData = try? JSONSerialization.data(withJSONObject: jsonObject),
let jsonString = String(data: jsonData, encoding: .utf8) {
return jsonString.md5()
}
queuedFatalError("Could not serialize configuration for cache")
}
internal var cacheURL: URL {
let baseURL: URL
if let path = cachePath {
baseURL = URL(fileURLWithPath: path)
} else {
#if os(Linux)
baseURL = URL(fileURLWithPath: "/var/tmp/")
#else
baseURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
#endif
}
let folder = baseURL.appendingPathComponent("SwiftLint/\(Version.current.value)")
do {
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
} catch {
queuedPrintError("Error while creating cache: " + error.localizedDescription)
}
return folder.appendingPathComponent("cache.json")
}
}