Skip to content

Commit

Permalink
Cache location converters (#4481)
Browse files Browse the repository at this point in the history
This is 2%-12% faster according to OSSCheck
  • Loading branch information
jpsim authored Oct 26, 2022
1 parent 6b5352f commit 1b59a3f
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ private let structureDictionaryCache = Cache { file in
private let syntaxTreeCache = Cache { file -> SourceFileSyntax in
return Parser.parse(source: file.contents)
}
private let locationConverterCache = Cache { file -> SourceLocationConverter in
return SourceLocationConverter(file: file.path ?? "<nopath>", tree: file.syntaxTree)
}
private let commandsCache = Cache { file -> [Command] in
guard file.contents.contains("swiftlint:") else {
return []
Expand Down Expand Up @@ -164,9 +167,7 @@ extension SwiftLintFile {

internal var syntaxTree: SourceFileSyntax { syntaxTreeCache.get(self) }

internal var locationConverter: SourceLocationConverter {
SourceLocationConverter(file: path ?? "<nopath>", tree: syntaxTree)
}
internal var locationConverter: SourceLocationConverter { locationConverterCache.get(self) }

internal var commands: [Command] { commandsCache.get(self) }

Expand Down Expand Up @@ -204,6 +205,7 @@ extension SwiftLintFile {
syntaxTokensByLinesCache.invalidate(self)
syntaxKindsByLinesCache.invalidate(self)
syntaxTreeCache.invalidate(self)
locationConverterCache.invalidate(self)
commandsCache.invalidate(self)
linesWithTokensCache.invalidate(self)
}
Expand All @@ -218,6 +220,7 @@ extension SwiftLintFile {
syntaxTokensByLinesCache.clear()
syntaxKindsByLinesCache.clear()
syntaxTreeCache.clear()
locationConverterCache.clear()
commandsCache.clear()
linesWithTokensCache.clear()
}
Expand Down

0 comments on commit 1b59a3f

Please sign in to comment.