Skip to content

Commit 1d00e3c

Browse files
author
Itai Ferber
committed
Migrate Data from hashValue to hash(into:)
1 parent 1b177d0 commit 1d00e3c

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

stdlib/public/Darwin/Foundation/Data.swift

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -866,12 +866,10 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
866866
}
867867
}
868868

869-
@inlinable
870-
var hashValue: Int {
871-
let count: Int = numericCast(length)
872-
return Swift.withUnsafeBytes(of: bytes) { (rawBuffer) -> Int in
873-
return Int(bitPattern: CFHashBytes(UnsafeMutablePointer(mutating: rawBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self)), count))
874-
}
869+
@inlinable @inline(__always) // This should always be inlined into _Representation.hash(into:).
870+
func hash(into hasher: inout Hasher) {
871+
hasher.combine(length)
872+
Swift.withUnsafeBytes(of: bytes) { hasher.combine(bytes: $0) }
875873
}
876874
}
877875

@@ -1084,11 +1082,14 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
10841082
storage.copyBytes(to: pointer, from: range)
10851083
}
10861084

1087-
@inlinable
1088-
var hashValue: Int {
1089-
let hashRange = startIndex..<Swift.min(startIndex + 80, endIndex)
1090-
return storage.withUnsafeBytes(in: hashRange) {
1091-
return Int(bitPattern: CFHashBytes(UnsafeMutablePointer(mutating: $0.baseAddress?.assumingMemoryBound(to: UInt8.self)), $0.count))
1085+
@inlinable @inline(__always) // This should always be inlined into _Representation.hash(into:).
1086+
func hash(into hasher: inout Hasher) {
1087+
hasher.combine(count)
1088+
1089+
// At most, hash the first 80 bytes of this data.
1090+
let range = startIndex ..< Swift.min(startIndex + 80, endIndex)
1091+
storage.withUnsafeBytes(in: range) {
1092+
hasher.combine(bytes: $0)
10921093
}
10931094
}
10941095
}
@@ -1271,11 +1272,14 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
12711272
storage.copyBytes(to: pointer, from: range)
12721273
}
12731274

1274-
@inlinable
1275-
var hashValue: Int {
1276-
let hashRange = startIndex..<Swift.min(startIndex + 80, endIndex)
1277-
return storage.withUnsafeBytes(in: hashRange) {
1278-
return Int(bitPattern: CFHashBytes(UnsafeMutablePointer(mutating: $0.baseAddress?.assumingMemoryBound(to: UInt8.self)), CFIndex($0.count)))
1275+
@inlinable @inline(__always) // This should always be inlined into _Representation.hash(into:).
1276+
func hash(into hasher: inout Hasher) {
1277+
hasher.combine(count)
1278+
1279+
// Hash at most the first 80 bytes of this data.
1280+
let range = startIndex ..< Swift.min(startIndex + 80, endIndex)
1281+
storage.withUnsafeBytes(in: range) {
1282+
hasher.combine(bytes: $0)
12791283
}
12801284
}
12811285
}
@@ -1847,17 +1851,17 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
18471851
}
18481852
}
18491853

1850-
@inlinable
1851-
var hashValue: Int {
1854+
@inlinable @inline(__always) // This should always be inlined into Data.hash(into:).
1855+
func hash(into hasher: inout Hasher) {
18521856
switch self {
18531857
case .empty:
1854-
return Int(bitPattern: CFHashBytes(nil, 0))
1858+
hasher.combine(Int(bitPattern: CFHashBytes(nil, 0)))
18551859
case .inline(let inline):
1856-
return inline.hashValue
1860+
inline.hash(into: &hasher)
18571861
case .slice(let slice):
1858-
return slice.hashValue
1859-
case .large(let slice):
1860-
return slice.hashValue
1862+
slice.hash(into: &hasher)
1863+
case .large(let large):
1864+
large.hash(into: &hasher)
18611865
}
18621866
}
18631867
}
@@ -2508,9 +2512,9 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
25082512
//
25092513

25102514
/// The hash value for the data.
2511-
@inlinable
2512-
public var hashValue: Int {
2513-
return _representation.hashValue
2515+
@inline(never) // This is not inlinable as emission into clients could cause cross-module inconsistencies if they are not all recompiled together.
2516+
public func hash(into hasher: inout Hasher) {
2517+
_representation.hash(into: &hasher)
25142518
}
25152519

25162520
@inlinable

0 commit comments

Comments
 (0)