kernel: Try an order-independent approach to hashing `dict`.
authorAlberto Gonzalez <boqwxp@airmail.cc>
Mon, 18 May 2020 17:10:01 +0000 (17:10 +0000)
committerAlberto Gonzalez <boqwxp@airmail.cc>
Tue, 19 May 2020 23:32:53 +0000 (23:32 +0000)
Co-Authored-By: David Shah <dave@ds0.me>
Co-Authored-By: Eddie Hung <eddie@fpgeh.com>
kernel/hashlib.h

index 1284f3f8d2ac861233137f6f5db950802a72ac80..5c87b55f537ac3020cb53fa5df57f5234baf0a66 100644 (file)
@@ -617,12 +617,10 @@ public:
        }
 
        unsigned int hash() const {
-               std::vector<entry_t> entries_(entries); //make a copy to preserve const-ness
-               std::sort(entries_.begin(), entries_.end());
                unsigned int h = mkhash_init;
-               for (unsigned int i = 0; i < entries_.size(); ++i) {
-                       h = mkhash(h, hash_ops<K>::hash(entries_[i].udata.first));
-                       h = mkhash(h, hash_ops<T>::hash(entries_[i].udata.second));
+               for (auto &entry : entries) {
+                       h ^= hash_ops<K>::hash(entry.udata.first);
+                       h ^= hash_ops<T>::hash(entry.udata.second);
                }
                return h;
        }