|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftSyntax |
| 14 | + |
| 15 | +/// Unqualified lookup cache. Should be used when performing |
| 16 | +/// large sequences of adjacent lookups to maximise performance. |
| 17 | +public class LookupCache { |
| 18 | + /// Cached results of `ScopeSyntax.lookupParent` calls. |
| 19 | + /// Identified by `SyntaxIdentifier`. |
| 20 | + private var ancestorResultsCache: [SyntaxIdentifier: [LookupResult]] = [:] |
| 21 | + /// Cached results of `SequentialScopeSyntax.sequentialLookup` calls. |
| 22 | + /// Identified by `SyntaxIdentifier`. |
| 23 | + private var sequentialResultsCache: [SyntaxIdentifier: [LookupResult]] = [:] |
| 24 | + /// Looked-up scope identifiers during cache accesses. |
| 25 | + private var hits: Set<SyntaxIdentifier> = [] |
| 26 | + |
| 27 | + private let dropMod: Int |
| 28 | + private var evictionCount = 0 |
| 29 | + |
| 30 | + /// Creates a new unqualified lookup cache. |
| 31 | + /// `drop` parameter specifies how many eviction calls will be |
| 32 | + /// ignored before evicting not-hit members of the cache. |
| 33 | + /// |
| 34 | + /// Example cache eviction sequences (s - skip, e - evict): |
| 35 | + /// - `drop = 0` - `e -> e -> e -> e -> e -> ...` |
| 36 | + /// - `drop = 1` - `s -> e -> s -> s -> e -> ...` |
| 37 | + /// - `drop = 3` - `s -> s -> s -> e -> s -> ...` |
| 38 | + /// |
| 39 | + /// - Note: `drop = 0` effectively maintains exactly one path of cached results to |
| 40 | + /// the root in the cache (assuming we evict cache members after each lookup in a sequence of lookups). |
| 41 | + /// Higher the `drop` value, more such paths can potentially be stored in the cache at any given moment. |
| 42 | + /// Because of that, a higher `drop` value also translates to a higher number of cache-hits, |
| 43 | + /// but it might not directly translate to better performance. Because of a larger memory footprint, |
| 44 | + /// memory accesses could take longer, slowing down the eviction process. That's why the `drop` value |
| 45 | + /// could be fine-tuned to maximize the performance given file size, |
| 46 | + /// number of lookups, and amount of available memory. |
| 47 | + public init(drop: Int = 0) { |
| 48 | + self.dropMod = drop + 1 |
| 49 | + } |
| 50 | + |
| 51 | + /// Get cached ancestor results for the given `id`. |
| 52 | + /// `nil` if there's no cache entry for the given `id`. |
| 53 | + /// Adds `id` and ids of all ancestors to the cache `hits`. |
| 54 | + func getCachedAncestorResults(id: SyntaxIdentifier) -> [LookupResult]? { |
| 55 | + guard let results = ancestorResultsCache[id] else { return nil } |
| 56 | + hits.formUnion(results.map(\.scope.id)) |
| 57 | + hits.insert(id) |
| 58 | + return results |
| 59 | + } |
| 60 | + |
| 61 | + /// Set cached ancestor results for the given `id`. |
| 62 | + /// Adds `id` to the cache `hits`. |
| 63 | + func setCachedAncestorResults(id: SyntaxIdentifier, results: [LookupResult]) { |
| 64 | + hits.insert(id) |
| 65 | + ancestorResultsCache[id] = results |
| 66 | + } |
| 67 | + |
| 68 | + /// Get cached sequential lookup results for the given `id`. |
| 69 | + /// `nil` if there's no cache entry for the given `id`. |
| 70 | + /// Adds `id` to the cache `hits`. |
| 71 | + func getCachedSequentialResults(id: SyntaxIdentifier) -> [LookupResult]? { |
| 72 | + guard let results = sequentialResultsCache[id] else { return nil } |
| 73 | + hits.insert(id) |
| 74 | + return results |
| 75 | + } |
| 76 | + |
| 77 | + /// Set cached sequential lookup results for the given `id`. |
| 78 | + /// Adds `id` to the cache `hits`. |
| 79 | + func setCachedSequentialResults(id: SyntaxIdentifier, results: [LookupResult]) { |
| 80 | + hits.insert(id) |
| 81 | + sequentialResultsCache[id] = results |
| 82 | + } |
| 83 | + |
| 84 | + /// Removes all cached entries without a hit, unless it's prohibited |
| 85 | + /// by the internal drop counter (as specified by `drop` in the initializer). |
| 86 | + /// The dropping behavior can be disabled for this call with the `bypassDropCounter` |
| 87 | + /// parameter, resulting in immediate eviction of entries without a hit. |
| 88 | + public func evictEntriesWithoutHit(bypassDropCounter: Bool = false) { |
| 89 | + if !bypassDropCounter { |
| 90 | + evictionCount = (evictionCount + 1) % dropMod |
| 91 | + guard evictionCount != 0 else { return } |
| 92 | + } |
| 93 | + |
| 94 | + for key in Set(ancestorResultsCache.keys).union(sequentialResultsCache.keys).subtracting(hits) { |
| 95 | + ancestorResultsCache.removeValue(forKey: key) |
| 96 | + sequentialResultsCache.removeValue(forKey: key) |
| 97 | + } |
| 98 | + |
| 99 | + hits = [] |
| 100 | + } |
| 101 | +} |
0 commit comments