From a1950a4c00140c41f646bab6f0e2f9c9c55d81f0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 1 Jul 2022 19:45:55 +0800 Subject: [PATCH] make a default alloc hint that's not 0 set to 8 bytes initially, could be adjusted up or down. --- services/pddb/src/backend/basis.rs | 12 ++++++++++-- services/pddb/src/backend/dictionary.rs | 10 +++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 854e9f8e3..1f22b19bf 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -196,6 +196,10 @@ pub(crate) const DK_PER_VPAGE: usize = VPAGE_SIZE / DK_STRIDE; // should be 32 - pub(crate) const DICT_VSIZE: u64 = 0xFE_0000; /// maximum number of dictionaries in a system pub(crate) const DICT_MAXCOUNT: usize = 16383; +/// default alloc hint, if none is given (needs to be non-zero) +/// this would be the typical "minimum space" reserved for a key +/// users are of course allowed to specify something smaller, but it should be non-zero +pub(crate) const DEFAULT_ALLOC_HINT: usize = 8; /// This is the format of the Basis as stored on disk #[derive(PartialEq, Debug, Default)] @@ -583,10 +587,14 @@ impl BasisCache { // mutate the page table to allocate data while we're accessing the page table. This huge gob of code // computes the pages needed. :-/ let mut pages_needed = 2; // things go badly when no space is available so make sure there's always at least 1 spot - let reserved = if data.len() + offset.unwrap_or(0) > alloc_hint.unwrap_or(0) { + let reserved = if data.len() + offset.unwrap_or(0) > alloc_hint.unwrap_or(DEFAULT_ALLOC_HINT) { data.len() + offset.unwrap_or(0) } else { - alloc_hint.unwrap_or(0) + if alloc_hint.unwrap_or(DEFAULT_ALLOC_HINT) == 0 { // disallow 0-sized alloc hints, round up to the default size if someone tries 0 + DEFAULT_ALLOC_HINT + } else { + alloc_hint.unwrap_or(DEFAULT_ALLOC_HINT) + } }; let reserved_pages = if reserved % VPAGE_SIZE == 0 { reserved / VPAGE_SIZE diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index b234dbeb8..e38027a6a 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -570,7 +570,7 @@ impl DictCacheEntry { } else { // key does not exist (or was previously erased) -- create one or replace the erased one. // try to fit the key in the small pool first - if ((data.len() + offset) < SMALL_CAPACITY) && (alloc_hint.unwrap_or(0) < SMALL_CAPACITY) { + if ((data.len() + offset) < SMALL_CAPACITY) && (alloc_hint.unwrap_or(DEFAULT_ALLOC_HINT) < SMALL_CAPACITY) { log::debug!("creating small key {}", name); // handle the case that we're a brand new dictionary and no small keys have ever been stored before. if self.small_pool.len() == 0 { @@ -578,8 +578,8 @@ impl DictCacheEntry { self.rebuild_free_pool(); } let pool_candidate = self.small_pool_free.pop().expect("Free pool was allocated & rebuilt, but still empty."); - let mut reservation = if alloc_hint.unwrap_or(0) > data.len() + offset { - alloc_hint.unwrap_or(0) + let mut reservation = if alloc_hint.unwrap_or(DEFAULT_ALLOC_HINT) > data.len() + offset { + alloc_hint.unwrap_or(DEFAULT_ALLOC_HINT) } else { data.len() + offset }; @@ -647,8 +647,8 @@ impl DictCacheEntry { log::debug!("creating large key"); // it didn't fit in the small pool, stick it in the big pool. let reservation = PageAlignedVa::from( - if alloc_hint.unwrap_or(0) > data.len() + offset { - alloc_hint.unwrap_or(0) + if alloc_hint.unwrap_or(DEFAULT_ALLOC_HINT) > data.len() + offset { + alloc_hint.unwrap_or(DEFAULT_ALLOC_HINT) } else { data.len() + offset });