-
Notifications
You must be signed in to change notification settings - Fork 441
[SwiftLexicalLookup] Unqualified lookup caching #3068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MAJKFL
wants to merge
5
commits into
swiftlang:main
Choose a base branch
from
MAJKFL:swift-lexical-lookup-caching
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e547074
Merge remote-tracking branch 'upstream/main' into rfc-upstream-sync
MAJKFL e7c02d8
Add `SwiftLexicalLookup` result caching.
MAJKFL 83e1ca3
Add `SwiftLexicalLookup` cache unit testing.
MAJKFL 047d95b
Merge remote-tracking branch 'upstream/main' into swift-lexical-looku…
MAJKFL 07aa269
Add missing cache parameters to lookup calls.
MAJKFL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,101 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftSyntax | ||
|
||
/// Unqualified lookup cache. Should be used when performing | ||
/// large sequences of adjacent lookups to maximise performance. | ||
public class LookupCache { | ||
/// Cached results of `ScopeSyntax.lookupParent` calls. | ||
/// Identified by `SyntaxIdentifier`. | ||
private var ancestorResultsCache: [SyntaxIdentifier: [LookupResult]] = [:] | ||
/// Cached results of `SequentialScopeSyntax.sequentialLookup` calls. | ||
/// Identified by `SyntaxIdentifier`. | ||
private var sequentialResultsCache: [SyntaxIdentifier: [LookupResult]] = [:] | ||
/// Looked-up scope identifiers during cache accesses. | ||
private var hits: Set<SyntaxIdentifier> = [] | ||
|
||
private let dropMod: Int | ||
private var evictionCount = 0 | ||
|
||
/// Creates a new unqualified lookup cache. | ||
/// `drop` parameter specifies how many eviction calls will be | ||
/// ignored before evicting not-hit members of the cache. | ||
/// | ||
/// Example cache eviction sequences (s - skip, e - evict): | ||
/// - `drop = 0` - `e -> e -> e -> e -> e -> ...` | ||
/// - `drop = 1` - `s -> e -> s -> s -> e -> ...` | ||
/// - `drop = 3` - `s -> s -> s -> e -> s -> ...` | ||
/// | ||
/// - Note: `drop = 0` effectively maintains exactly one path of cached results to | ||
/// the root in the cache (assuming we evict cache members after each lookup in a sequence of lookups). | ||
/// Higher the `drop` value, more such paths can potentially be stored in the cache at any given moment. | ||
/// Because of that, a higher `drop` value also translates to a higher number of cache-hits, | ||
/// but it might not directly translate to better performance. Because of a larger memory footprint, | ||
/// memory accesses could take longer, slowing down the eviction process. That's why the `drop` value | ||
/// could be fine-tuned to maximize the performance given file size, | ||
/// number of lookups, and amount of available memory. | ||
public init(drop: Int = 0) { | ||
self.dropMod = drop + 1 | ||
} | ||
|
||
/// Get cached ancestor results for the given `id`. | ||
/// `nil` if there's no cache entry for the given `id`. | ||
/// Adds `id` and ids of all ancestors to the cache `hits`. | ||
func getCachedAncestorResults(id: SyntaxIdentifier) -> [LookupResult]? { | ||
guard let results = ancestorResultsCache[id] else { return nil } | ||
hits.formUnion(results.map(\.scope.id)) | ||
hits.insert(id) | ||
return results | ||
} | ||
|
||
/// Set cached ancestor results for the given `id`. | ||
/// Adds `id` to the cache `hits`. | ||
func setCachedAncestorResults(id: SyntaxIdentifier, results: [LookupResult]) { | ||
hits.insert(id) | ||
ancestorResultsCache[id] = results | ||
} | ||
|
||
/// Get cached sequential lookup results for the given `id`. | ||
/// `nil` if there's no cache entry for the given `id`. | ||
/// Adds `id` to the cache `hits`. | ||
func getCachedSequentialResults(id: SyntaxIdentifier) -> [LookupResult]? { | ||
guard let results = sequentialResultsCache[id] else { return nil } | ||
hits.insert(id) | ||
return results | ||
} | ||
|
||
/// Set cached sequential lookup results for the given `id`. | ||
/// Adds `id` to the cache `hits`. | ||
func setCachedSequentialResults(id: SyntaxIdentifier, results: [LookupResult]) { | ||
hits.insert(id) | ||
sequentialResultsCache[id] = results | ||
} | ||
|
||
/// Removes all cached entries without a hit, unless it's prohibited | ||
/// by the internal drop counter (as specified by `drop` in the initializer). | ||
/// The dropping behavior can be disabled for this call with the `bypassDropCounter` | ||
/// parameter, resulting in immediate eviction of entries without a hit. | ||
public func evictEntriesWithoutHit(bypassDropCounter: Bool = false) { | ||
if !bypassDropCounter { | ||
evictionCount = (evictionCount + 1) % dropMod | ||
guard evictionCount != 0 else { return } | ||
} | ||
|
||
for key in Set(ancestorResultsCache.keys).union(sequentialResultsCache.keys).subtracting(hits) { | ||
ancestorResultsCache.removeValue(forKey: key) | ||
sequentialResultsCache.removeValue(forKey: key) | ||
} | ||
|
||
hits = [] | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not a fan of the
drop
naming here. I don’t have a better suggestion yet, maybe I’ll come up with one.