-
Notifications
You must be signed in to change notification settings - Fork 18
Add GeneratorEngine
module with tests and macros
#31
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4db7eaf
Add `SystemSQLite` target for future use in caching
MaxDesiatov 3ea95f4
Add `GeneratorEngine` module with tests and macros
MaxDesiatov 73c1f4e
Fix EngineTests after API updates
MaxDesiatov a5e5254
Merge branch 'main' into maxd/engine
MaxDesiatov c525ec9
Fix duplicate `SystemSQLite` target
MaxDesiatov 09201ea
Add `libsqlite-dev` dependency to `Dockerfile`
MaxDesiatov 80a4173
Bump default version to 5.9 in `Docker/Dockerfile`
MaxDesiatov b5304e8
Use `libsqlite3-dev` instead of `libsqlite-dev` in `Dockerfile`
MaxDesiatov c96bda6
Fix incompatibility with swift-corelibs-foundation
MaxDesiatov 2d677b5
Address PR feedback
MaxDesiatov 5ccecd4
Avoid passing keys as strings in most places
MaxDesiatov aef9fcb
Merge branch 'main' into maxd/engine
MaxDesiatov 996fd27
Merge branch 'main' into maxd/engine
MaxDesiatov cc71706
Merge branch 'main' into maxd/engine
MaxDesiatov 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
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,81 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2023 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 Macros | ||
|
||
@_exported import protocol Crypto.HashFunction | ||
import struct Foundation.URL | ||
import struct SystemPackage.FilePath | ||
|
||
/// Indicates that values of a conforming type can be hashed with an arbitrary hashing function. Unlike `Hashable`, | ||
/// this protocol doesn't utilize random seed values and produces consistent hash values across process launches. | ||
public protocol CacheKeyProtocol { | ||
func hash(with hashFunction: inout some HashFunction) | ||
} | ||
|
||
extension Bool: CacheKeyProtocol { | ||
public func hash(with hashFunction: inout some HashFunction) { | ||
"Swift.Bool".hash(with: &hashFunction) | ||
hashFunction.update(data: self ? [1] : [0]) | ||
} | ||
} | ||
|
||
extension Int: CacheKeyProtocol { | ||
public func hash(with hashFunction: inout some HashFunction) { | ||
"Swift.Int".hash(with: &hashFunction) | ||
withUnsafeBytes(of: self) { | ||
hashFunction.update(bufferPointer: $0) | ||
} | ||
} | ||
} | ||
|
||
extension String: CacheKeyProtocol { | ||
public func hash(with hashFunction: inout some HashFunction) { | ||
var t = "Swift.String" | ||
t.withUTF8 { | ||
hashFunction.update(bufferPointer: .init($0)) | ||
} | ||
var x = self | ||
x.withUTF8 { | ||
hashFunction.update(bufferPointer: .init($0)) | ||
} | ||
} | ||
} | ||
|
||
extension FilePath: CacheKeyProtocol { | ||
public func hash(with hashFunction: inout some HashFunction) { | ||
"SystemPackage.FilePath".hash(with: &hashFunction) | ||
self.string.hash(with: &hashFunction) | ||
} | ||
} | ||
|
||
extension URL: CacheKeyProtocol { | ||
public func hash(with hashFunction: inout some HashFunction) { | ||
"Foundation.URL".hash(with: &hashFunction) | ||
self.description.hash(with: &hashFunction) | ||
} | ||
} | ||
|
||
extension Optional: CacheKeyProtocol where Wrapped: CacheKeyProtocol { | ||
public func hash(with hashFunction: inout some HashFunction) { | ||
"Swift.Optional".hash(with: &hashFunction) | ||
if let self { | ||
true.hash(with: &hashFunction) | ||
self.hash(with: &hashFunction) | ||
} else { | ||
false.hash(with: &hashFunction) | ||
} | ||
} | ||
} | ||
|
||
@attached(extension, conformances: CacheKeyProtocol, names: named(hash(with:))) | ||
public macro CacheKey() = #externalMacro(module: "Macros", type: "CacheKeyMacro") |
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,38 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2023 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 struct SystemPackage.FilePath | ||
|
||
struct FileCacheRecord { | ||
let path: FilePath | ||
let hash: String | ||
} | ||
|
||
extension FileCacheRecord: Codable { | ||
enum CodingKeys: CodingKey { | ||
case path | ||
case hash | ||
} | ||
|
||
// FIXME: `Codable` on `FilePath` is broken | ||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.path = try FilePath(container.decode(String.self, forKey: .path)) | ||
self.hash = try container.decode(String.self, forKey: .hash) | ||
} | ||
|
||
func encode(to encoder: any Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.path.string, forKey: .path) | ||
try container.encode(self.hash, forKey: .hash) | ||
} | ||
} |
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.
that's not ideal. You want to do hash
hashOf(0 or false)
ifnil
. AndhashOf(1 or true) ++ hashOf(self)
if not nil.Otherwise you'll get hash unnecesary hash collisions
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.
@MaxDesiatov Does the code here address @weissi 's comment?
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.
Not sure why it doesn't show "outdated" here, I've updated it this way to address the comment:
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 thought that might be it, but I was having trouble following the PR history to check.