From 461a1a114102df95c217fa0abee3bfb038a6d016 Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 11 Nov 2024 18:27:10 +0000 Subject: [PATCH 01/11] Store keys and encoded values together --- canbench_results.yml | 174 ++++++++++++++++++++-------------------- src/btreemap.rs | 2 + src/btreemap/node.rs | 95 +++++++++++----------- src/btreemap/node/v1.rs | 28 +++---- src/btreemap/node/v2.rs | 22 +++-- 5 files changed, 158 insertions(+), 163 deletions(-) diff --git a/canbench_results.yml b/canbench_results.yml index 76f2bee3..99a3286f 100644 --- a/canbench_results.yml +++ b/canbench_results.yml @@ -1,511 +1,511 @@ benches: btreemap_get_blob_128_1024: total: - instructions: 907603660 + instructions: 906694111 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_128_1024_v2: total: - instructions: 1007199087 + instructions: 997960235 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_16_1024: total: - instructions: 372167770 + instructions: 353773007 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_16_1024_v2: total: - instructions: 457662954 + instructions: 446874806 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_256_1024: total: - instructions: 1426064341 + instructions: 1428323389 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_256_1024_v2: total: - instructions: 1527035040 + instructions: 1516938567 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_32_1024: total: - instructions: 419061339 + instructions: 396952386 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_32_1024_v2: total: - instructions: 504369966 + instructions: 491205527 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_4_1024: total: - instructions: 252116091 + instructions: 239848259 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_4_1024_v2: total: - instructions: 335503348 + instructions: 328638659 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_512_1024: total: - instructions: 2460840276 + instructions: 2462824837 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_512_1024_v2: total: - instructions: 2563001074 + instructions: 2552893992 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_64_1024: total: - instructions: 668073317 + instructions: 657248738 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_64_1024_v2: total: - instructions: 753614013 + instructions: 744402589 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_1024: total: - instructions: 292704842 + instructions: 278797412 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_1024_v2: total: - instructions: 375374739 + instructions: 364240870 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_u64: total: - instructions: 257168736 + instructions: 236664659 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_u64_v2: total: - instructions: 340837593 + instructions: 330146208 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_blob_8: total: - instructions: 238330996 + instructions: 218612079 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_blob_8_v2: total: - instructions: 297681217 + instructions: 287538959 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_u64: total: - instructions: 241234542 + instructions: 221295797 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_u64_v2: total: - instructions: 305672279 + instructions: 295603779 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_insert_10mib_values: total: - instructions: 145812477 + instructions: 145123011 heap_increase: 0 stable_memory_increase: 32 scopes: {} btreemap_insert_blob_1024_128: total: - instructions: 5161897359 + instructions: 5180711486 heap_increase: 0 stable_memory_increase: 262 scopes: {} btreemap_insert_blob_1024_128_v2: total: - instructions: 5551050739 + instructions: 5276407606 heap_increase: 0 stable_memory_increase: 196 scopes: {} btreemap_insert_blob_1024_16: total: - instructions: 5117351375 + instructions: 5139647335 heap_increase: 0 stable_memory_increase: 241 scopes: {} btreemap_insert_blob_1024_16_v2: total: - instructions: 5508484410 + instructions: 5234216429 heap_increase: 0 stable_memory_increase: 181 scopes: {} btreemap_insert_blob_1024_256: total: - instructions: 5184887283 + instructions: 5204144775 heap_increase: 0 stable_memory_increase: 292 scopes: {} btreemap_insert_blob_1024_256_v2: total: - instructions: 5574297530 + instructions: 5302050534 heap_increase: 0 stable_memory_increase: 219 scopes: {} btreemap_insert_blob_1024_32: total: - instructions: 5124591897 + instructions: 5148628538 heap_increase: 0 stable_memory_increase: 239 scopes: {} btreemap_insert_blob_1024_32_v2: total: - instructions: 5514794407 + instructions: 5244865037 heap_increase: 0 stable_memory_increase: 180 scopes: {} btreemap_insert_blob_1024_4: total: - instructions: 5013226971 + instructions: 5038327727 heap_increase: 0 stable_memory_increase: 235 scopes: {} btreemap_insert_blob_1024_4_v2: total: - instructions: 5408556192 + instructions: 5132753325 heap_increase: 0 stable_memory_increase: 176 scopes: {} btreemap_insert_blob_1024_512: total: - instructions: 5296477642 + instructions: 5324251704 heap_increase: 0 stable_memory_increase: 348 scopes: {} btreemap_insert_blob_1024_512_v2: total: - instructions: 5692592790 + instructions: 5427136650 heap_increase: 0 stable_memory_increase: 261 scopes: {} btreemap_insert_blob_1024_64: total: - instructions: 5160666685 + instructions: 5179800034 heap_increase: 0 stable_memory_increase: 250 scopes: {} btreemap_insert_blob_1024_64_v2: total: - instructions: 5550334403 + instructions: 5276074845 heap_increase: 0 stable_memory_increase: 188 scopes: {} btreemap_insert_blob_1024_8: total: - instructions: 5098581170 + instructions: 5121717976 heap_increase: 0 stable_memory_increase: 237 scopes: {} btreemap_insert_blob_1024_8_v2: total: - instructions: 5491442141 + instructions: 5216103708 heap_increase: 0 stable_memory_increase: 178 scopes: {} btreemap_insert_blob_128_1024: total: - instructions: 1521313518 + instructions: 1488425983 heap_increase: 0 stable_memory_increase: 260 scopes: {} btreemap_insert_blob_128_1024_v2: total: - instructions: 1651648946 + instructions: 1584458419 heap_increase: 0 stable_memory_increase: 195 scopes: {} btreemap_insert_blob_16_1024: total: - instructions: 914649153 + instructions: 865965187 heap_increase: 0 stable_memory_increase: 215 scopes: {} btreemap_insert_blob_16_1024_v2: total: - instructions: 1012886554 + instructions: 970069707 heap_increase: 0 stable_memory_increase: 161 scopes: {} btreemap_insert_blob_256_1024: total: - instructions: 2072274900 + instructions: 2054811242 heap_increase: 0 stable_memory_increase: 292 scopes: {} btreemap_insert_blob_256_1024_v2: total: - instructions: 2262444663 + instructions: 2158790238 heap_increase: 0 stable_memory_increase: 219 scopes: {} btreemap_insert_blob_32_1024: total: - instructions: 971181545 + instructions: 915607999 heap_increase: 0 stable_memory_increase: 230 scopes: {} btreemap_insert_blob_32_1024_v2: total: - instructions: 1063753591 + instructions: 1021271136 heap_increase: 0 stable_memory_increase: 173 scopes: {} btreemap_insert_blob_4_1024: total: - instructions: 702280030 + instructions: 667231730 heap_increase: 0 stable_memory_increase: 123 scopes: {} btreemap_insert_blob_4_1024_v2: total: - instructions: 787268408 + instructions: 757328172 heap_increase: 0 stable_memory_increase: 92 scopes: {} btreemap_insert_blob_512_1024: total: - instructions: 3204802664 + instructions: 3186864052 heap_increase: 0 stable_memory_increase: 351 scopes: {} btreemap_insert_blob_512_1024_v2: total: - instructions: 3464684781 + instructions: 3290353099 heap_increase: 0 stable_memory_increase: 263 scopes: {} btreemap_insert_blob_64_1024: total: - instructions: 1242001761 + instructions: 1185840056 heap_increase: 0 stable_memory_increase: 245 scopes: {} btreemap_insert_blob_64_1024_v2: total: - instructions: 1345180958 + instructions: 1295654987 heap_increase: 0 stable_memory_increase: 183 scopes: {} btreemap_insert_blob_8_1024: total: - instructions: 828098541 + instructions: 786880957 heap_increase: 0 stable_memory_increase: 183 scopes: {} btreemap_insert_blob_8_1024_v2: total: - instructions: 919884588 + instructions: 882996105 heap_increase: 0 stable_memory_increase: 138 scopes: {} btreemap_insert_blob_8_u64: total: - instructions: 424320990 + instructions: 376847874 heap_increase: 0 stable_memory_increase: 6 scopes: {} btreemap_insert_blob_8_u64_v2: total: - instructions: 524532367 + instructions: 487626398 heap_increase: 0 stable_memory_increase: 4 scopes: {} btreemap_insert_u64_blob_8: total: - instructions: 435841221 + instructions: 383006732 heap_increase: 0 stable_memory_increase: 7 scopes: {} btreemap_insert_u64_blob_8_v2: total: - instructions: 502367747 + instructions: 460704266 heap_increase: 0 stable_memory_increase: 5 scopes: {} btreemap_insert_u64_u64: total: - instructions: 448311336 + instructions: 395416475 heap_increase: 0 stable_memory_increase: 7 scopes: {} btreemap_insert_u64_u64_v2: total: - instructions: 518585831 + instructions: 476784683 heap_increase: 0 stable_memory_increase: 6 scopes: {} btreemap_iter_count_10mib_values: total: - instructions: 521270 + instructions: 494636 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_iter_count_small_values: total: - instructions: 10242765 + instructions: 8912352 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_128_1024: total: - instructions: 1916023862 + instructions: 1900680561 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_128_1024_v2: total: - instructions: 2060123740 + instructions: 2046463038 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_16_1024: total: - instructions: 1174997981 + instructions: 1132683471 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_16_1024_v2: total: - instructions: 1310740941 + instructions: 1276211171 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_256_1024: total: - instructions: 2563712145 + instructions: 2572076872 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_256_1024_v2: total: - instructions: 2714997748 + instructions: 2717242136 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_32_1024: total: - instructions: 1257954087 + instructions: 1213487272 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_32_1024_v2: total: - instructions: 1396007710 + instructions: 1361149133 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_4_1024: total: - instructions: 730539960 + instructions: 695211669 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_4_1024_v2: total: - instructions: 832319712 + instructions: 806103061 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_512_1024: total: - instructions: 3925463425 + instructions: 3965928745 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_512_1024_v2: total: - instructions: 4079794221 + instructions: 4115180253 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_64_1024: total: - instructions: 1579833528 + instructions: 1542262106 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_64_1024_v2: total: - instructions: 1719825276 + instructions: 1698297437 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_1024: total: - instructions: 958306352 + instructions: 916605928 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_1024_v2: total: - instructions: 1076045855 + instructions: 1044794490 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_u64: total: - instructions: 549451611 + instructions: 503077476 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_u64_v2: total: - instructions: 685586627 + instructions: 652131222 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_blob_8: total: - instructions: 609355138 + instructions: 557575752 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_blob_8_v2: total: - instructions: 705661181 + instructions: 667331888 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_u64: total: - instructions: 634545364 + instructions: 582847509 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_u64_v2: total: - instructions: 739907256 + instructions: 701888902 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -517,13 +517,13 @@ benches: scopes: {} memory_manager_grow: total: - instructions: 352839872 + instructions: 351879867 heap_increase: 2 stable_memory_increase: 32000 scopes: {} memory_manager_overhead: total: - instructions: 1182161127 + instructions: 1182159676 heap_increase: 0 stable_memory_increase: 8320 scopes: {} diff --git a/src/btreemap.rs b/src/btreemap.rs index 716eaf77..d8296ceb 100644 --- a/src/btreemap.rs +++ b/src/btreemap.rs @@ -2216,6 +2216,7 @@ mod test { assert!(!btree.is_empty()); for i in 0..1000u32 { + println!("{}", i); assert_eq!(btree.remove(&b(i.to_le_bytes().as_slice())), Some(b(&[]))); } @@ -2235,6 +2236,7 @@ mod test { assert!(!btree.is_empty()); for i in 0..1000u32 { + println!("{i}"); assert_eq!(btree.pop_first().unwrap().1, b(&i.to_le_bytes())); } diff --git a/src/btreemap/node.rs b/src/btreemap/node.rs index 50f3cbd2..88f36a30 100644 --- a/src/btreemap/node.rs +++ b/src/btreemap/node.rs @@ -49,10 +49,11 @@ pub type Entry = (K, Vec); #[derive(Debug)] pub struct Node { address: Address, - keys: Vec, + // List of tuples consisting of a key and the encoded value. // Values are stored in a Refcell as they are loaded lazily. // A RefCell allows loading the value and caching it without requiring exterior mutability. - encoded_values: RefCell>, + // INVARIANT: the list is sorted by key. + keys_encoded_values: Vec<(K, RefCell)>, // For the key at position I, children[I] points to the left // child of this key and children[I + 1] points to the right child. children: Vec
, @@ -106,9 +107,13 @@ impl Node { pub fn get_max(&self, memory: &M) -> Entry { match self.node_type { NodeType::Leaf => { - let last_idx = self.encoded_values.borrow().len() - 1; + let last_idx = self.keys_encoded_values.len() - 1; ( - self.keys.last().expect("A node can never be empty").clone(), + self.keys_encoded_values + .last() + .expect("A node can never be empty") + .0 + .clone(), self.value(last_idx, memory).to_vec(), ) } @@ -147,7 +152,7 @@ impl Node { /// Returns true if the node cannot store anymore entries, false otherwise. pub fn is_full(&self) -> bool { - self.keys.len() >= CAPACITY + self.keys_encoded_values.len() >= CAPACITY } /// Swaps the entry at index `idx` with the given entry, returning the old entry. @@ -157,24 +162,27 @@ impl Node { (mut key, value): Entry, memory: &M, ) -> Entry { - core::mem::swap(&mut self.keys[idx], &mut key); let old_value = self.value(idx, memory).to_vec(); - self.encoded_values.borrow_mut()[idx] = Value::ByVal(value); + core::mem::swap(&mut self.keys_encoded_values[idx].0, &mut key); + self.keys_encoded_values[idx].1 = RefCell::new(Value::ByVal(value)); (key, old_value) } /// Returns a copy of the entry at the specified index. pub fn entry(&self, idx: usize, memory: &M) -> Entry { - (self.keys[idx].clone(), self.value(idx, memory).to_vec()) + ( + self.keys_encoded_values[idx].0.clone(), + self.value(idx, memory).to_vec(), + ) } /// Returns a reference to the encoded value at the specified index. pub fn value(&self, idx: usize, memory: &M) -> Ref<[u8]> { // Load and cache the value from the underlying memory if needed. { - let mut values = self.encoded_values.borrow_mut(); + let mut value = self.keys_encoded_values[idx].1.borrow_mut(); - if let Value::ByRef(offset) = values[idx] { + if let Value::ByRef(offset) = *value { // Value isn't loaded yet. let reader = NodeReader { address: self.address, @@ -184,17 +192,17 @@ impl Node { }; let value_len = read_u32(&reader, Address::from(offset.get())) as usize; - let mut value = vec![0; value_len]; - reader.read((offset + U32_SIZE).get(), &mut value); + let mut bytes = vec![0; value_len]; + reader.read((offset + U32_SIZE).get(), &mut bytes); // Cache the value internally. - values[idx] = Value::ByVal(value); + *value = Value::ByVal(bytes); } } // Return a reference to the value. - Ref::map(self.encoded_values.borrow(), |values| { - if let Value::ByVal(v) = &values[idx] { + Ref::map(self.keys_encoded_values[idx].1.borrow(), |values| { + if let Value::ByVal(v) = values { &v[..] } else { unreachable!("value must have been loaded already."); @@ -208,7 +216,7 @@ impl Node { /// Returns a reference to the key at the specified index. pub fn key(&self, idx: usize) -> &K { - &self.keys[idx] + &self.keys_encoded_values[idx].0 } /// Returns the child's address at the given index. @@ -243,25 +251,21 @@ impl Node { /// Inserts a new entry at the specified index. pub fn insert_entry(&mut self, idx: usize, (key, encoded_value): Entry) { - self.keys.insert(idx, key); - self.encoded_values - .borrow_mut() - .insert(idx, Value::ByVal(encoded_value)); + self.keys_encoded_values + .insert(idx, (key, RefCell::new(Value::ByVal(encoded_value)))); } /// Removes the entry at the specified index. pub fn remove_entry(&mut self, idx: usize, memory: &M) -> Entry { let value = self.value(idx, memory).to_vec(); - self.encoded_values.borrow_mut().remove(idx); - (self.keys.remove(idx), value) + let (key, _) = self.keys_encoded_values.remove(idx); + (key, value) } /// Adds a new entry at the back of the node. pub fn push_entry(&mut self, (key, encoded_value): Entry) { - self.keys.push(key); - self.encoded_values - .borrow_mut() - .push(Value::ByVal(encoded_value)); + self.keys_encoded_values + .push((key, RefCell::new(Value::ByVal(encoded_value)))); } /// Removes an entry from the back of the node. @@ -271,10 +275,9 @@ impl Node { return None; } - let key = self.keys.pop().expect("node must not be empty"); let last_value = self.value(len - 1, memory).to_vec(); - self.encoded_values - .borrow_mut() + let (key, _) = self + .keys_encoded_values .pop() .expect("node must not be empty"); @@ -313,8 +316,7 @@ impl Node { Self::append(&mut source, self, median); // Move the entries and children into self. - self.keys = core::mem::take(&mut source.keys); - self.encoded_values = core::mem::take(&mut source.encoded_values); + self.keys_encoded_values = core::mem::take(&mut source.keys_encoded_values); self.children = core::mem::take(&mut source.children); } @@ -343,32 +345,31 @@ impl Node { a.push_entry(median); - a.keys.append(&mut b.keys); - a.encoded_values - .borrow_mut() - .append(&mut b.encoded_values.borrow_mut()); + a.keys_encoded_values.append(&mut b.keys_encoded_values); // Move the children (if any exist). a.children.append(&mut b.children); // Assert postconditions. - assert_eq!(b.keys.len(), 0); - assert_eq!(b.encoded_values.borrow().len(), 0); + assert_eq!(b.keys_encoded_values.len(), 0); assert_eq!(b.children.len(), 0); } #[cfg(test)] pub fn entries(&self, memory: &M) -> Vec> { - self.keys + self.keys_encoded_values .iter() - .cloned() - .zip((0..self.keys.len()).map(|idx| self.value(idx, memory).to_vec())) + .enumerate() + .map(|(idx, (key, _))| (key.clone(), self.value(idx, memory).to_vec())) .collect() } #[cfg(test)] - pub fn keys(&self) -> &[K] { - &self.keys + pub fn keys(&self) -> Vec { + self.keys_encoded_values + .iter() + .map(|(key, _)| key.clone()) + .collect() } #[cfg(test)] @@ -378,7 +379,7 @@ impl Node { /// Returns the number of entries in the node. pub fn entries_len(&self) -> usize { - self.keys.len() + self.keys_encoded_values.len() } /// Searches for the key in the node's entries. @@ -388,7 +389,8 @@ impl Node { /// returned, containing the index where a matching key could be inserted /// while maintaining sorted order. pub fn search(&self, key: &K) -> Result { - self.keys.binary_search(key) + self.keys_encoded_values + .binary_search_by_key(&key, |entry| &entry.0) } /// Returns the maximum size a node can be if it has bounded keys and values. @@ -400,7 +402,7 @@ impl Node { /// Returns true if the node is at the minimum required size, false otherwise. pub fn at_minimum(&self) -> bool { - self.keys.len() < B + self.keys_encoded_values.len() < B } /// Returns true if an entry can be removed without having to merge it into another node @@ -419,8 +421,7 @@ impl Node { } // Move the entries and children above the median into the new sibling. - sibling.keys = self.keys.split_off(B); - *sibling.encoded_values.borrow_mut() = self.encoded_values.borrow_mut().split_off(B); + sibling.keys_encoded_values = self.keys_encoded_values.split_off(B); if self.node_type == NodeType::Internal { sibling.children = self.children.split_off(B); } diff --git a/src/btreemap/node/v1.rs b/src/btreemap/node/v1.rs index 6854e72e..9fec2a3d 100644 --- a/src/btreemap/node/v1.rs +++ b/src/btreemap/node/v1.rs @@ -44,8 +44,7 @@ impl Node { Node { address, node_type, - keys: vec![], - encoded_values: RefCell::default(), + keys_encoded_values: vec![], children: vec![], version: Version::V1(page_size), overflows: Vec::with_capacity(0), @@ -64,8 +63,7 @@ impl Node { let _p = canbench::profile("node_load_v1"); // Load the entries. - let mut keys = Vec::with_capacity(header.num_entries as usize); - let mut encoded_values = Vec::with_capacity(header.num_entries as usize); + let mut keys_encoded_values = Vec::with_capacity(header.num_entries as usize); let mut offset = NodeHeader::size(); let mut buf = Vec::with_capacity(max_key_size.max(max_value_size) as usize); for _ in 0..header.num_entries { @@ -78,10 +76,9 @@ impl Node { memory.read((address + offset).get(), &mut buf); offset += Bytes::from(max_key_size); let key = K::from_bytes(Cow::Borrowed(&buf)); - keys.push(key); - // Values are loaded lazily. Store a reference and skip loading it. - encoded_values.push(Value::ByRef(offset)); + keys_encoded_values.push((key, RefCell::new(Value::ByRef(offset)))); + offset += U32_SIZE + Bytes::from(max_value_size); } @@ -95,13 +92,12 @@ impl Node { children.push(child); } - assert_eq!(children.len(), keys.len() + 1); + assert_eq!(children.len(), keys_encoded_values.len() + 1); } Self { address, - keys, - encoded_values: RefCell::new(encoded_values), + keys_encoded_values, children, node_type: match header.node_type { LEAF_NODE_TYPE => NodeType::Leaf, @@ -125,15 +121,15 @@ impl Node { assert!(self.children.is_empty()); } NodeType::Internal => { - assert_eq!(self.children.len(), self.keys.len() + 1); + assert_eq!(self.children.len(), self.keys_encoded_values.len() + 1); } }; // We should never be saving an empty node. - assert!(!self.keys.is_empty() || !self.children.is_empty()); + assert!(!self.keys_encoded_values.is_empty() || !self.children.is_empty()); // Assert entries are sorted in strictly increasing order. - assert!(self.keys.windows(2).all(|e| e[0] < e[1])); + assert!(self.keys_encoded_values.windows(2).all(|e| e[0].0 < e[1].0)); let (max_key_size, max_value_size) = match self.version { Version::V1(DerivedPageSize { @@ -150,7 +146,7 @@ impl Node { NodeType::Leaf => LEAF_NODE_TYPE, NodeType::Internal => INTERNAL_NODE_TYPE, }, - num_entries: self.keys.len() as u16, + num_entries: self.keys_encoded_values.len() as u16, }; write_struct(&header, self.address, memory); @@ -159,12 +155,12 @@ impl Node { // Load all the values. This is necessary so that we don't overwrite referenced // values when writing the entries to the node. - for i in 0..self.keys.len() { + for i in 0..self.keys_encoded_values.len() { self.value(i, memory); } // Write the entries. - for (idx, key) in self.keys.iter().enumerate() { + for (idx, (key, _)) in self.keys_encoded_values.iter().enumerate() { // Write the size of the key. let key_bytes = key.to_bytes_checked(); write_u32(memory, self.address + offset, key_bytes.len() as u32); diff --git a/src/btreemap/node/v2.rs b/src/btreemap/node/v2.rs index 6d9cde7d..02de476e 100644 --- a/src/btreemap/node/v2.rs +++ b/src/btreemap/node/v2.rs @@ -98,8 +98,7 @@ impl Node { address, node_type, version: Version::V2(page_size), - keys: vec![], - encoded_values: RefCell::default(), + keys_encoded_values: vec![], children: vec![], overflows: Vec::with_capacity(0), } @@ -150,8 +149,7 @@ impl Node { } // Load the keys. - let mut keys = Vec::with_capacity(num_entries); - let mut encoded_values = Vec::with_capacity(num_entries); + let mut keys_encoded_values = Vec::with_capacity(num_entries); let mut buf = vec![]; for _ in 0..num_entries { // Load the key's size. @@ -170,21 +168,20 @@ impl Node { reader.read(offset.get(), &mut buf); let key = K::from_bytes(Cow::Borrowed(&buf)); offset += Bytes::from(key_size); - keys.push(key); + keys_encoded_values.push((key, RefCell::new(Value::ByRef(Bytes::from(0usize))))); } // Load the values - for _ in 0..num_entries { + for i in 0..num_entries { // Load the values lazily. - encoded_values.push(Value::ByRef(Bytes::from(offset.get()))); + keys_encoded_values[i].1 = RefCell::new(Value::ByRef(Bytes::from(offset.get()))); let value_size = read_u32(&reader, offset) as usize; offset += U32_SIZE + Bytes::from(value_size as u64); } Self { address, - keys, - encoded_values: RefCell::new(encoded_values), + keys_encoded_values, children, node_type, version: Version::V2(page_size), @@ -199,11 +196,10 @@ impl Node { let page_size = self.version.page_size().get(); assert!(page_size >= MINIMUM_PAGE_SIZE); - assert_eq!(self.keys.len(), self.encoded_values.borrow().len()); // Load all the values. This is necessary so that we don't overwrite referenced // values when writing the entries to the node. - for i in 0..self.keys.len() { + for i in 0..self.keys_encoded_values.len() { self.value(i, allocator.memory()); } @@ -225,7 +221,7 @@ impl Node { NodeType::Leaf => LEAF_NODE_TYPE, NodeType::Internal => INTERNAL_NODE_TYPE, }, - num_entries: self.keys.len() as u16, + num_entries: self.keys_encoded_values.len() as u16, }; writer.write_struct(&header, offset); @@ -243,7 +239,7 @@ impl Node { } // Write the keys. - for key in self.keys.iter() { + for (key, _) in self.keys_encoded_values.iter() { let key_bytes = key.to_bytes_checked(); // Write the size of the key if it isn't fixed in size. From 233d2e9979388f6a4beb0e43772ce9d44c70e4f9 Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 11 Nov 2024 18:51:33 +0000 Subject: [PATCH 02/11] Optimize reusing values --- src/btreemap/node.rs | 60 ++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/btreemap/node.rs b/src/btreemap/node.rs index 88f36a30..ba5a0f61 100644 --- a/src/btreemap/node.rs +++ b/src/btreemap/node.rs @@ -159,13 +159,15 @@ impl Node { pub fn swap_entry( &mut self, idx: usize, - (mut key, value): Entry, + (key, value): Entry, memory: &M, ) -> Entry { - let old_value = self.value(idx, memory).to_vec(); - core::mem::swap(&mut self.keys_encoded_values[idx].0, &mut key); - self.keys_encoded_values[idx].1 = RefCell::new(Value::ByVal(value)); - (key, old_value) + let mut swap_with = (key, RefCell::new(Value::ByVal(value))); + core::mem::swap(&mut self.keys_encoded_values[idx], &mut swap_with); + ( + swap_with.0, + self.resolve_value(RefCell::into_inner(swap_with.1), memory), + ) } /// Returns a copy of the entry at the specified index. @@ -179,10 +181,26 @@ impl Node { /// Returns a reference to the encoded value at the specified index. pub fn value(&self, idx: usize, memory: &M) -> Ref<[u8]> { // Load and cache the value from the underlying memory if needed. - { - let mut value = self.keys_encoded_values[idx].1.borrow_mut(); + let mut value = self.keys_encoded_values[idx].1.borrow_mut(); + if let Value::ByRef(offset) = *value { + // Cache the value internally. + *value = Value::ByVal(self.resolve_value(Value::ByRef(offset), memory)); + } + drop(value); // drop borrow because it's reborrowed below + + // Return a reference to the value. + Ref::map(self.keys_encoded_values[idx].1.borrow(), |values| { + if let Value::ByVal(v) = values { + &v[..] + } else { + unreachable!("value must have been loaded already."); + } + }) + } - if let Value::ByRef(offset) = *value { + fn resolve_value(&self, value: Value, memory: &M) -> Vec { + match value { + Value::ByRef(offset) => { // Value isn't loaded yet. let reader = NodeReader { address: self.address, @@ -195,19 +213,10 @@ impl Node { let mut bytes = vec![0; value_len]; reader.read((offset + U32_SIZE).get(), &mut bytes); - // Cache the value internally. - *value = Value::ByVal(bytes); + bytes } + Value::ByVal(bytes) => bytes, } - - // Return a reference to the value. - Ref::map(self.keys_encoded_values[idx].1.borrow(), |values| { - if let Value::ByVal(v) = values { - &v[..] - } else { - unreachable!("value must have been loaded already."); - } - }) } fn page_size(&self) -> PageSize { @@ -257,9 +266,8 @@ impl Node { /// Removes the entry at the specified index. pub fn remove_entry(&mut self, idx: usize, memory: &M) -> Entry { - let value = self.value(idx, memory).to_vec(); - let (key, _) = self.keys_encoded_values.remove(idx); - (key, value) + let (key, value) = self.keys_encoded_values.remove(idx); + (key, self.resolve_value(RefCell::into_inner(value), memory)) } /// Adds a new entry at the back of the node. @@ -275,13 +283,15 @@ impl Node { return None; } - let last_value = self.value(len - 1, memory).to_vec(); - let (key, _) = self + let (key, last_value) = self .keys_encoded_values .pop() .expect("node must not be empty"); - Some((key, last_value)) + Some(( + key, + self.resolve_value(RefCell::into_inner(last_value), memory), + )) } /// Merges the entries and children of the `source` node into self, along with the median entry. From 9d8f8a9c7807f0b9883324759449094a26a18426 Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 11 Nov 2024 18:52:28 +0000 Subject: [PATCH 03/11] Revert canbench results --- canbench_results.yml | 174 +++++++++++++++++++++---------------------- 1 file changed, 87 insertions(+), 87 deletions(-) diff --git a/canbench_results.yml b/canbench_results.yml index 99a3286f..76f2bee3 100644 --- a/canbench_results.yml +++ b/canbench_results.yml @@ -1,511 +1,511 @@ benches: btreemap_get_blob_128_1024: total: - instructions: 906694111 + instructions: 907603660 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_128_1024_v2: total: - instructions: 997960235 + instructions: 1007199087 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_16_1024: total: - instructions: 353773007 + instructions: 372167770 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_16_1024_v2: total: - instructions: 446874806 + instructions: 457662954 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_256_1024: total: - instructions: 1428323389 + instructions: 1426064341 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_256_1024_v2: total: - instructions: 1516938567 + instructions: 1527035040 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_32_1024: total: - instructions: 396952386 + instructions: 419061339 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_32_1024_v2: total: - instructions: 491205527 + instructions: 504369966 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_4_1024: total: - instructions: 239848259 + instructions: 252116091 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_4_1024_v2: total: - instructions: 328638659 + instructions: 335503348 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_512_1024: total: - instructions: 2462824837 + instructions: 2460840276 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_512_1024_v2: total: - instructions: 2552893992 + instructions: 2563001074 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_64_1024: total: - instructions: 657248738 + instructions: 668073317 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_64_1024_v2: total: - instructions: 744402589 + instructions: 753614013 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_1024: total: - instructions: 278797412 + instructions: 292704842 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_1024_v2: total: - instructions: 364240870 + instructions: 375374739 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_u64: total: - instructions: 236664659 + instructions: 257168736 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_u64_v2: total: - instructions: 330146208 + instructions: 340837593 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_blob_8: total: - instructions: 218612079 + instructions: 238330996 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_blob_8_v2: total: - instructions: 287538959 + instructions: 297681217 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_u64: total: - instructions: 221295797 + instructions: 241234542 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_u64_v2: total: - instructions: 295603779 + instructions: 305672279 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_insert_10mib_values: total: - instructions: 145123011 + instructions: 145812477 heap_increase: 0 stable_memory_increase: 32 scopes: {} btreemap_insert_blob_1024_128: total: - instructions: 5180711486 + instructions: 5161897359 heap_increase: 0 stable_memory_increase: 262 scopes: {} btreemap_insert_blob_1024_128_v2: total: - instructions: 5276407606 + instructions: 5551050739 heap_increase: 0 stable_memory_increase: 196 scopes: {} btreemap_insert_blob_1024_16: total: - instructions: 5139647335 + instructions: 5117351375 heap_increase: 0 stable_memory_increase: 241 scopes: {} btreemap_insert_blob_1024_16_v2: total: - instructions: 5234216429 + instructions: 5508484410 heap_increase: 0 stable_memory_increase: 181 scopes: {} btreemap_insert_blob_1024_256: total: - instructions: 5204144775 + instructions: 5184887283 heap_increase: 0 stable_memory_increase: 292 scopes: {} btreemap_insert_blob_1024_256_v2: total: - instructions: 5302050534 + instructions: 5574297530 heap_increase: 0 stable_memory_increase: 219 scopes: {} btreemap_insert_blob_1024_32: total: - instructions: 5148628538 + instructions: 5124591897 heap_increase: 0 stable_memory_increase: 239 scopes: {} btreemap_insert_blob_1024_32_v2: total: - instructions: 5244865037 + instructions: 5514794407 heap_increase: 0 stable_memory_increase: 180 scopes: {} btreemap_insert_blob_1024_4: total: - instructions: 5038327727 + instructions: 5013226971 heap_increase: 0 stable_memory_increase: 235 scopes: {} btreemap_insert_blob_1024_4_v2: total: - instructions: 5132753325 + instructions: 5408556192 heap_increase: 0 stable_memory_increase: 176 scopes: {} btreemap_insert_blob_1024_512: total: - instructions: 5324251704 + instructions: 5296477642 heap_increase: 0 stable_memory_increase: 348 scopes: {} btreemap_insert_blob_1024_512_v2: total: - instructions: 5427136650 + instructions: 5692592790 heap_increase: 0 stable_memory_increase: 261 scopes: {} btreemap_insert_blob_1024_64: total: - instructions: 5179800034 + instructions: 5160666685 heap_increase: 0 stable_memory_increase: 250 scopes: {} btreemap_insert_blob_1024_64_v2: total: - instructions: 5276074845 + instructions: 5550334403 heap_increase: 0 stable_memory_increase: 188 scopes: {} btreemap_insert_blob_1024_8: total: - instructions: 5121717976 + instructions: 5098581170 heap_increase: 0 stable_memory_increase: 237 scopes: {} btreemap_insert_blob_1024_8_v2: total: - instructions: 5216103708 + instructions: 5491442141 heap_increase: 0 stable_memory_increase: 178 scopes: {} btreemap_insert_blob_128_1024: total: - instructions: 1488425983 + instructions: 1521313518 heap_increase: 0 stable_memory_increase: 260 scopes: {} btreemap_insert_blob_128_1024_v2: total: - instructions: 1584458419 + instructions: 1651648946 heap_increase: 0 stable_memory_increase: 195 scopes: {} btreemap_insert_blob_16_1024: total: - instructions: 865965187 + instructions: 914649153 heap_increase: 0 stable_memory_increase: 215 scopes: {} btreemap_insert_blob_16_1024_v2: total: - instructions: 970069707 + instructions: 1012886554 heap_increase: 0 stable_memory_increase: 161 scopes: {} btreemap_insert_blob_256_1024: total: - instructions: 2054811242 + instructions: 2072274900 heap_increase: 0 stable_memory_increase: 292 scopes: {} btreemap_insert_blob_256_1024_v2: total: - instructions: 2158790238 + instructions: 2262444663 heap_increase: 0 stable_memory_increase: 219 scopes: {} btreemap_insert_blob_32_1024: total: - instructions: 915607999 + instructions: 971181545 heap_increase: 0 stable_memory_increase: 230 scopes: {} btreemap_insert_blob_32_1024_v2: total: - instructions: 1021271136 + instructions: 1063753591 heap_increase: 0 stable_memory_increase: 173 scopes: {} btreemap_insert_blob_4_1024: total: - instructions: 667231730 + instructions: 702280030 heap_increase: 0 stable_memory_increase: 123 scopes: {} btreemap_insert_blob_4_1024_v2: total: - instructions: 757328172 + instructions: 787268408 heap_increase: 0 stable_memory_increase: 92 scopes: {} btreemap_insert_blob_512_1024: total: - instructions: 3186864052 + instructions: 3204802664 heap_increase: 0 stable_memory_increase: 351 scopes: {} btreemap_insert_blob_512_1024_v2: total: - instructions: 3290353099 + instructions: 3464684781 heap_increase: 0 stable_memory_increase: 263 scopes: {} btreemap_insert_blob_64_1024: total: - instructions: 1185840056 + instructions: 1242001761 heap_increase: 0 stable_memory_increase: 245 scopes: {} btreemap_insert_blob_64_1024_v2: total: - instructions: 1295654987 + instructions: 1345180958 heap_increase: 0 stable_memory_increase: 183 scopes: {} btreemap_insert_blob_8_1024: total: - instructions: 786880957 + instructions: 828098541 heap_increase: 0 stable_memory_increase: 183 scopes: {} btreemap_insert_blob_8_1024_v2: total: - instructions: 882996105 + instructions: 919884588 heap_increase: 0 stable_memory_increase: 138 scopes: {} btreemap_insert_blob_8_u64: total: - instructions: 376847874 + instructions: 424320990 heap_increase: 0 stable_memory_increase: 6 scopes: {} btreemap_insert_blob_8_u64_v2: total: - instructions: 487626398 + instructions: 524532367 heap_increase: 0 stable_memory_increase: 4 scopes: {} btreemap_insert_u64_blob_8: total: - instructions: 383006732 + instructions: 435841221 heap_increase: 0 stable_memory_increase: 7 scopes: {} btreemap_insert_u64_blob_8_v2: total: - instructions: 460704266 + instructions: 502367747 heap_increase: 0 stable_memory_increase: 5 scopes: {} btreemap_insert_u64_u64: total: - instructions: 395416475 + instructions: 448311336 heap_increase: 0 stable_memory_increase: 7 scopes: {} btreemap_insert_u64_u64_v2: total: - instructions: 476784683 + instructions: 518585831 heap_increase: 0 stable_memory_increase: 6 scopes: {} btreemap_iter_count_10mib_values: total: - instructions: 494636 + instructions: 521270 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_iter_count_small_values: total: - instructions: 8912352 + instructions: 10242765 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_128_1024: total: - instructions: 1900680561 + instructions: 1916023862 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_128_1024_v2: total: - instructions: 2046463038 + instructions: 2060123740 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_16_1024: total: - instructions: 1132683471 + instructions: 1174997981 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_16_1024_v2: total: - instructions: 1276211171 + instructions: 1310740941 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_256_1024: total: - instructions: 2572076872 + instructions: 2563712145 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_256_1024_v2: total: - instructions: 2717242136 + instructions: 2714997748 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_32_1024: total: - instructions: 1213487272 + instructions: 1257954087 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_32_1024_v2: total: - instructions: 1361149133 + instructions: 1396007710 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_4_1024: total: - instructions: 695211669 + instructions: 730539960 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_4_1024_v2: total: - instructions: 806103061 + instructions: 832319712 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_512_1024: total: - instructions: 3965928745 + instructions: 3925463425 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_512_1024_v2: total: - instructions: 4115180253 + instructions: 4079794221 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_64_1024: total: - instructions: 1542262106 + instructions: 1579833528 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_64_1024_v2: total: - instructions: 1698297437 + instructions: 1719825276 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_1024: total: - instructions: 916605928 + instructions: 958306352 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_1024_v2: total: - instructions: 1044794490 + instructions: 1076045855 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_u64: total: - instructions: 503077476 + instructions: 549451611 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_u64_v2: total: - instructions: 652131222 + instructions: 685586627 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_blob_8: total: - instructions: 557575752 + instructions: 609355138 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_blob_8_v2: total: - instructions: 667331888 + instructions: 705661181 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_u64: total: - instructions: 582847509 + instructions: 634545364 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_u64_v2: total: - instructions: 701888902 + instructions: 739907256 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -517,13 +517,13 @@ benches: scopes: {} memory_manager_grow: total: - instructions: 351879867 + instructions: 352839872 heap_increase: 2 stable_memory_increase: 32000 scopes: {} memory_manager_overhead: total: - instructions: 1182159676 + instructions: 1182161127 heap_increase: 0 stable_memory_increase: 8320 scopes: {} From eaecab7bec4e72a6eba4df97eb04f359e926cacf Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 11 Nov 2024 18:57:45 +0000 Subject: [PATCH 04/11] Remove printfs --- src/btreemap.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/btreemap.rs b/src/btreemap.rs index d8296ceb..716eaf77 100644 --- a/src/btreemap.rs +++ b/src/btreemap.rs @@ -2216,7 +2216,6 @@ mod test { assert!(!btree.is_empty()); for i in 0..1000u32 { - println!("{}", i); assert_eq!(btree.remove(&b(i.to_le_bytes().as_slice())), Some(b(&[]))); } @@ -2236,7 +2235,6 @@ mod test { assert!(!btree.is_empty()); for i in 0..1000u32 { - println!("{i}"); assert_eq!(btree.pop_first().unwrap().1, b(&i.to_le_bytes())); } From 24df5734e7d8d44217f3442782fac39f587db604 Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 11 Nov 2024 19:03:00 +0000 Subject: [PATCH 05/11] Clippy --- src/btreemap/node/v2.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/btreemap/node/v2.rs b/src/btreemap/node/v2.rs index 02de476e..e346b1f9 100644 --- a/src/btreemap/node/v2.rs +++ b/src/btreemap/node/v2.rs @@ -172,9 +172,9 @@ impl Node { } // Load the values - for i in 0..num_entries { + for (_key, value) in keys_encoded_values.iter_mut() { // Load the values lazily. - keys_encoded_values[i].1 = RefCell::new(Value::ByRef(Bytes::from(offset.get()))); + *value = RefCell::new(Value::ByRef(Bytes::from(offset.get()))); let value_size = read_u32(&reader, offset) as usize; offset += U32_SIZE + Bytes::from(value_size as u64); } From 3a21d7834f91875ae25490a47f6525ef12d74188 Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 11 Nov 2024 19:23:27 +0000 Subject: [PATCH 06/11] Update canbench_results.yml --- canbench_results.yml | 174 +++++++++++++++++++++---------------------- 1 file changed, 87 insertions(+), 87 deletions(-) diff --git a/canbench_results.yml b/canbench_results.yml index 76f2bee3..4ae935dd 100644 --- a/canbench_results.yml +++ b/canbench_results.yml @@ -1,511 +1,511 @@ benches: btreemap_get_blob_128_1024: total: - instructions: 907603660 + instructions: 906595437 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_128_1024_v2: total: - instructions: 1007199087 + instructions: 995007277 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_16_1024: total: - instructions: 372167770 + instructions: 353674291 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_16_1024_v2: total: - instructions: 457662954 + instructions: 444017154 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_256_1024: total: - instructions: 1426064341 + instructions: 1428224730 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_256_1024_v2: total: - instructions: 1527035040 + instructions: 1513968864 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_32_1024: total: - instructions: 419061339 + instructions: 396853702 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_32_1024_v2: total: - instructions: 504369966 + instructions: 488287509 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_4_1024: total: - instructions: 252116091 + instructions: 239759751 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_4_1024_v2: total: - instructions: 335503348 + instructions: 325669813 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_512_1024: total: - instructions: 2460840276 + instructions: 2462726180 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_512_1024_v2: total: - instructions: 2563001074 + instructions: 2549921993 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_64_1024: total: - instructions: 668073317 + instructions: 657150062 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_64_1024_v2: total: - instructions: 753614013 + instructions: 741435587 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_1024: total: - instructions: 292704842 + instructions: 278698743 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_1024_v2: total: - instructions: 375374739 + instructions: 361455987 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_u64: total: - instructions: 257168736 + instructions: 236566066 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_u64_v2: total: - instructions: 340837593 + instructions: 327341207 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_blob_8: total: - instructions: 238330996 + instructions: 218513418 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_blob_8_v2: total: - instructions: 297681217 + instructions: 284553618 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_u64: total: - instructions: 241234542 + instructions: 221197138 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_u64_v2: total: - instructions: 305672279 + instructions: 292625246 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_insert_10mib_values: total: - instructions: 145812477 + instructions: 143608336 heap_increase: 0 stable_memory_increase: 32 scopes: {} btreemap_insert_blob_1024_128: total: - instructions: 5161897359 + instructions: 5186688498 heap_increase: 0 stable_memory_increase: 262 scopes: {} btreemap_insert_blob_1024_128_v2: total: - instructions: 5551050739 + instructions: 5279083743 heap_increase: 0 stable_memory_increase: 196 scopes: {} btreemap_insert_blob_1024_16: total: - instructions: 5117351375 + instructions: 5145553451 heap_increase: 0 stable_memory_increase: 241 scopes: {} btreemap_insert_blob_1024_16_v2: total: - instructions: 5508484410 + instructions: 5237156731 heap_increase: 0 stable_memory_increase: 181 scopes: {} btreemap_insert_blob_1024_256: total: - instructions: 5184887283 + instructions: 5209366961 heap_increase: 0 stable_memory_increase: 292 scopes: {} btreemap_insert_blob_1024_256_v2: total: - instructions: 5574297530 + instructions: 5305072336 heap_increase: 0 stable_memory_increase: 219 scopes: {} btreemap_insert_blob_1024_32: total: - instructions: 5124591897 + instructions: 5154385858 heap_increase: 0 stable_memory_increase: 239 scopes: {} btreemap_insert_blob_1024_32_v2: total: - instructions: 5514794407 + instructions: 5247630219 heap_increase: 0 stable_memory_increase: 180 scopes: {} btreemap_insert_blob_1024_4: total: - instructions: 5013226971 + instructions: 5044387011 heap_increase: 0 stable_memory_increase: 235 scopes: {} btreemap_insert_blob_1024_4_v2: total: - instructions: 5408556192 + instructions: 5135872130 heap_increase: 0 stable_memory_increase: 176 scopes: {} btreemap_insert_blob_1024_512: total: - instructions: 5296477642 + instructions: 5327760616 heap_increase: 0 stable_memory_increase: 348 scopes: {} btreemap_insert_blob_1024_512_v2: total: - instructions: 5692592790 + instructions: 5428420681 heap_increase: 0 stable_memory_increase: 261 scopes: {} btreemap_insert_blob_1024_64: total: - instructions: 5160666685 + instructions: 5185986998 heap_increase: 0 stable_memory_increase: 250 scopes: {} btreemap_insert_blob_1024_64_v2: total: - instructions: 5550334403 + instructions: 5278954984 heap_increase: 0 stable_memory_increase: 188 scopes: {} btreemap_insert_blob_1024_8: total: - instructions: 5098581170 + instructions: 5127709740 heap_increase: 0 stable_memory_increase: 237 scopes: {} btreemap_insert_blob_1024_8_v2: total: - instructions: 5491442141 + instructions: 5219145274 heap_increase: 0 stable_memory_increase: 178 scopes: {} btreemap_insert_blob_128_1024: total: - instructions: 1521313518 + instructions: 1485358283 heap_increase: 0 stable_memory_increase: 260 scopes: {} btreemap_insert_blob_128_1024_v2: total: - instructions: 1651648946 + instructions: 1578791645 heap_increase: 0 stable_memory_increase: 195 scopes: {} btreemap_insert_blob_16_1024: total: - instructions: 914649153 + instructions: 860340784 heap_increase: 0 stable_memory_increase: 215 scopes: {} btreemap_insert_blob_16_1024_v2: total: - instructions: 1012886554 + instructions: 962743559 heap_increase: 0 stable_memory_increase: 161 scopes: {} btreemap_insert_blob_256_1024: total: - instructions: 2072274900 + instructions: 2052265958 heap_increase: 0 stable_memory_increase: 292 scopes: {} btreemap_insert_blob_256_1024_v2: total: - instructions: 2262444663 + instructions: 2153564053 heap_increase: 0 stable_memory_increase: 219 scopes: {} btreemap_insert_blob_32_1024: total: - instructions: 971181545 + instructions: 911459404 heap_increase: 0 stable_memory_increase: 230 scopes: {} btreemap_insert_blob_32_1024_v2: total: - instructions: 1063753591 + instructions: 1014772174 heap_increase: 0 stable_memory_increase: 173 scopes: {} btreemap_insert_blob_4_1024: total: - instructions: 702280030 + instructions: 652196411 heap_increase: 0 stable_memory_increase: 123 scopes: {} btreemap_insert_blob_4_1024_v2: total: - instructions: 787268408 + instructions: 744572800 heap_increase: 0 stable_memory_increase: 92 scopes: {} btreemap_insert_blob_512_1024: total: - instructions: 3204802664 + instructions: 3185092358 heap_increase: 0 stable_memory_increase: 351 scopes: {} btreemap_insert_blob_512_1024_v2: total: - instructions: 3464684781 + instructions: 3285828737 heap_increase: 0 stable_memory_increase: 263 scopes: {} btreemap_insert_blob_64_1024: total: - instructions: 1242001761 + instructions: 1182582965 heap_increase: 0 stable_memory_increase: 245 scopes: {} btreemap_insert_blob_64_1024_v2: total: - instructions: 1345180958 + instructions: 1289748375 heap_increase: 0 stable_memory_increase: 183 scopes: {} btreemap_insert_blob_8_1024: total: - instructions: 828098541 + instructions: 778239701 heap_increase: 0 stable_memory_increase: 183 scopes: {} btreemap_insert_blob_8_1024_v2: total: - instructions: 919884588 + instructions: 873351784 heap_increase: 0 stable_memory_increase: 138 scopes: {} btreemap_insert_blob_8_u64: total: - instructions: 424320990 + instructions: 374264701 heap_increase: 0 stable_memory_increase: 6 scopes: {} btreemap_insert_blob_8_u64_v2: total: - instructions: 524532367 + instructions: 480961397 heap_increase: 0 stable_memory_increase: 4 scopes: {} btreemap_insert_u64_blob_8: total: - instructions: 435841221 + instructions: 383086894 heap_increase: 0 stable_memory_increase: 7 scopes: {} btreemap_insert_u64_blob_8_v2: total: - instructions: 502367747 + instructions: 457868460 heap_increase: 0 stable_memory_increase: 5 scopes: {} btreemap_insert_u64_u64: total: - instructions: 448311336 + instructions: 395163537 heap_increase: 0 stable_memory_increase: 7 scopes: {} btreemap_insert_u64_u64_v2: total: - instructions: 518585831 + instructions: 473740540 heap_increase: 0 stable_memory_increase: 6 scopes: {} btreemap_iter_count_10mib_values: total: - instructions: 521270 + instructions: 492753 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_iter_count_small_values: total: - instructions: 10242765 + instructions: 8818343 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_128_1024: total: - instructions: 1916023862 + instructions: 1858400550 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_128_1024_v2: total: - instructions: 2060123740 + instructions: 2006246469 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_16_1024: total: - instructions: 1174997981 + instructions: 1076917085 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_16_1024_v2: total: - instructions: 1310740941 + instructions: 1223417835 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_256_1024: total: - instructions: 2563712145 + instructions: 2525062413 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_256_1024_v2: total: - instructions: 2714997748 + instructions: 2673313410 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_32_1024: total: - instructions: 1257954087 + instructions: 1157049968 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_32_1024_v2: total: - instructions: 1396007710 + instructions: 1307825290 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_4_1024: total: - instructions: 730539960 + instructions: 660258894 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_4_1024_v2: total: - instructions: 832319712 + instructions: 772178116 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_512_1024: total: - instructions: 3925463425 + instructions: 3931888044 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_512_1024_v2: total: - instructions: 4079794221 + instructions: 4082800310 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_64_1024: total: - instructions: 1579833528 + instructions: 1494553436 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_64_1024_v2: total: - instructions: 1719825276 + instructions: 1653016159 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_1024: total: - instructions: 958306352 + instructions: 869160446 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_1024_v2: total: - instructions: 1076045855 + instructions: 999613951 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_u64: total: - instructions: 549451611 + instructions: 490651656 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_u64_v2: total: - instructions: 685586627 + instructions: 640022598 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_blob_8: total: - instructions: 609355138 + instructions: 545563140 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_blob_8_v2: total: - instructions: 705661181 + instructions: 655134199 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_u64: total: - instructions: 634545364 + instructions: 567125956 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_u64_v2: total: - instructions: 739907256 + instructions: 686106415 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -517,13 +517,13 @@ benches: scopes: {} memory_manager_grow: total: - instructions: 352839872 + instructions: 351879867 heap_increase: 2 stable_memory_increase: 32000 scopes: {} memory_manager_overhead: total: - instructions: 1182161127 + instructions: 1182159676 heap_increase: 0 stable_memory_increase: 8320 scopes: {} From 80b1f58995cc9f2fde80d7e57e0a3fcb4ee4e366 Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 11 Nov 2024 19:30:07 +0000 Subject: [PATCH 07/11] Update canbench_results.yml --- canbench_results.yml | 170 +++++++++++++++++++++---------------------- 1 file changed, 85 insertions(+), 85 deletions(-) diff --git a/canbench_results.yml b/canbench_results.yml index 62a7066d..a7fcbdc8 100644 --- a/canbench_results.yml +++ b/canbench_results.yml @@ -1,511 +1,511 @@ benches: btreemap_get_blob_128_1024: total: - instructions: 907603660 + instructions: 906595437 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_128_1024_v2: total: - instructions: 1007199087 + instructions: 995007277 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_16_1024: total: - instructions: 372167770 + instructions: 353674291 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_16_1024_v2: total: - instructions: 457662954 + instructions: 444017154 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_256_1024: total: - instructions: 1426064341 + instructions: 1428224730 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_256_1024_v2: total: - instructions: 1527035040 + instructions: 1513968864 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_32_1024: total: - instructions: 419061339 + instructions: 396853702 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_32_1024_v2: total: - instructions: 504369966 + instructions: 488287509 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_4_1024: total: - instructions: 252116091 + instructions: 239759751 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_4_1024_v2: total: - instructions: 335503348 + instructions: 325669813 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_512_1024: total: - instructions: 2460840276 + instructions: 2462726180 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_512_1024_v2: total: - instructions: 2563001074 + instructions: 2549921993 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_64_1024: total: - instructions: 668073317 + instructions: 657150062 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_64_1024_v2: total: - instructions: 753614013 + instructions: 741435587 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_1024: total: - instructions: 292704842 + instructions: 278698743 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_1024_v2: total: - instructions: 375374739 + instructions: 361455987 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_u64: total: - instructions: 257168736 + instructions: 236566066 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_blob_8_u64_v2: total: - instructions: 340837593 + instructions: 327341207 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_blob_8: total: - instructions: 238330996 + instructions: 218513418 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_blob_8_v2: total: - instructions: 297681217 + instructions: 284553618 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_u64: total: - instructions: 241234542 + instructions: 221197138 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_get_u64_u64_v2: total: - instructions: 305672279 + instructions: 292625246 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_insert_10mib_values: total: - instructions: 145922325 + instructions: 143718184 heap_increase: 0 stable_memory_increase: 32 scopes: {} btreemap_insert_blob_1024_128: total: - instructions: 5161914701 + instructions: 5186705840 heap_increase: 0 stable_memory_increase: 262 scopes: {} btreemap_insert_blob_1024_128_v2: total: - instructions: 5551068081 + instructions: 5279101085 heap_increase: 0 stable_memory_increase: 196 scopes: {} btreemap_insert_blob_1024_16: total: - instructions: 5117369042 + instructions: 5145571118 heap_increase: 0 stable_memory_increase: 241 scopes: {} btreemap_insert_blob_1024_16_v2: total: - instructions: 5508502115 + instructions: 5237174436 heap_increase: 0 stable_memory_increase: 181 scopes: {} btreemap_insert_blob_1024_256: total: - instructions: 5184904755 + instructions: 5209384433 heap_increase: 0 stable_memory_increase: 292 scopes: {} btreemap_insert_blob_1024_256_v2: total: - instructions: 5574315002 + instructions: 5305089808 heap_increase: 0 stable_memory_increase: 219 scopes: {} btreemap_insert_blob_1024_32: total: - instructions: 5124609174 + instructions: 5154403135 heap_increase: 0 stable_memory_increase: 239 scopes: {} btreemap_insert_blob_1024_32_v2: total: - instructions: 5514811773 + instructions: 5247647585 heap_increase: 0 stable_memory_increase: 180 scopes: {} btreemap_insert_blob_1024_4: total: - instructions: 5013244404 + instructions: 5044404444 heap_increase: 0 stable_memory_increase: 235 scopes: {} btreemap_insert_blob_1024_4_v2: total: - instructions: 5408573739 + instructions: 5135889677 heap_increase: 0 stable_memory_increase: 176 scopes: {} btreemap_insert_blob_1024_512: total: - instructions: 5296495023 + instructions: 5327777997 heap_increase: 0 stable_memory_increase: 348 scopes: {} btreemap_insert_blob_1024_512_v2: total: - instructions: 5692610171 + instructions: 5428438062 heap_increase: 0 stable_memory_increase: 261 scopes: {} btreemap_insert_blob_1024_64: total: - instructions: 5160684248 + instructions: 5186004561 heap_increase: 0 stable_memory_increase: 250 scopes: {} btreemap_insert_blob_1024_64_v2: total: - instructions: 5550351966 + instructions: 5278972547 heap_increase: 0 stable_memory_increase: 188 scopes: {} btreemap_insert_blob_1024_8: total: - instructions: 5098598668 + instructions: 5127727238 heap_increase: 0 stable_memory_increase: 237 scopes: {} btreemap_insert_blob_1024_8_v2: total: - instructions: 5491459791 + instructions: 5219162924 heap_increase: 0 stable_memory_increase: 178 scopes: {} btreemap_insert_blob_128_1024: total: - instructions: 1521330782 + instructions: 1485375547 heap_increase: 0 stable_memory_increase: 260 scopes: {} btreemap_insert_blob_128_1024_v2: total: - instructions: 1651666210 + instructions: 1578808909 heap_increase: 0 stable_memory_increase: 195 scopes: {} btreemap_insert_blob_16_1024: total: - instructions: 914664896 + instructions: 860356527 heap_increase: 0 stable_memory_increase: 215 scopes: {} btreemap_insert_blob_16_1024_v2: total: - instructions: 1012902411 + instructions: 962759416 heap_increase: 0 stable_memory_increase: 161 scopes: {} btreemap_insert_blob_256_1024: total: - instructions: 2072292372 + instructions: 2052283430 heap_increase: 0 stable_memory_increase: 292 scopes: {} btreemap_insert_blob_256_1024_v2: total: - instructions: 2262462135 + instructions: 2153581525 heap_increase: 0 stable_memory_increase: 219 scopes: {} btreemap_insert_blob_32_1024: total: - instructions: 971198185 + instructions: 911476044 heap_increase: 0 stable_memory_increase: 230 scopes: {} btreemap_insert_blob_32_1024_v2: total: - instructions: 1063770231 + instructions: 1014788814 heap_increase: 0 stable_memory_increase: 173 scopes: {} btreemap_insert_blob_4_1024: total: - instructions: 702289156 + instructions: 652205537 heap_increase: 0 stable_memory_increase: 123 scopes: {} btreemap_insert_blob_4_1024_v2: total: - instructions: 787277534 + instructions: 744581926 heap_increase: 0 stable_memory_increase: 92 scopes: {} btreemap_insert_blob_512_1024: total: - instructions: 3204820188 + instructions: 3185109882 heap_increase: 0 stable_memory_increase: 351 scopes: {} btreemap_insert_blob_512_1024_v2: total: - instructions: 3464702305 + instructions: 3285846261 heap_increase: 0 stable_memory_increase: 263 scopes: {} btreemap_insert_blob_64_1024: total: - instructions: 1242018908 + instructions: 1182600112 heap_increase: 0 stable_memory_increase: 245 scopes: {} btreemap_insert_blob_64_1024_v2: total: - instructions: 1345198105 + instructions: 1289765522 heap_increase: 0 stable_memory_increase: 183 scopes: {} btreemap_insert_blob_8_1024: total: - instructions: 828112100 + instructions: 778253260 heap_increase: 0 stable_memory_increase: 183 scopes: {} btreemap_insert_blob_8_1024_v2: total: - instructions: 919898223 + instructions: 873365419 heap_increase: 0 stable_memory_increase: 138 scopes: {} btreemap_insert_blob_8_u64: total: - instructions: 424334575 + instructions: 374278286 heap_increase: 0 stable_memory_increase: 6 scopes: {} btreemap_insert_blob_8_u64_v2: total: - instructions: 524551164 + instructions: 480980194 heap_increase: 0 stable_memory_increase: 4 scopes: {} btreemap_insert_u64_blob_8: total: - instructions: 435858693 + instructions: 383104366 heap_increase: 0 stable_memory_increase: 7 scopes: {} btreemap_insert_u64_blob_8_v2: total: - instructions: 502390007 + instructions: 457890720 heap_increase: 0 stable_memory_increase: 5 scopes: {} btreemap_insert_u64_u64: total: - instructions: 448328834 + instructions: 395181035 heap_increase: 0 stable_memory_increase: 7 scopes: {} btreemap_insert_u64_u64_v2: total: - instructions: 518609796 + instructions: 473764505 heap_increase: 0 stable_memory_increase: 6 scopes: {} btreemap_iter_count_10mib_values: total: - instructions: 525036 + instructions: 497697 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_iter_count_small_values: total: - instructions: 10458516 + instructions: 9092132 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_128_1024: total: - instructions: 1916049094 + instructions: 1858425782 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_128_1024_v2: total: - instructions: 2060148972 + instructions: 2006271701 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_16_1024: total: - instructions: 1175020990 + instructions: 1076940094 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_16_1024_v2: total: - instructions: 1310763950 + instructions: 1223440844 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_256_1024: total: - instructions: 2563737681 + instructions: 2525087949 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_256_1024_v2: total: - instructions: 2715023284 + instructions: 2673338946 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_32_1024: total: - instructions: 1257978407 + instructions: 1157074288 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_32_1024_v2: total: - instructions: 1396032068 + instructions: 1307849648 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_4_1024: total: - instructions: 730553298 + instructions: 660272232 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_4_1024_v2: total: - instructions: 832333088 + instructions: 772191492 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_512_1024: total: - instructions: 3925489037 + instructions: 3931913656 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_512_1024_v2: total: - instructions: 4079819871 + instructions: 4082825960 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_64_1024: total: - instructions: 1579858589 + instructions: 1494578497 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_64_1024_v2: total: - instructions: 1719850337 + instructions: 1653041220 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_1024: total: - instructions: 958326169 + instructions: 869180263 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_1024_v2: total: - instructions: 1076065710 + instructions: 999633806 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_u64: total: - instructions: 549471466 + instructions: 490671511 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_blob_8_u64_v2: total: - instructions: 685613050 + instructions: 640049021 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_blob_8: total: - instructions: 609380674 + instructions: 545588676 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_blob_8_v2: total: - instructions: 705692037 + instructions: 655165055 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_u64: total: - instructions: 634570938 + instructions: 567151530 heap_increase: 0 stable_memory_increase: 0 scopes: {} btreemap_remove_u64_u64_v2: total: - instructions: 739939423 + instructions: 686138582 heap_increase: 0 stable_memory_increase: 0 scopes: {} From 00e920f168b329692063714de0ced5d1024f74b9 Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 11 Nov 2024 19:55:14 +0000 Subject: [PATCH 08/11] fix: Fix args in post-comment action --- .github/workflows/canbench-post-comment.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/canbench-post-comment.yml b/.github/workflows/canbench-post-comment.yml index 8583f4ab..09379f47 100644 --- a/.github/workflows/canbench-post-comment.yml +++ b/.github/workflows/canbench-post-comment.yml @@ -33,6 +33,6 @@ jobs: with: message: | ${{ matrix.benchmark.result }} - comment_tag: ${{ matrix.benchmark.title }} - pr_number: ${{ needs.download-results.outputs.pr_number }} + comment-tag: ${{ matrix.benchmark.title }} + pr-number: ${{ needs.download-results.outputs.pr_number }} From 2edccaff701725dd9ad25f68fca96d7035fb6b29 Mon Sep 17 00:00:00 2001 From: David Frank Date: Tue, 12 Nov 2024 14:44:45 +0000 Subject: [PATCH 09/11] opt: Replace BTreeMap with constant vector in MemoryManager Since we can have maximum 255 values, we can store those cheaply in a Vec and save time during the lookup. --- canbench_results.yml | 86 ++++++++++++++++++++--------------------- src/btreemap/node/v1.rs | 1 + src/memory_manager.rs | 50 ++++++++++-------------- 3 files changed, 64 insertions(+), 73 deletions(-) diff --git a/canbench_results.yml b/canbench_results.yml index a7fcbdc8..faf64774 100644 --- a/canbench_results.yml +++ b/canbench_results.yml @@ -1,7 +1,7 @@ benches: btreemap_get_blob_128_1024: total: - instructions: 906595437 + instructions: 893470460 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -13,7 +13,7 @@ benches: scopes: {} btreemap_get_blob_16_1024: total: - instructions: 353674291 + instructions: 337448259 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -25,7 +25,7 @@ benches: scopes: {} btreemap_get_blob_256_1024: total: - instructions: 1428224730 + instructions: 1415311791 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -37,7 +37,7 @@ benches: scopes: {} btreemap_get_blob_32_1024: total: - instructions: 396853702 + instructions: 381707704 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -49,7 +49,7 @@ benches: scopes: {} btreemap_get_blob_4_1024: total: - instructions: 239759751 + instructions: 227061208 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -61,7 +61,7 @@ benches: scopes: {} btreemap_get_blob_512_1024: total: - instructions: 2462726180 + instructions: 2449915207 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -73,7 +73,7 @@ benches: scopes: {} btreemap_get_blob_64_1024: total: - instructions: 657150062 + instructions: 627911472 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -85,7 +85,7 @@ benches: scopes: {} btreemap_get_blob_8_1024: total: - instructions: 278698743 + instructions: 268381297 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -97,7 +97,7 @@ benches: scopes: {} btreemap_get_blob_8_u64: total: - instructions: 236566066 + instructions: 225474655 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -109,7 +109,7 @@ benches: scopes: {} btreemap_get_u64_blob_8: total: - instructions: 218513418 + instructions: 205920808 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -121,7 +121,7 @@ benches: scopes: {} btreemap_get_u64_u64: total: - instructions: 221197138 + instructions: 209260964 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -139,7 +139,7 @@ benches: scopes: {} btreemap_insert_blob_1024_128: total: - instructions: 5186705840 + instructions: 5161959009 heap_increase: 0 stable_memory_increase: 262 scopes: {} @@ -151,7 +151,7 @@ benches: scopes: {} btreemap_insert_blob_1024_16: total: - instructions: 5145571118 + instructions: 5118959504 heap_increase: 0 stable_memory_increase: 241 scopes: {} @@ -163,7 +163,7 @@ benches: scopes: {} btreemap_insert_blob_1024_256: total: - instructions: 5209384433 + instructions: 5188758304 heap_increase: 0 stable_memory_increase: 292 scopes: {} @@ -175,7 +175,7 @@ benches: scopes: {} btreemap_insert_blob_1024_32: total: - instructions: 5154403135 + instructions: 5127387539 heap_increase: 0 stable_memory_increase: 239 scopes: {} @@ -187,7 +187,7 @@ benches: scopes: {} btreemap_insert_blob_1024_4: total: - instructions: 5044404444 + instructions: 5017669644 heap_increase: 0 stable_memory_increase: 235 scopes: {} @@ -199,7 +199,7 @@ benches: scopes: {} btreemap_insert_blob_1024_512: total: - instructions: 5327777997 + instructions: 5311041305 heap_increase: 0 stable_memory_increase: 348 scopes: {} @@ -211,7 +211,7 @@ benches: scopes: {} btreemap_insert_blob_1024_64: total: - instructions: 5186004561 + instructions: 5159953922 heap_increase: 0 stable_memory_increase: 250 scopes: {} @@ -223,7 +223,7 @@ benches: scopes: {} btreemap_insert_blob_1024_8: total: - instructions: 5127727238 + instructions: 5101541533 heap_increase: 0 stable_memory_increase: 237 scopes: {} @@ -235,7 +235,7 @@ benches: scopes: {} btreemap_insert_blob_128_1024: total: - instructions: 1485375547 + instructions: 1469230483 heap_increase: 0 stable_memory_increase: 260 scopes: {} @@ -247,7 +247,7 @@ benches: scopes: {} btreemap_insert_blob_16_1024: total: - instructions: 860356527 + instructions: 849042412 heap_increase: 0 stable_memory_increase: 215 scopes: {} @@ -259,7 +259,7 @@ benches: scopes: {} btreemap_insert_blob_256_1024: total: - instructions: 2052283430 + instructions: 2040557559 heap_increase: 0 stable_memory_increase: 292 scopes: {} @@ -271,7 +271,7 @@ benches: scopes: {} btreemap_insert_blob_32_1024: total: - instructions: 911476044 + instructions: 900953923 heap_increase: 0 stable_memory_increase: 230 scopes: {} @@ -283,7 +283,7 @@ benches: scopes: {} btreemap_insert_blob_4_1024: total: - instructions: 652205537 + instructions: 642764718 heap_increase: 0 stable_memory_increase: 123 scopes: {} @@ -295,7 +295,7 @@ benches: scopes: {} btreemap_insert_blob_512_1024: total: - instructions: 3185109882 + instructions: 3174656554 heap_increase: 0 stable_memory_increase: 351 scopes: {} @@ -307,7 +307,7 @@ benches: scopes: {} btreemap_insert_blob_64_1024: total: - instructions: 1182600112 + instructions: 1168179927 heap_increase: 0 stable_memory_increase: 245 scopes: {} @@ -319,7 +319,7 @@ benches: scopes: {} btreemap_insert_blob_8_1024: total: - instructions: 778253260 + instructions: 767840002 heap_increase: 0 stable_memory_increase: 183 scopes: {} @@ -331,7 +331,7 @@ benches: scopes: {} btreemap_insert_blob_8_u64: total: - instructions: 374278286 + instructions: 365969245 heap_increase: 0 stable_memory_increase: 6 scopes: {} @@ -343,7 +343,7 @@ benches: scopes: {} btreemap_insert_u64_blob_8: total: - instructions: 383104366 + instructions: 374602557 heap_increase: 0 stable_memory_increase: 7 scopes: {} @@ -355,7 +355,7 @@ benches: scopes: {} btreemap_insert_u64_u64: total: - instructions: 395181035 + instructions: 387286840 heap_increase: 0 stable_memory_increase: 7 scopes: {} @@ -379,7 +379,7 @@ benches: scopes: {} btreemap_remove_blob_128_1024: total: - instructions: 1858425782 + instructions: 1841264273 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -391,7 +391,7 @@ benches: scopes: {} btreemap_remove_blob_16_1024: total: - instructions: 1076940094 + instructions: 1065400308 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -403,7 +403,7 @@ benches: scopes: {} btreemap_remove_blob_256_1024: total: - instructions: 2525087949 + instructions: 2515293756 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -415,7 +415,7 @@ benches: scopes: {} btreemap_remove_blob_32_1024: total: - instructions: 1157074288 + instructions: 1144867455 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -427,7 +427,7 @@ benches: scopes: {} btreemap_remove_blob_4_1024: total: - instructions: 660272232 + instructions: 650985281 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -439,7 +439,7 @@ benches: scopes: {} btreemap_remove_blob_512_1024: total: - instructions: 3931913656 + instructions: 3922390914 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -451,7 +451,7 @@ benches: scopes: {} btreemap_remove_blob_64_1024: total: - instructions: 1494578497 + instructions: 1482176514 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -463,7 +463,7 @@ benches: scopes: {} btreemap_remove_blob_8_1024: total: - instructions: 869180263 + instructions: 859176371 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -475,7 +475,7 @@ benches: scopes: {} btreemap_remove_blob_8_u64: total: - instructions: 490671511 + instructions: 482482482 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -487,7 +487,7 @@ benches: scopes: {} btreemap_remove_u64_blob_8: total: - instructions: 545588676 + instructions: 536314185 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -499,7 +499,7 @@ benches: scopes: {} btreemap_remove_u64_u64: total: - instructions: 567151530 + instructions: 557614102 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -517,13 +517,13 @@ benches: scopes: {} memory_manager_grow: total: - instructions: 351687872 + instructions: 349031714 heap_increase: 2 stable_memory_increase: 32000 scopes: {} memory_manager_overhead: total: - instructions: 1182143127 + instructions: 1182084703 heap_increase: 0 stable_memory_increase: 8320 scopes: {} diff --git a/src/btreemap/node/v1.rs b/src/btreemap/node/v1.rs index 9fec2a3d..b25b9ace 100644 --- a/src/btreemap/node/v1.rs +++ b/src/btreemap/node/v1.rs @@ -86,6 +86,7 @@ impl Node { let mut children = vec![]; if header.node_type == INTERNAL_NODE_TYPE { // The number of children is equal to the number of entries + 1. + children.reserve(header.num_entries as usize + 1); for _ in 0..header.num_entries + 1 { let child = Address::from(read_u64(memory, address + offset)); offset += Address::size(); diff --git a/src/memory_manager.rs b/src/memory_manager.rs index a84075be..7e8a6e4f 100644 --- a/src/memory_manager.rs +++ b/src/memory_manager.rs @@ -47,7 +47,6 @@ use crate::{ }; use std::cell::RefCell; use std::cmp::min; -use std::collections::BTreeMap; use std::rc::Rc; const MAGIC: &[u8; 3] = b"MGR"; @@ -228,7 +227,7 @@ struct MemoryManagerInner { memory_sizes_in_pages: [u64; MAX_NUM_MEMORIES as usize], // A map mapping each managed memory to the bucket ids that are allocated to it. - memory_buckets: BTreeMap>, + memory_buckets: Vec>, } impl MemoryManagerInner { @@ -255,7 +254,7 @@ impl MemoryManagerInner { memory, allocated_buckets: 0, memory_sizes_in_pages: [0; MAX_NUM_MEMORIES as usize], - memory_buckets: BTreeMap::new(), + memory_buckets: vec![vec![]; MAX_NUM_MEMORIES as usize], bucket_size_in_pages, }; @@ -280,13 +279,10 @@ impl MemoryManagerInner { let mut buckets = vec![0; MAX_NUM_BUCKETS as usize]; memory.read(bucket_allocations_address(BucketId(0)).get(), &mut buckets); - let mut memory_buckets = BTreeMap::new(); + let mut memory_buckets = vec![vec![]; MAX_NUM_MEMORIES as usize]; for (bucket_idx, memory) in buckets.into_iter().enumerate() { if memory != UNALLOCATED_BUCKET_MARKER { - memory_buckets - .entry(MemoryId(memory)) - .or_insert_with(Vec::new) - .push(BucketId(bucket_idx as u16)); + memory_buckets[memory as usize].push(BucketId(bucket_idx as u16)); } } @@ -331,14 +327,12 @@ impl MemoryManagerInner { return -1; } + let memory_bucket = &mut self.memory_buckets[id.0 as usize]; // Allocate new buckets as needed. + memory_bucket.reserve(new_buckets_needed as usize); for _ in 0..new_buckets_needed { let new_bucket_id = BucketId(self.allocated_buckets); - - self.memory_buckets - .entry(id) - .or_default() - .push(new_bucket_id); + memory_bucket.push(new_bucket_id); // Write in stable store that this bucket belongs to the memory with the provided `id`. write( @@ -402,12 +396,9 @@ impl MemoryManagerInner { } // Initializes a [`BucketIterator`]. - fn bucket_iter(&self, id: MemoryId, offset: u64, length: usize) -> BucketIterator { + fn bucket_iter(&self, MemoryId(id): MemoryId, offset: u64, length: usize) -> BucketIterator { // Get the buckets allocated to the given memory id. - let buckets = match self.memory_buckets.get(&id) { - Some(s) => s.as_slice(), - None => &[], - }; + let buckets = self.memory_buckets[id as usize].as_slice(); BucketIterator { virtual_segment: Segment { @@ -543,7 +534,6 @@ fn bucket_allocations_address(id: BucketId) -> Address { #[cfg(test)] mod test { use super::*; - use maplit::btreemap; use proptest::prelude::*; const MAX_MEMORY_IN_PAGES: u64 = MAX_NUM_BUCKETS * BUCKET_SIZE_IN_PAGES; @@ -572,12 +562,11 @@ mod test { memory.read(0, &mut bytes); assert_eq!(bytes, vec![1, 2, 3]); - assert_eq!( - mem_mgr.inner.borrow().memory_buckets, - btreemap! { - MemoryId(0) => vec![BucketId(0)] - } - ); + assert_eq!(mem_mgr.inner.borrow().memory_buckets[0], vec![BucketId(0)]); + + assert!(mem_mgr.inner.borrow().memory_buckets[1..] + .iter() + .all(|x| x.is_empty())); } #[test] @@ -594,13 +583,14 @@ mod test { assert_eq!(memory_1.size(), 1); assert_eq!( - mem_mgr.inner.borrow().memory_buckets, - btreemap! { - MemoryId(0) => vec![BucketId(0)], - MemoryId(1) => vec![BucketId(1)], - } + &mem_mgr.inner.borrow().memory_buckets[..2], + &[vec![BucketId(0)], vec![BucketId(1)],] ); + assert!(mem_mgr.inner.borrow().memory_buckets[2..] + .iter() + .all(|x| x.is_empty())); + memory_0.write(0, &[1, 2, 3]); memory_0.write(0, &[1, 2, 3]); memory_1.write(0, &[4, 5, 6]); From b689714812092b8340643991756560120e5eeda9 Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 18 Nov 2024 13:08:46 +0000 Subject: [PATCH 10/11] Canbench --- canbench_results.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/canbench_results.yml b/canbench_results.yml index e108bb41..78faaf16 100644 --- a/canbench_results.yml +++ b/canbench_results.yml @@ -589,13 +589,13 @@ benches: scopes: {} memory_manager_grow: total: - instructions: 351687872 + instructions: 349031714 heap_increase: 2 stable_memory_increase: 32000 scopes: {} memory_manager_overhead: total: - instructions: 1182143127 + instructions: 1182084703 heap_increase: 0 stable_memory_increase: 8320 scopes: {} From beb4c5fa146afcf4585dc91b66dcc6b536b79604 Mon Sep 17 00:00:00 2001 From: David Frank Date: Mon, 2 Dec 2024 13:00:44 +0000 Subject: [PATCH 11/11] Canbench --- canbench_results.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/canbench_results.yml b/canbench_results.yml index b205d13e..7697b660 100644 --- a/canbench_results.yml +++ b/canbench_results.yml @@ -73,7 +73,7 @@ benches: scopes: {} btreemap_get_blob_512_1024_v2_mem_manager: total: - instructions: 3164909208 + instructions: 3093928908 heap_increase: 0 stable_memory_increase: 0 scopes: { } @@ -139,7 +139,7 @@ benches: scopes: { } btreemap_get_u64_u64_v2_mem_manager: total: - instructions: 673752904 + instructions: 619036864 heap_increase: 0 stable_memory_increase: 0 scopes: {} @@ -223,7 +223,7 @@ benches: scopes: { } btreemap_insert_blob_1024_512_v2_mem_manager: total: - instructions: 6292667075 + instructions: 6188501297 heap_increase: 0 stable_memory_increase: 256 scopes: {} @@ -379,7 +379,7 @@ benches: scopes: {} btreemap_insert_u64_u64_mem_manager: total: - instructions: 1019612837 + instructions: 937850849 heap_increase: 0 stable_memory_increase: 0 scopes: { } @@ -625,13 +625,13 @@ benches: scopes: {} memory_manager_grow: total: - instructions: 349384162 + instructions: 346632000 heap_increase: 2 stable_memory_increase: 32000 scopes: {} memory_manager_overhead: total: - instructions: 1182116368 + instructions: 1182056741 heap_increase: 0 stable_memory_increase: 8320 scopes: {} @@ -661,7 +661,7 @@ benches: scopes: {} vec_get_blob_4_mem_manager: total: - instructions: 14476122 + instructions: 12856122 heap_increase: 0 stable_memory_increase: 0 scopes: { } @@ -673,7 +673,7 @@ benches: scopes: { } vec_get_blob_64_mem_manager: total: - instructions: 24146389 + instructions: 22526389 heap_increase: 0 stable_memory_increase: 0 scopes: {}