changeset 177:acc25ac52f89

cache: Make CacheKey an array instead of an expensive vec
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 12 Aug 2017 12:06:15 +0200
parents 2da18faf438b
children 1a085a3214ae
files src/cache.rs src/table_reader.rs
diffstat 2 files changed, 18 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/cache.rs	Sat Aug 12 12:05:17 2017 +0200
+++ b/src/cache.rs	Sat Aug 12 12:06:15 2017 +0200
@@ -150,7 +150,7 @@
     }
 }
 
-pub type CacheKey = Vec<u8>;
+pub type CacheKey = [u8; 16];
 pub type CacheID = u64;
 type CacheEntry<T> = (T, LRUHandle<CacheKey>);
 
@@ -240,15 +240,19 @@
     use super::*;
     use super::LRUList;
 
+    fn make_key(a: u8, b: u8, c: u8) -> CacheKey {
+        [a, b, c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
+    }
+
     #[test]
     fn test_blockcache_cache_add_rm() {
         let mut cache = Cache::new(128);
 
-        let h_123 = "aaa".as_bytes().to_vec();
-        let h_521 = "aab".as_bytes().to_vec();
-        let h_372 = "aac".as_bytes().to_vec();
-        let h_332 = "aad".as_bytes().to_vec();
-        let h_899 = "aae".as_bytes().to_vec();
+        let h_123 = make_key(1, 2, 3);
+        let h_521 = make_key(1, 2, 4);
+        let h_372 = make_key(3, 4, 5);
+        let h_332 = make_key(6, 3, 1);
+        let h_899 = make_key(8, 2, 1);
 
         cache.insert(&h_123, 123);
         cache.insert(&h_332, 332);
@@ -272,11 +276,11 @@
     fn test_blockcache_cache_capacity() {
         let mut cache = Cache::new(3);
 
-        let h_123 = "aaa".as_bytes().to_vec();
-        let h_521 = "aab".as_bytes().to_vec();
-        let h_372 = "aac".as_bytes().to_vec();
-        let h_332 = "aad".as_bytes().to_vec();
-        let h_899 = "aae".as_bytes().to_vec();
+        let h_123 = make_key(1, 2, 3);
+        let h_521 = make_key(1, 2, 4);
+        let h_372 = make_key(3, 4, 5);
+        let h_332 = make_key(6, 3, 1);
+        let h_899 = make_key(8, 2, 1);
 
         cache.insert(&h_123, 123);
         cache.insert(&h_332, 332);
--- a/src/table_reader.rs	Sat Aug 12 12:05:17 2017 +0200
+++ b/src/table_reader.rs	Sat Aug 12 12:06:15 2017 +0200
@@ -137,9 +137,9 @@
     }
 
     fn block_cache_handle(&self, block_off: usize) -> cache::CacheKey {
-        let mut dst = Vec::with_capacity(2 * 8);
-        dst.write_fixedint(self.cache_id).expect("error writing to vec");
-        dst.write_fixedint(block_off as u64).expect("error writing to vec");
+        let mut dst = [0; 2 * 8];
+        (&mut dst[..8]).write_fixedint(self.cache_id).expect("error writing to vec");
+        (&mut dst[8..]).write_fixedint(block_off as u64).expect("error writing to vec");
         dst
     }