Skip to content

Fix direct FilePath hashing in QueryEngine #7926

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 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions Sources/QueryEngine/CacheKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Copyright (c) 2023-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
Expand All @@ -16,17 +16,21 @@ 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.
package protocol CacheKey: Encodable {
public protocol CacheKey: Encodable {}

/// Types that cannot be decomposed more to be hashed
protocol LeafCacheKey: CacheKey {
func hash(with hashFunction: inout some HashFunction)
}

extension Bool: CacheKey {
extension Bool: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
hashFunction.update(data: self ? [1] : [0])
}
}

extension Int: CacheKey {
extension Int: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -35,7 +39,7 @@ extension Int: CacheKey {
}
}

extension Int8: CacheKey {
extension Int8: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -44,7 +48,7 @@ extension Int8: CacheKey {
}
}

extension Int16: CacheKey {
extension Int16: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -53,7 +57,7 @@ extension Int16: CacheKey {
}
}

extension Int32: CacheKey {
extension Int32: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -62,7 +66,7 @@ extension Int32: CacheKey {
}
}

extension Int64: CacheKey {
extension Int64: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -71,7 +75,7 @@ extension Int64: CacheKey {
}
}

extension UInt: CacheKey {
extension UInt: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -80,7 +84,7 @@ extension UInt: CacheKey {
}
}

extension UInt8: CacheKey {
extension UInt8: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -89,7 +93,7 @@ extension UInt8: CacheKey {
}
}

extension UInt16: CacheKey {
extension UInt16: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -98,7 +102,7 @@ extension UInt16: CacheKey {
}
}

extension UInt32: CacheKey {
extension UInt32: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -107,7 +111,7 @@ extension UInt32: CacheKey {
}
}

extension UInt64: CacheKey {
extension UInt64: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -116,7 +120,7 @@ extension UInt64: CacheKey {
}
}

extension Float: CacheKey {
extension Float: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -125,7 +129,7 @@ extension Float: CacheKey {
}
}

extension Double: CacheKey {
extension Double: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -134,7 +138,7 @@ extension Double: CacheKey {
}
}

extension String: CacheKey {
extension String: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
var t = String(reflecting: Self.self)
t.withUTF8 {
Expand All @@ -147,21 +151,21 @@ extension String: CacheKey {
}
}

extension FilePath: CacheKey {
extension FilePath: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.string.hash(with: &hashFunction)
}
}

extension FilePath.Component: CacheKey {
extension FilePath.Component: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.string.hash(with: &hashFunction)
}
}

extension URL: CacheKey {
extension URL: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.description.hash(with: &hashFunction)
Expand Down
Loading