{"id":15670,"date":"2016-08-25T15:31:31","date_gmt":"2016-08-25T19:31:31","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=15670"},"modified":"2016-08-25T18:58:30","modified_gmt":"2016-08-25T22:58:30","slug":"foundation-hashing","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/08\/25\/foundation-hashing\/","title":{"rendered":"Foundation Hashing"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/tapbot_paul\/status\/768256624423084032\">Paul Haddad<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/tapbot_paul\/status\/768256624423084032\">\n<p>TIL NSData only uses the first 80 bytes to hash itself and doesn&rsquo;t use length at all for some reason.<\/p>\n<\/blockquote>\n<p>Other classes with potentially bad hash codes: <code>NSArray<\/code> and <code>NSDictionary<\/code> both simply return their count. <a href=\"http:\/\/opensource.apple.com\/source\/CF\/CF-1153.18\/CFString.c\">NSString<\/a>&rsquo;s hashing has changed a few times. Currently it uses up to <a href=\"http:\/\/opensource.apple.com\/source\/CF\/CF-1153.18\/CFUtilities.c\">96 characters<\/a> (taken from the beginning, middle, and end) and also incorporates the length.<\/p>\n<p>If you know these hash functions are bad for your data, you can use wrapper objects to override the <code>-hash<\/code> method or an <a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/Cocoa\/Reference\/NSMapTable_class\/\">NSMapTable<\/a> with a custom <a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/Cocoa\/Reference\/Foundation\/Classes\/NSPointerFunctions_Class\/index.html\">NSPointerFunctions<\/a>.<\/p>\n<p>Previously: <a href=\"http:\/\/mjtsai.com\/blog\/2016\/05\/24\/swift-tuples-arent-equatable\/\">Swift Tuples Aren&rsquo;t Equatable<\/a>.<\/p>\n<p>Update (2016-08-25): <a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/768917034377711616\">David<\/a> <a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/768921404741824512\">Smith<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/Catfish_Man\/status\/768917034377711616\"><p>Using the length is a good idea. We [should] change that.<\/p><\/blockquote>\n<blockquote cite=\"https:\/\/twitter.com\/Catfish_Man\/status\/768921404741824512\"><p>Just an oversight afaik. Not all that many people hitting that code path. Should be a safe fix.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Paul Haddad: TIL NSData only uses the first 80 bytes to hash itself and doesn&rsquo;t use length at all for some reason. Other classes with potentially bad hash codes: NSArray and NSDictionary both simply return their count. NSString&rsquo;s hashing has changed a few times. Currently it uses up to 96 characters (taken from the beginning, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[289,69,31,1137,30,1199,138,71],"class_list":["post-15670","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-algorithm","tag-cocoa","tag-ios","tag-ios-9","tag-mac","tag-mac-os-x-10-11","tag-optimization","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=15670"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15670\/revisions"}],"predecessor-version":[{"id":15675,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15670\/revisions\/15675"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=15670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=15670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=15670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}