Reduce memory usage (#4349)

When linting SwiftLint, this brings memory usage down by around 20%,
going from 372MB to 297MB.

This is achieved by removing the unused `RebuildQueue` and by clearing
cached data associated with files after processing them.

Depends on https://github.com/jpsim/SourceKitten/pull/749.
This commit is contained in:
JP Simard
2022-10-12 09:39:36 -04:00
committed by GitHub
parent 87e9757af2
commit 602070164b
5 changed files with 11 additions and 27 deletions
@@ -27,7 +27,6 @@ private var responseCache = Cache({ file -> [String: SourceKitRepresentable]? in
})
private var structureCache = Cache({ file -> Structure? in
if let structure = responseCache.get(file).map(Structure.init) {
queueForRebuild.append(structure)
return structure
}
return nil
@@ -68,25 +67,6 @@ internal var parserDiagnosticsDisabledForTests = false
private var assertHandlers = [FileCacheKey: AssertHandler]()
private var assertHandlerCache = Cache({ file in assertHandlers[file.cacheKey] })
private struct RebuildQueue {
private let lock = PlatformLock()
private var queue = [Structure]()
mutating func append(_ structure: Structure) {
lock.doLocked {
queue.append(structure)
}
}
mutating func clear() {
lock.doLocked {
queue.removeAll(keepingCapacity: false)
}
}
}
private var queueForRebuild = RebuildQueue()
private class Cache<T> {
private var values = [FileCacheKey: T]()
private let factory: (SwiftLintFile) -> T
@@ -235,6 +215,7 @@ extension SwiftLintFile {
/// Invalidates all cached data for this file.
public func invalidateCache() {
file.clearCaches()
responseCache.invalidate(self)
assertHandlerCache.invalidate(self)
structureCache.invalidate(self)
@@ -247,7 +228,6 @@ extension SwiftLintFile {
}
internal static func clearCaches() {
queueForRebuild.clear()
responseCache.clear()
assertHandlerCache.clear()
structureCache.clear()