-
Notifications
You must be signed in to change notification settings - Fork 440
[Macros] Cache parsed syntax tree in compiler plugins #2682
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
Merged
rintaro
merged 2 commits into
swiftlang:main
from
rintaro:macros-parsed-syntax-registry
Jun 15, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
116 changes: 116 additions & 0 deletions
116
Sources/SwiftCompilerPluginMessageHandling/LRUCache.swift
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,116 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Simple LRU cache. | ||
@_spi(Testing) | ||
public class LRUCache<Key: Hashable, Value> { | ||
private class _Node { | ||
unowned var prev: _Node? = nil | ||
unowned var next: _Node? = nil | ||
|
||
let key: Key | ||
var value: Value | ||
|
||
init(key: Key, value: Value) { | ||
self.key = key | ||
self.value = value | ||
} | ||
} | ||
|
||
private var table: [Key: _Node] | ||
|
||
// Double linked list | ||
private unowned var head: _Node? | ||
private unowned var tail: _Node? | ||
|
||
public let capacity: Int | ||
|
||
public init(capacity: Int) { | ||
self.table = [:] | ||
self.head = nil | ||
self.tail = nil | ||
self.capacity = capacity | ||
} | ||
|
||
public var count: Int { | ||
return table.count | ||
} | ||
|
||
public subscript(key: Key) -> Value? { | ||
get { | ||
guard let node = table[key] else { | ||
return nil | ||
} | ||
moveToHead(node: node) | ||
return node.value | ||
} | ||
|
||
set { | ||
switch (table[key], newValue) { | ||
case let (nil, newValue?): // create. | ||
self.ensureCapacityForNewValue() | ||
let node = _Node(key: key, value: newValue) | ||
addToHead(node: node) | ||
table[key] = node | ||
|
||
case let (node?, newValue?): // update. | ||
moveToHead(node: node) | ||
node.value = newValue | ||
|
||
case let (node?, nil): // delete. | ||
remove(node: node) | ||
table[key] = nil | ||
|
||
case (nil, nil): // no-op. | ||
break | ||
} | ||
} | ||
} | ||
|
||
private func ensureCapacityForNewValue() { | ||
while self.table.count >= self.capacity, let tail = self.tail { | ||
remove(node: tail) | ||
table[tail.key] = nil | ||
} | ||
} | ||
|
||
private func moveToHead(node: _Node) { | ||
if node === self.head { | ||
return | ||
} | ||
remove(node: node) | ||
addToHead(node: node) | ||
} | ||
|
||
private func addToHead(node: _Node) { | ||
node.next = self.head | ||
node.next?.prev = node | ||
node.prev = nil | ||
self.head = node | ||
if self.tail == nil { | ||
self.tail = node | ||
} | ||
} | ||
|
||
private func remove(node: _Node) { | ||
node.next?.prev = node.prev | ||
node.prev?.next = node.next | ||
if node === self.head { | ||
self.head = node.next | ||
} | ||
if node === self.tail { | ||
self.tail = node.prev | ||
} | ||
node.prev = nil | ||
node.next = nil | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@_spi(Testing) import SwiftCompilerPluginMessageHandling | ||
import XCTest | ||
|
||
final class LRUCacheTests: XCTestCase { | ||
func testBasic() { | ||
let cache = LRUCache<String, Int>(capacity: 2) | ||
cache["foo"] = 0 | ||
cache["bar"] = 1 | ||
XCTAssertEqual(cache["foo"], 0) | ||
cache["baz"] = 2 | ||
XCTAssertEqual(cache["foo"], 0) | ||
XCTAssertEqual(cache["bar"], nil) | ||
XCTAssertEqual(cache["baz"], 2) | ||
XCTAssertEqual(cache.count, 2) | ||
|
||
hamishknight marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cache["qux"] = nil | ||
cache["baz"] = nil | ||
cache["foo"] = 10 | ||
XCTAssertEqual(cache["foo"], 10) | ||
XCTAssertEqual(cache["bar"], nil) | ||
XCTAssertEqual(cache["baz"], nil) | ||
XCTAssertEqual(cache["qux"], nil) | ||
XCTAssertEqual(cache.count, 1) | ||
} | ||
} |
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.
Naming nitpick: A registry for me is something that stores long-lived values (possibly even eternal), which is quite the opposite of a cache. Would
ParsedSyntaxCache
be a better name?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.
Heh, in fairness it was originally the first. The LRU was added after 😅. But yeah, Cache is probably the better name now.