that was a long walk to fix a bad architectural decision

trying to allocate key indicies within the dictionary pool in a late-binding
way is hard to do in rust, because of various interior mutability problems.
i guess it could be done but it turned out to be easier to pro-actively
track everything and generate them on demand.

allocations are hard in rust. i guess that's why the alloc thing is
still not stable.
This commit is contained in:
bunnie 2021-11-22 01:46:53 +08:00
parent e4e5e6c390
commit 2859e54bb9
3 changed files with 13 additions and 14 deletions

View file

@ -283,7 +283,7 @@ impl BasisCache {
keys: HashMap::<String, KeyCacheEntry>::new(), keys: HashMap::<String, KeyCacheEntry>::new(),
clean: false, clean: false,
age: 0, age: 0,
free_keys: RefCell::new(free_keys), free_keys,
last_disk_key_index: KEY_MAXCOUNT as u32, last_disk_key_index: KEY_MAXCOUNT as u32,
flags: init_flags, flags: init_flags,
key_count: 0, key_count: 0,

View file

@ -121,7 +121,7 @@ pub(crate) struct DictCacheEntry {
/// count of total keys in the dictionary -- may be equal to or larger than the number of elements in `keys` /// count of total keys in the dictionary -- may be equal to or larger than the number of elements in `keys`
pub(crate) key_count: u32, pub(crate) key_count: u32,
/// track the pool of free key indices. Wrapped in a refcell so we can work the index mechanism while updating the keys HashMap /// track the pool of free key indices. Wrapped in a refcell so we can work the index mechanism while updating the keys HashMap
pub(crate) free_keys: RefCell<BinaryHeap::<FreeKeyRange>>, pub(crate) free_keys: BinaryHeap::<FreeKeyRange>,
/// hint for when to stop doing a brute-force search for the existence of a key in the disk records. /// hint for when to stop doing a brute-force search for the existence of a key in the disk records.
/// This field is set to the max count on a new, naive record; and set only upon a sync() or a fill() call. /// This field is set to the max count on a new, naive record; and set only upon a sync() or a fill() call.
pub(crate) last_disk_key_index: u32, pub(crate) last_disk_key_index: u32,
@ -161,7 +161,7 @@ impl DictCacheEntry {
index: index as u32, index: index as u32,
keys: HashMap::<String, KeyCacheEntry>::new(), keys: HashMap::<String, KeyCacheEntry>::new(),
key_count: dict.num_keys, key_count: dict.num_keys,
free_keys: RefCell::new(free_keys), free_keys,
last_disk_key_index: KEY_MAXCOUNT as u32, last_disk_key_index: KEY_MAXCOUNT as u32,
clean: true, clean: true,
age: dict.age, age: dict.age,
@ -715,10 +715,10 @@ impl DictCacheEntry {
} }
pub(crate) fn get_free_key_index(&mut self) -> Option<NonZeroU32> { pub(crate) fn get_free_key_index(&mut self) -> Option<NonZeroU32> {
if let Some(free_key) = self.free_keys.borrow_mut().pop() { if let Some(free_key) = self.free_keys.pop() {
let index = free_key.start; let index = free_key.start;
if free_key.run > 0 { if free_key.run > 0 {
self.free_keys.borrow_mut().push( self.free_keys.push(
FreeKeyRange { FreeKeyRange {
start: index + 1, start: index + 1,
run: free_key.run - 1, run: free_key.run - 1,
@ -735,8 +735,8 @@ impl DictCacheEntry {
None None
} }
} }
pub(crate) fn put_free_key_index(&self, index: u32) { pub(crate) fn put_free_key_index(&mut self, index: u32) {
let free_keys = self.free_keys.replace(BinaryHeap::<FreeKeyRange>::new()); let free_keys = std::mem::replace(&mut self.free_keys, BinaryHeap::<FreeKeyRange>::new());
let free_key_vec = free_keys.into_sorted_vec(); let free_key_vec = free_keys.into_sorted_vec();
// this is a bit weird because we have three cases: // this is a bit weird because we have three cases:
// - the new key is more than 1 away from any element, in which case we insert it as a singleton (start = index, run = 0) // - the new key is more than 1 away from any element, in which case we insert it as a singleton (start = index, run = 0)
@ -751,11 +751,11 @@ impl DictCacheEntry {
} }
match free_key_vec[i].compare_to(i as u32) { match free_key_vec[i].compare_to(i as u32) {
FreeKeyCases::LessThan => { FreeKeyCases::LessThan => {
self.free_keys.borrow_mut().push(FreeKeyRange{start: index as u32, run: 0}); self.free_keys.push(FreeKeyRange{start: index as u32, run: 0});
break; break;
} }
FreeKeyCases::LeftAdjacent => { FreeKeyCases::LeftAdjacent => {
self.free_keys.borrow_mut().push(FreeKeyRange{start: index as u32, run: free_key_vec[i].run + 1}); self.free_keys.push(FreeKeyRange{start: index as u32, run: free_key_vec[i].run + 1});
} }
FreeKeyCases::Within => { FreeKeyCases::Within => {
log::error!("Double-free error in free_keys()"); log::error!("Double-free error in free_keys()");
@ -765,18 +765,18 @@ impl DictCacheEntry {
// see if we should merge to the right // see if we should merge to the right
if i + 1 < free_key_vec.len() { if i + 1 < free_key_vec.len() {
if free_key_vec[i+1].compare_to(i as u32) == FreeKeyCases::LeftAdjacent { if free_key_vec[i+1].compare_to(i as u32) == FreeKeyCases::LeftAdjacent {
self.free_keys.borrow_mut().push(FreeKeyRange{ self.free_keys.push(FreeKeyRange{
start: free_key_vec[i].start, start: free_key_vec[i].start,
run: free_key_vec[i].run + free_key_vec[i+1].run + 2 run: free_key_vec[i].run + free_key_vec[i+1].run + 2
}); });
skip = true skip = true
} }
} else { } else {
self.free_keys.borrow_mut().push(FreeKeyRange { start: free_key_vec[i].start, run: free_key_vec[i].run + 1 }) self.free_keys.push(FreeKeyRange { start: free_key_vec[i].start, run: free_key_vec[i].run + 1 })
} }
} }
FreeKeyCases::GreaterThan => { FreeKeyCases::GreaterThan => {
self.free_keys.borrow_mut().push(free_key_vec[i]); self.free_keys.push(free_key_vec[i]);
} }
} }
} }

View file

@ -399,14 +399,13 @@ fn xmain() -> ! {
} }
basis_cache.key_update(&mut pddb_os, "system settings", "wifi/wpa_keys/e4200", "12345678".as_bytes(), None, None, None, false).expect("couldn't add a key"); basis_cache.key_update(&mut pddb_os, "system settings", "wifi/wpa_keys/e4200", "12345678".as_bytes(), None, None, None, false).expect("couldn't add a key");
/*
// add a "big" key // add a "big" key
let mut bigdata = [0u8; 5000]; let mut bigdata = [0u8; 5000];
for (i, d) in bigdata.iter_mut().enumerate() { for (i, d) in bigdata.iter_mut().enumerate() {
*d = i as u8; *d = i as u8;
} }
basis_cache.key_update(&mut pddb_os, "system settings", "big_pool1", &bigdata, None, None, None, false).expect("couldn't add a key"); basis_cache.key_update(&mut pddb_os, "system settings", "big_pool1", &bigdata, None, None, None, false).expect("couldn't add a key");
*/
#[cfg(not(any(target_os = "none", target_os = "xous")))] #[cfg(not(any(target_os = "none", target_os = "xous")))]
pddb_os.dbg_dump(); pddb_os.dbg_dump();