Skip to content

Commit 13fd74d

Browse files
committed
Actually cache rule names.
The `nameCache` for rule names wasn't actually ever storing any rule names, so the cache hit rate was pretty low. Now rule names are stored in the cache and the hit rate is much better. I tested this by formatting the swift-protobuf library. - Before: average 56.7 seconds - After: average 24.3 seconds
1 parent 52626da commit 13fd74d

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Sources/SwiftFormatCore/Rule.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ fileprivate var nameCache = [ObjectIdentifier: String]()
2727
extension Rule {
2828
/// By default, the `ruleName` is just the name of the implementing rule class.
2929
public static var ruleName: String {
30-
return nameCache[
31-
ObjectIdentifier(self),
32-
default: String("\(self)".split(separator: ".").last!)
33-
]
30+
let identifier = ObjectIdentifier(self)
31+
if let cachedName = nameCache[identifier] {
32+
return cachedName
33+
}
34+
let name = String("\(self)".split(separator: ".").last!)
35+
nameCache[identifier] = name
36+
return name
3437
}
3538
}

0 commit comments

Comments
 (0)