servo: Merge #13676 - Remove usage of deprecated `SipHasher` (from servo:no-siphasher); r=pcwalton
authorSimon Sapin <simon.sapin@exyr.org>
Mon, 10 Oct 2016 11:39:06 -0500
changeset 339941 058da893151506587793f4159a4af7b840c41d82
parent 339940 671d3032398a9f25c563bb1f0f00d33b4170b15d
child 339942 66c3a0daf6c6ff3d2b5d79899905a364a0ca36a1
push id86548
push userkwierso@gmail.com
push dateSat, 04 Feb 2017 01:35:21 +0000
treeherdermozilla-inbound@e7b96d015d03 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspcwalton
servo: Merge #13676 - Remove usage of deprecated `SipHasher` (from servo:no-siphasher); r=pcwalton <!-- Please describe your changes on the following line: --> <s>Hashing in `SimpleHashCache` is not randomized anymore. Does this matter?</s> r? @pcwalton --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [x] These changes do not require tests because _____ <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: ef7423bf0034a41b12ba50e623c9b3dba39a53bc
servo/components/style/cache.rs
--- a/servo/components/style/cache.rs
+++ b/servo/components/style/cache.rs
@@ -1,17 +1,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 //! Two simple cache data structures.
 
-use rand;
-use rand::Rng;
-use std::hash::{Hash, Hasher, SipHasher};
+use std::collections::hash_map::RandomState;
+use std::hash::{Hash, Hasher, BuildHasher};
 use std::slice::{Iter, IterMut};
 
 pub struct LRUCache<K, V> {
     entries: Vec<(K, V)>,
     cache_size: usize,
 }
 
 impl<K: PartialEq, V: Clone> LRUCache<K, V> {
@@ -67,38 +66,35 @@ impl<K: PartialEq, V: Clone> LRUCache<K,
 
     pub fn evict_all(&mut self) {
         self.entries.clear();
     }
 }
 
 pub struct SimpleHashCache<K, V> {
     entries: Vec<Option<(K, V)>>,
-    k0: u64,
-    k1: u64,
+    random: RandomState,
 }
 
 impl<K: Clone + Eq + Hash, V: Clone> SimpleHashCache<K, V> {
     pub fn new(cache_size: usize) -> SimpleHashCache<K, V> {
-        let mut r = rand::thread_rng();
         SimpleHashCache {
             entries: vec![None; cache_size],
-            k0: r.gen(),
-            k1: r.gen(),
+            random: RandomState::new(),
         }
     }
 
     #[inline]
     fn to_bucket(&self, h: usize) -> usize {
         h % self.entries.len()
     }
 
     #[inline]
     fn bucket_for_key<Q: Hash>(&self, key: &Q) -> usize {
-        let mut hasher = SipHasher::new_with_keys(self.k0, self.k1);
+        let mut hasher = self.random.build_hasher();
         key.hash(&mut hasher);
         self.to_bucket(hasher.finish() as usize)
     }
 
     pub fn insert(&mut self, key: K, value: V) {
         let bucket_index = self.bucket_for_key(&key);
         self.entries[bucket_index] = Some((key, value));
     }