Skip to content

[SE-0206][stdlib] hash(into:)/hashValue cleanup #16154

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 3 commits into from
Apr 30, 2018
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
5 changes: 0 additions & 5 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2263,11 +2263,6 @@ extension ${Self} : Equatable where Element : Equatable {
}

extension ${Self}: Hashable where Element: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(count) // discriminator
Expand Down
12 changes: 0 additions & 12 deletions stdlib/public/core/Bool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,6 @@ public // COMPILER_INTRINSIC
func _getBool(_ v: Builtin.Int1) -> Bool { return Bool(v) }

extension Bool : Equatable, Hashable {
/// The hash value for the Boolean value.
///
/// Two values that are equal always have equal hash values.
///
/// - Note: The hash value is not guaranteed to be stable across different
/// invocations of the same program. Do not persist the hash value across
/// program runs.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine((self ? 1 : 0) as UInt8)
Expand Down
10 changes: 0 additions & 10 deletions stdlib/public/core/CTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,6 @@ extension OpaquePointer: Equatable {
}

extension OpaquePointer: Hashable {
/// The pointer's hash value.
///
/// The hash value is not guaranteed to be stable across different
/// invocations of the same program. Do not persist the hash value across
/// program runs.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(Int(Builtin.ptrtoint_Word(_rawValue)))
Expand Down
11 changes: 4 additions & 7 deletions stdlib/public/core/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,11 @@ extension Character : Comparable {
}

extension Character: Hashable {
/// The character's hash value.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
// not @inlinable (performance)
@effects(releasenone)
public func hash(into hasher: inout Hasher) {
// FIXME(performance): constructing a temporary string is extremely
// wasteful and inefficient.
return String(self).hashValue
hasher.combine(String(self))
}
}
10 changes: 0 additions & 10 deletions stdlib/public/core/ClosedRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,6 @@ extension ClosedRange.Index : Comparable {

extension ClosedRange.Index: Hashable
where Bound: Strideable, Bound.Stride: SignedInteger, Bound: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
switch self {
Expand Down Expand Up @@ -395,11 +390,6 @@ extension ClosedRange: Equatable {
}

extension ClosedRange: Hashable where Bound: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(lowerBound)
Expand Down
5 changes: 5 additions & 0 deletions stdlib/public/core/Codable.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,11 @@ public struct CodingUserInfoKey : RawRepresentable, Equatable, Hashable {
public var hashValue: Int {
return self.rawValue.hashValue
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(self.rawValue)
}
}

//===----------------------------------------------------------------------===//
Expand Down
27 changes: 16 additions & 11 deletions stdlib/public/core/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1449,11 +1449,6 @@ extension Dictionary: Equatable where Value: Equatable {
}

extension Dictionary: Hashable where Value: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
var commutativeHash = 0
Expand Down Expand Up @@ -4334,18 +4329,28 @@ extension Dictionary.Index {

@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
#if _runtime(_ObjC)
if _fastPath(_guaranteedNative) {
return _nativeIndex.offset
hasher.combine(0 as UInt8)
hasher.combine(_nativeIndex.offset)
return
}

switch _value {
case ._native(let nativeIndex):
return nativeIndex.offset
#if _runtime(_ObjC)
hasher.combine(0 as UInt8)
hasher.combine(nativeIndex.offset)
case ._cocoa(let cocoaIndex):
return cocoaIndex.currentKeyIndex
#endif
hasher.combine(1 as UInt8)
hasher.combine(cocoaIndex.currentKeyIndex)
}
#else
hasher.combine(_nativeIndex.offset)
#endif
}
}

Expand Down
5 changes: 0 additions & 5 deletions stdlib/public/core/Flatten.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,6 @@ extension FlattenCollection.Index : Comparable {

extension FlattenCollection.Index : Hashable
where Base.Index : Hashable, Base.Element.Index : Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(_outer)
Expand Down
9 changes: 0 additions & 9 deletions stdlib/public/core/FloatingPointTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1521,15 +1521,6 @@ extension ${Self} : _ExpressibleByBuiltinFloatLiteral {
% end

extension ${Self} : Hashable {
/// The number's hash value.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
var v = self
Expand Down
13 changes: 0 additions & 13 deletions stdlib/public/core/Integers.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -3694,19 +3694,6 @@ ${assignmentOperatorComment(x.operator, True)}
%# end of concrete type: ${Self}

extension ${Self} : Hashable {
/// The integer's hash value.
///
/// The hash value is not guaranteed to be stable across different
/// invocations of the same program. Do not persist the hash value across
/// program runs.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
@inline(__always)
get {
return _hashValue(for: self)
}
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
// FIXME(hasher): To correctly bridge `Set`s/`Dictionary`s containing
Expand Down
14 changes: 2 additions & 12 deletions stdlib/public/core/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class AnyKeyPath: Hashable, _AppendKeyPath {
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
final public func hash(into hasher: inout Hasher) {
return withBuffer {
var buffer = $0
while true {
Expand Down Expand Up @@ -441,12 +441,7 @@ internal struct ComputedPropertyID: Hashable {
}

@inlinable // FIXME(sil-serialize-all)
internal var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
internal func hash(into hasher: inout Hasher) {
hasher.combine(value)
hasher.combine(isStoredProperty)
hasher.combine(isTableOffset)
Expand Down Expand Up @@ -573,11 +568,6 @@ internal enum KeyPathComponent: Hashable {
}
}

@inlinable // FIXME(sil-serialize-all)
internal var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
internal func hash(into hasher: inout Hasher) {
var hasher = hasher
Expand Down
10 changes: 2 additions & 8 deletions stdlib/public/core/ObjectIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,9 @@ extension ObjectIdentifier: Comparable {
}

extension ObjectIdentifier: Hashable {
// FIXME: Better hashing algorithm
/// The identifier's hash value.
///
/// The hash value is not guaranteed to be stable across different
/// invocations of the same program. Do not persist the hash value across
/// program runs.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return Int(Builtin.ptrtoint_Word(_value))
public func hash(into hasher: inout Hasher) {
hasher.combine(Int(Builtin.ptrtoint_Word(_value)))
}
}

Expand Down
11 changes: 0 additions & 11 deletions stdlib/public/core/Optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,6 @@ extension Optional : Equatable where Wrapped : Equatable {
}

extension Optional: Hashable where Wrapped: Hashable {
/// The hash value for the optional instance.
///
/// Two optionals that are equal will always have equal hash values.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
switch self {
Expand Down
5 changes: 0 additions & 5 deletions stdlib/public/core/PrefixWhile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@ extension LazyPrefixWhileCollection.Index: Comparable {
}

extension LazyPrefixWhileCollection.Index: Hashable where Base.Index: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
switch _value {
Expand Down
5 changes: 0 additions & 5 deletions stdlib/public/core/Range.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,6 @@ extension Range: Equatable {
}

extension Range: Hashable where Bound: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(lowerBound)
Expand Down
5 changes: 0 additions & 5 deletions stdlib/public/core/Reverse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,6 @@ extension ReversedCollection.Index: Comparable {
}

extension ReversedCollection.Index: Hashable where Base.Index: Hashable {
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return base.hashValue
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
hasher.combine(base)
Expand Down
35 changes: 17 additions & 18 deletions stdlib/public/core/Set.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,20 +484,9 @@ extension Set: Equatable {
}

extension Set: Hashable {
/// The hash value for the set.
///
/// Two sets that are equal will always have equal hash values.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
// FIXME(ABI)#177: <rdar://problem/18915294> Cache Set<T> hashValue
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
// FIXME(ABI)#177: <rdar://problem/18915294> Cache Set<T> hashValue
var hash = 0
for member in self {
hash ^= _hashValue(for: member)
Expand Down Expand Up @@ -3662,18 +3651,28 @@ extension Set.Index {

@inlinable // FIXME(sil-serialize-all)
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable // FIXME(sil-serialize-all)
public func hash(into hasher: inout Hasher) {
#if _runtime(_ObjC)
if _fastPath(_guaranteedNative) {
return _nativeIndex.offset
hasher.combine(0 as UInt8)
hasher.combine(_nativeIndex.offset)
return
}

switch _value {
case ._native(let nativeIndex):
return nativeIndex.offset
#if _runtime(_ObjC)
hasher.combine(0 as UInt8)
hasher.combine(nativeIndex.offset)
case ._cocoa(let cocoaIndex):
return cocoaIndex.currentKeyIndex
#endif
hasher.combine(1 as UInt8)
hasher.combine(cocoaIndex.currentKeyIndex)
}
#else
hasher.combine(_nativeIndex.offset)
#endif
}
}

Expand Down
14 changes: 0 additions & 14 deletions stdlib/public/core/StringHashable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,13 @@ extension _StringGuts {
}

extension String : Hashable {
/// The string's hash value.
///
/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save hash values to use during a future execution.
@inlinable
public var hashValue: Int {
return _hashValue(for: self)
}

@inlinable
public func hash(into hasher: inout Hasher) {
_guts.hash(into: &hasher)
}
}

extension StringProtocol {
@inlinable
public var hashValue : Int {
return _hashValue(for: self)
}

@inlinable
public func hash(into hasher: inout Hasher) {
_wholeString._guts.hash(_encodedOffsetRange, into: &hasher)
Expand Down
Loading