<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br><br>Sent from my iPhone</div><div><br>On 13 Mar 2017, at 19:30, Dimitri Racordon via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
While I understand and agree with the goal of the proposal, I too feel like it’s more difficult to understand than the current approach. HashVisitable looks odd, and one would have to understand the role and API of the Hasher protocol.
<div class=""><br class="">
</div>
<div class="">I would argue that if Swift provided a good default hashing API, then we could simply call it when computing the hashValue.</div>
<div class=""><br class="">
</div>
<div class=""><font face="Menlo" class="">struct GridPoint: Hashable {</font></div>
<div class=""><font face="Menlo" class=""> var x: Int</font></div>
<div class=""><font face="Menlo" class=""> var y: Int</font></div>
<div class=""><font face="Menlo" class=""> var hashValue: Int {</font></div>
<div class=""><font face="Menlo" class=""> let hasher = Swift.DefaultHasher()</font></div>
<div class=""><font face="Menlo" class=""> hasher.hash(self.x)</font></div>
<div class=""><font face="Menlo" class=""> hasher.hash(self.y)</font></div>
<div class=""><font face="Menlo" class=""> return hasher.value</font></div>
<div class=""><font face="Menlo" class=""> }</font></div>
<div class=""><font face="Menlo" class=""> static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { /* … */ }</font></div>
<div class=""><font face="Menlo" class="">}</font><br class="">
<div class=""><br class="">
</div>
<div class="">It might seem wordy, but it would allow one to leverage good hashing algorithms, while not forcing people who do have a good hashing function for their particular type to define their own hasher.</div></div></div></blockquote><div><br></div><div>This is exactly what this proposal is proposing, with a slightly different API that allows you to more easily handle subtypes.</div><div><br></div><div>What in the current proposal is more complicated to you than what you propose?</div><br><blockquote type="cite"><div><div class="">
<div class="">On an unrelated note, maybe I missed something but I think HashVisitable would still need to conform to Equatable as well, if it were to replace Hashable. This is unclear in your proposal, as the equality operator is present in the motivational
example, but absent from the proposed solution.</div>
<div class=""><br class="">
<div>
<blockquote type="cite" class="">
<div class="">On 13 Mar 2017, at 18:54, Sean Heber via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="">I’m dumb when it comes to proper hashing, but it’s such a tediously common thing in my experience to need to add Hashable to some kind of a struct so I can stash it in a set or use it as a dictionary key. Is there really no way to make this all
more automatic? I have to be honest - this proposal seems *harder* to understand than the way it works now. Of course the easiest would be if the language could just do this “good enough" for me using reflection or whatever and if I really did run into a problem
where I wanted to do this myself, I could override something.<br class="">
<br class="">
Perfect is the enemy of good.<br class="">
<br class="">
l8r<br class="">
Sean<br class="">
<br class="">
<br class="">
<blockquote type="cite" class="">On Mar 13, 2017, at 10:38 AM, Vincent Esche via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class="">
<br class="">
Hi there,<br class="">
<br class="">
I've written up a proposal for an overhaul/replacement of the Hashable protocol and would love to hear your feedback on it!<br class="">
<br class="">
Rendered | Blog Post<br class="">
<br class="">
Cheers,<br class="">
Vincent<br class="">
<br class="">
Ps: I'd like to thank David Hart (@hartbit) for his great editorial feedback on this proposal. 👍<br class="">
<br class="">
HashVisitable<br class="">
<br class="">
<span class="Apple-tab-span" style="white-space:pre"></span>• Proposal: SE-NNNN<br class="">
<span class="Apple-tab-span" style="white-space:pre"></span>• Authors: Vincent Esche<br class="">
<span class="Apple-tab-span" style="white-space:pre"></span>• Review Manager: TBD<br class="">
<span class="Apple-tab-span" style="white-space:pre"></span>• Status: Awaiting review<br class="">
Introduction<br class="">
<br class="">
Replace the Hashable protocol by two new procotols (Hasher and HashVisitable) to improve safety, versatility and learnability.<br class="">
<br class="">
Motivation<br class="">
<br class="">
Implementing Hashable is difficult and the consequences if not done well have dire performance and safety repercussions.<br class="">
<br class="">
The documentation of Hashable lists a sample implementation of var hashValue:<br class="">
<br class="">
/// A point in an x-y coordinate system.<br class="">
struct GridPoint {<br class="">
var x: Int<br class="">
var y: Int<br class="">
}<br class="">
<br class="">
extension GridPoint: Hashable {<br class="">
var hashValue: Int {<br class="">
return x.hashValue ^ y.hashValue<br class="">
}<br class="">
<br class="">
static func == (lhs: GridPoint, rhs: GridPoint) -> Bool {<br class="">
return lhs.x == rhs.x && lhs.y == rhs.y<br class="">
}<br class="">
}<br class="">
<br class="">
Calculating the hashes of all GridPoints (given the above implementation) on a 1000 × 1000 grid …<br class="">
<br class="">
let (width, height) = (1000, 1000)<br class="">
let total = width * height<br class="">
var hashes = Set<Int>()<br class="">
for x in 0..<width {<br class="">
for y in 0..<height {<br class="">
hashes.insert(GridPoint(x: x, y: y).hashValue)<br class="">
}<br class="">
}<br class="">
print("\(hashes.count) unique hashes out of a total of \(total).")<br class="">
<br class="">
… results in just 1024 unique hash values for 1_000_000 unique values.<br class="">
<br class="">
In other words: The recommended implementation causes 99.9% of values to trigger a hash collision.<br class="">
<br class="">
Out of those 1_000_000 values the median collision count was 976 with min and max being 976 and 1000respectively.<br class="">
<br class="">
The collision rate will have negative impact in algorithms which heavily use hashValue like the ones in Dictionaryand Set. Furthermore, it increases the vulnerability to DDOS attacks when exposed to the web.<br class="">
<br class="">
If even the official Swift documentation gets the implementation of hashValue wrong, then who is to expect the average Swift programmer to do any better?<br class="">
<br class="">
In contrast, running the same snippet using HashVisitable and the semi-secure Fnv1aHash (see below) results in zero collisions!<br class="">
<br class="">
Finally, the design of the Hashable protocol forces the use of one implementation without the possibility of switching between multiple hashing algorithms.<br class="">
<br class="">
Proposed solution<br class="">
<br class="">
Instead of coupling the hashing algorithm with each and every Swift type, we should provide a hashing API based on the visitor-pattern. By freeing application developers from the burden of having to implement hashing algorithms, the Standard Library can provide
default ones which fulfill collision, performance and security goals. Furthermore, it would allow developers to swap to different algorithms based on the use case.<br class="">
<br class="">
Detailed design<br class="">
<br class="">
The proposal deprecates the Hashable protocol and introduces the following two:<br class="">
<br class="">
protocol Hasher<br class="">
{<br class="">
<br class="">
mutating func finish() -> Int<br class="">
<br class="">
<br class="">
mutating func write(bytes<br class="">
: UnsafeRawBufferPointer)<br class="">
}<br class="">
<br class="">
<br class="">
protocol HashVisitable<br class="">
{<br class="">
<br class="">
func hash<H: Hasher>(_ hasher: inout<br class="">
H)<br class="">
}<br class="">
<br class="">
Hasher is the protocol which represents a hashing algorithm, and HashVisitable replaces Hashable. For types entirely represented by their memory layout, the following protocol would provide a default implementation:<br class="">
<br class="">
protocol ContiguouslyHashable: HashVisitable {}<br class="">
<br class="">
extension ContiguouslyHashable {<br class="">
func hash<H: Hasher>(_ hasher: inout H) {<br class="">
var mutableSelf = self<br class="">
try! Swift.withUnsafeBytes(of: &mutableSelf) {<br class="">
hasher.write(bytes: $0)<br class="">
}<br class="">
}<br class="">
}<br class="">
<br class="">
extension Bool : ContiguouslyHashable {}<br class="">
extension UInt8 : ContiguouslyHashable {}<br class="">
extension UInt16 : ContiguouslyHashable {}<br class="">
extension UInt32 : ContiguouslyHashable {}<br class="">
extension UInt64 : ContiguouslyHashable {}<br class="">
extension UInt : ContiguouslyHashable {}<br class="">
extension Int8 : ContiguouslyHashable {}<br class="">
extension Int16 : ContiguouslyHashable {}<br class="">
extension Int32 : ContiguouslyHashable {}<br class="">
extension Int64 : ContiguouslyHashable {}<br class="">
extension Int : ContiguouslyHashable {}<br class="">
<br class="">
The Standard-Library would then provide a set of hashing implementations specific to each purpose. A possible choice for hashing algorithms would be the reasonably fast SipHash-2-4, and the reasonably secure SipHash-4-8.<br class="">
<br class="">
FNV-1A is another popular semi-secure but blazingly fast hash algorithm, which – for the sake of demonstration – could be implemented as follows:<br class="">
<br class="">
struct Fnv1aHash<br class="">
{<br class="">
<br class="">
fileprivate var state: UInt<br class="">
<br class="">
<br class="">
init(seed: UInt<br class="">
) {<br class="">
<br class="">
self.state = seed &+ 14695981039346656037<br class="">
<br class="">
}<br class="">
}<br class="">
<br class="">
<br class="">
extension Fnv1aHash: Hasher <br class="">
{<br class="">
<br class="">
mutating func write(bytes<br class="">
: UnsafeRawBufferPointer) {<br class="">
<br class="">
for byte in<br class="">
bytes {<br class="">
<br class="">
self.state = (self.state ^ UInt(byte)) &* 1099511628211<br class="">
<br class="">
}<br class="">
}<br class="">
<br class="">
mutating func finish() -> Int<br class="">
{<br class="">
<br class="">
return unsafeBitCast(self.state, to: Int.self<br class="">
)<br class="">
}<br class="">
}<br class="">
<br class="">
Coming back to the sample code present in the Hashable documentation, the new implementation would look like:<br class="">
<br class="">
extension GridPoint: HashVisitable <br class="">
{<br class="">
<br class="">
func hash<H: Hasher>(_ hasher: inout<br class="">
H) {<br class="">
<br class="">
self.x.hash(&<br class="">
hasher)<br class="">
<br class="">
self.y.hash(&<br class="">
hasher)<br class="">
}<br class="">
}<br class="">
<br class="">
Source compatibility<br class="">
<br class="">
Making use of "extending protocols to conform to protocols":<br class="">
<br class="">
extension Hashable: HashVisitable <br class="">
{<br class="">
<br class="">
func hash<H: Hasher>(_ hasher: inout<br class="">
H) {<br class="">
<br class="">
self.hashValue.hash(&<br class="">
hasher)<br class="">
}<br class="">
}<br class="">
<br class="">
Effect on ABI stability<br class="">
<br class="">
n/a<br class="">
<br class="">
Effect on API resilience<br class="">
<br class="">
This feature should be possible to add/remove without breaking ABI.<br class="">
<br class="">
Alternatives considered<br class="">
<br class="">
n/a<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote>
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>