-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from nschum/feature/cache
Add caching of Structure and SyntaxMap.
- Loading branch information
Showing
9 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import SourceKittenFramework | ||
|
||
private var structureCache = Cache({file in Structure(file: file)}) | ||
private var syntaxMapCache = Cache({file in SyntaxMap(file: file)}) | ||
|
||
private struct Cache<T> { | ||
|
||
private var values = [String: T]() | ||
private var factory: File -> T | ||
|
||
private init(_ factory: File -> T) { | ||
self.factory = factory | ||
} | ||
|
||
private mutating func get(file: File) -> T { | ||
if let path = file.path { | ||
if let value = values[path] { | ||
return value | ||
} else { | ||
let value = factory(file) | ||
values[path] = value | ||
return value | ||
} | ||
} else { | ||
return factory(file) | ||
} | ||
} | ||
|
||
private mutating func clear() { | ||
values.removeAll(keepCapacity: false) | ||
} | ||
} | ||
|
||
public extension File { | ||
|
||
public var structure: Structure { | ||
return structureCache.get(self) | ||
} | ||
|
||
public var syntaxMap: SyntaxMap { | ||
return syntaxMapCache.get(self) | ||
} | ||
|
||
public static func clearCaches() { | ||
structureCache.clear() | ||
syntaxMapCache.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters