Repository: cberner/redb
Pull request #572 (2023-05-02 16:04:21)

Convert a bunch of variable types away from usize


Patch:
diff --git a/benches/multithreaded_insert_benchmark.rs b/benches/multithreaded_insert_benchmark.rs
index 0361e5c..d056a6b 100644
--- a/benches/multithreaded_insert_benchmark.rs
+++ b/benches/multithreaded_insert_benchmark.rs
@@ -7,7 +7,7 @@ use rand::{Rng, SeedableRng};
 use redb::{Database, ReadableTable, TableDefinition};
 use std::time::Instant;
 
-const ELEMENTS: usize = 1_000_000;
+const ELEMENTS: u64 = 1_000_000;
 const RNG_SEED: u64 = 3;
 
 const TABLE1: TableDefinition<u128, u128> = TableDefinition::new("x");
diff --git a/fuzz/fuzz_targets/fuzz_redb.rs b/fuzz/fuzz_targets/fuzz_redb.rs
index 5ceee94..238d25e 100644
--- a/fuzz/fuzz_targets/fuzz_redb.rs
+++ b/fuzz/fuzz_targets/fuzz_redb.rs
@@ -80,8 +80,8 @@ fn exec_table_inner(db: Arc<Database>, transactions: &[FuzzTransaction], referen
                     }
                     FuzzOperation::InsertReserve { key, value_size } => {
                         let key = key.value;
-                        let value_size = value_size.value as usize;
-                        let mut value = table.insert_reserve(&key, value_size)?;
+                        let value_size = value_size.value;
+                        let mut value = table.insert_reserve(&key, value_size as u32)?;
                         value.as_mut().fill(0xFF);
                         local_reference.insert(key, value_size);
                     }
@@ -98,7 +98,7 @@ fn exec_table_inner(db: Arc<Database>, transactions: &[FuzzTransaction], referen
                         }
                     }
                     FuzzOperation::Len {} => {
-                        assert_eq!(local_reference.len(), table.len().unwrap());
+                        assert_eq!(local_reference.len() as u64, table.len().unwrap());
                     }
                     FuzzOperation::Range {
                         start_key,
@@ -235,7 +235,7 @@ fn exec_multimap_table_inner(db: Arc<Database>, transactions: &[FuzzTransaction]
                         for v in local_reference.values() {
                             reference_len += v.len();
                         }
-                        assert_eq!(reference_len, table.len().unwrap());
+                        assert_eq!(reference_len as u64, table.len().unwrap());
                     }
                     FuzzOperation::Range {
                         start_key,
diff --git a/src/db.rs b/src/db.rs
index a6de678..73eea8d 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -435,11 +435,10 @@ impl Database {
         Ok(())
     }
 
-    #[allow(clippy::too_many_arguments)]
     fn new(
         file: File,
         page_size: usize,
-        region_size: Option<usize>,
+        region_size: Option<u64>,
         read_cache_size_bytes: usize,
         write_cache_size_bytes: usize,
     ) -> Result<Self> {
@@ -528,7 +527,7 @@ impl Database {
 /// Configuration builder of a redb [Database].
 pub struct Builder {
     page_size: usize,
-    region_size: Option<usize>,
+    region_size: Option<u64>,
     read_cache_size_bytes: usize,
     write_cache_size_bytes: usize,
 }
@@ -580,7 +579,7 @@ impl Builder {
     }
 
     #[cfg(test)]
-    fn set_region_size(&mut self, size: usize) -> &mut Self {
+    fn set_region_size(&mut self, size: u64) -> &mut Self {
         assert!(size.is_power_of_two());
         self.region_size = Some(size);
         self
diff --git a/src/multimap_table.rs b/src/multimap_table.rs
index 753eb04..dcd000f 100644
--- a/src/multimap_table.rs
+++ b/src/multimap_table.rs
@@ -788,7 +788,7 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTabl
     }
 
     /// Returns the number of key-value pairs in the table
-    fn len(&self) -> Result<usize> {
+    fn len(&self) -> Result<u64> {
         let mut count = 0;
         for item in self.iter()? {
             let (_, values) = item?;
@@ -827,7 +827,7 @@ pub trait ReadableMultimapTable<K: RedbKey + 'static, V: RedbKey + 'static>: Sea
         K: 'a,
         KR: Borrow<K::SelfType<'a>> + 'a;
 
-    fn len(&self) -> Result<usize>;
+    fn len(&self) -> Result<u64>;
 
     fn is_empty(&self) -> Result<bool>;
 
@@ -889,7 +889,7 @@ impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K,
         Ok(MultimapRange::new(inner, self.mem))
     }
 
-    fn len(&self) -> Result<usize> {
+    fn len(&self) -> Result<u64> {
         let mut count = 0;
         for item in self.iter()? {
             let (_, values) = item?;
diff --git a/src/table.rs b/src/table.rs
index 6cb3d66..5e9b537 100644
--- a/src/table.rs
+++ b/src/table.rs
@@ -136,7 +136,7 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbValueMutInPlace + 'static> Table<'d
     pub fn insert_reserve<'a>(
         &mut self,
         key: impl Borrow<K::SelfType<'a>>,
-        value_length: usize,
+        value_length: u32,
     ) -> Result<AccessGuardMut<V>>
     where
         K: 'a,
@@ -163,7 +163,7 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> ReadableTable<K, V
         self.tree.range(range).map(Range::new)
     }
 
-    fn len(&self) -> Result<usize> {
+    fn len(&self) -> Result<u64> {
         self.tree.len()
     }
 
@@ -224,7 +224,7 @@ pub trait ReadableTable<K: RedbKey + 'static, V: RedbValue + 'static>: Sealed {
         KR: Borrow<K::SelfType<'a>> + 'a;
 
     /// Returns the number of entries in the table
-    fn len(&self) -> Result<usize>;
+    fn len(&self) -> Result<u64>;
 
     /// Returns `true` if the table is empty
     fn is_empty(&self) -> Result<bool>;
@@ -270,7 +270,7 @@ impl<'txn, K: RedbKey + 'static, V: RedbValue + 'static> ReadableTable<K, V>
         self.tree.range(range).map(Range::new)
     }
 
-    fn len(&self) -> Result<usize> {
+    fn len(&self) -> Result<u64> {
         self.tree.len()
     }
 
diff --git a/src/transactions.rs b/src/transactions.rs
index eb2a7b1..2755a0d 100644
--- a/src/transactions.rs
+++ b/src/transactions.rs
@@ -21,50 +21,50 @@ use std::{panic, thread};
 /// Informational storage stats about the database
 #[derive(Debug)]
 pub struct DatabaseStats {
-    pub(crate) tree_height: usize,
-    pub(crate) allocated_pages: usize,
-    pub(crate) leaf_pages: usize,
-    pub(crate) branch_pages: usize,
-    pub(crate) stored_leaf_bytes: usize,
-    pub(crate) metadata_bytes: usize,
-    pub(crate) fragmented_bytes: usize,
+    pub(crate) tree_height: u32,
+    pub(crate) allocated_pages: u64,
+    pub(crate) leaf_pages: u64,
+    pub(crate) branch_pages: u64,
+    pub(crate) stored_leaf_bytes: u64,
+    pub(crate) metadata_bytes: u64,
+    pub(crate) fragmented_bytes: u64,
     pub(crate) page_size: usize,
 }
 
 impl DatabaseStats {
     /// Maximum traversal distance to reach the deepest (key, value) pair, across all tables
-    pub fn tree_height(&self) -> usize {
+    pub fn tree_height(&self) -> u32 {
         self.tree_height
     }
 
     /// Number of pages allocated
-    pub fn allocated_pages(&self) -> usize {
+    pub fn allocated_pages(&self) -> u64 {
         self.allocated_pages
     }
 
     /// Number of leaf pages that store user data
-    pub fn leaf_pages(&self) -> usize {
+    pub fn leaf_pages(&self) -> u64 {
         self.leaf_pages
     }
 
     /// Number of branch pages in btrees that store user data
-    pub fn branch_pages(&self) -> usize {
+    pub fn branch_pages(&self) -> u64 {
         self.branch_pages
     }
 
     /// Number of bytes consumed by keys and values that have been inserted.
     /// Does not include indexing overhead
-    pub fn stored_bytes(&self) -> usize {
+    pub fn stored_bytes(&self) -> u64 {
         self.stored_leaf_bytes
     }
 
     /// Number of bytes consumed by keys in internal branch pages, plus other metadata
-    pub fn metadata_bytes(&self) -> usize {
+    pub fn metadata_bytes(&self) -> u64 {
         self.metadata_bytes
     }
 
     /// Number of bytes consumed by fragmentation, both in data pages and internal metadata tables
-    pub fn fragmented_bytes(&self) -> usize {
+    pub fn fragmented_bytes(&self) -> u64 {
         self.fragmented_bytes
     }
 
@@ -194,7 +194,7 @@ impl<'db> WriteTransaction<'db> {
 
     /// Creates a snapshot of the current database state, which can be used to rollback the database
     ///
-    /// Returns `[Error::InvalidSavepoint`], if the transaction is "dirty" (any tables have been openned)
+    /// Returns `[Error::InvalidSavepoint`], if the transaction is "dirty" (any tables have been opened)
     pub fn savepoint(&self) -> Result<Savepoint> {
         if self.dirty.load(Ordering::Acquire) {
             return Err(Error::InvalidSavepoint);
@@ -630,7 +630,8 @@ impl<'db> WriteTransaction<'db> {
                 transaction_id: self.transaction_id.0,
                 pagination_id: pagination_counter,
             };
-            let mut access_guard = freed_tree.insert_reserve(&key, buffer_size)?;
+            let mut access_guard =
+                freed_tree.insert_reserve(&key, buffer_size.try_into().unwrap())?;
 
             let mut freed_pages = self.freed_pages.lock().unwrap();
             let len = freed_pages.len();
diff --git a/src/tree_store/btree.rs b/src/tree_store/btree.rs
index 9be52bd..9e03839 100644
--- a/src/tree_store/btree.rs
+++ b/src/tree_store/btree.rs
@@ -17,12 +17,12 @@ use std::ops::{RangeBounds, RangeFull};
 use std::sync::{Arc, Mutex};
 
 pub(crate) struct BtreeStats {
-    pub(crate) tree_height: usize,
-    pub(crate) leaf_pages: usize,
-    pub(crate) branch_pages: usize,
-    pub(crate) stored_leaf_bytes: usize,
-    pub(crate) metadata_bytes: usize,
-    pub(crate) fragmented_bytes: usize,
+    pub(crate) tree_height: u32,
+    pub(crate) leaf_pages: u64,
+    pub(crate) branch_pages: u64,
+    pub(crate) stored_leaf_bytes: u64,
+    pub(crate) metadata_bytes: u64,
+    pub(crate) fragmented_bytes: u64,
 }
 
 pub(crate) struct UntypedBtreeMut<'a> {
@@ -304,7 +304,7 @@ impl<'a, K: RedbKey + 'a, V: RedbValue + 'a> BtreeMut<'a, K, V> {
         Ok(result)
     }
 
-    pub(crate) fn len(&self) -> Result<usize> {
+    pub(crate) fn len(&self) -> Result<u64> {
         self.read_tree()?.len()
     }
 }
@@ -316,7 +316,7 @@ impl<'a, K: RedbKey + 'a, V: RedbValueMutInPlace + 'a> BtreeMut<'a, K, V> {
     pub(crate) fn insert_reserve(
         &mut self,
         key: &K::SelfType<'_>,
-        value_length: usize,
+        value_length: u32,
     ) -> Result<AccessGuardMut<V>> {
         #[cfg(feature = "logging")]
         trace!(
@@ -327,7 +327,7 @@ impl<'a, K: RedbKey + 'a, V: RedbValueMutInPlace + 'a> BtreeMut<'a, K, V> {
         );
         let mut root = self.root.lock().unwrap();
         let mut freed_pages = self.freed_pages.lock().unwrap();
-        let mut value = vec![0u8; value_length];
+        let mut value = vec![0u8; value_length as usize];
         V::initialize(&mut value);
         let mut operation = MutateHelper::<K, V>::new(
             &mut root,
@@ -477,7 +477,7 @@ impl<'a, K: RedbKey, V: RedbValue> Btree<'a, K, V> {
         BtreeRangeIter::new(range, self.root.map(|(p, _)| p), self.mem)
     }
 
-    pub(crate) fn len(&self) -> Result<usize> {
+    pub(crate) fn len(&self) -> Result<u64> {
         let iter: BtreeRangeIter<K, V> = BtreeRangeIter::new::<RangeFull, K::SelfType<'_>>(
             ..,
             self.root.map(|(p, _)| p),
@@ -561,13 +561,13 @@ fn stats_helper(
             let accessor = LeafAccessor::new(page.memory(), fixed_key_size, fixed_value_size);
             let leaf_bytes = accessor.length_of_pairs(0, accessor.num_pairs());
             let overhead_bytes = accessor.total_length() - leaf_bytes;
-            let fragmented_bytes = page.memory().len() - accessor.total_length();
+            let fragmented_bytes = (page.memory().len() - accessor.total_length()) as u64;
             Ok(BtreeStats {
                 tree_height: 1,
                 leaf_pages: 1,
                 branch_pages: 0,
-                stored_leaf_bytes: leaf_bytes,
-                metadata_bytes: overhead_bytes,
+                stored_leaf_bytes: leaf_bytes.try_into().unwrap(),
+                metadata_bytes: overhead_bytes.try_into().unwrap(),
                 fragmented_bytes,
             })
         }
@@ -577,8 +577,8 @@ fn stats_helper(
             let mut leaf_pages = 0;
             let mut branch_pages = 1;
             let mut stored_leaf_bytes = 0;
-            let mut metadata_bytes = accessor.total_length();
-            let mut fragmented_bytes = page.memory().len() - accessor.total_length();
+            let mut metadata_bytes = accessor.total_length() as u64;
+            let mut fragmented_bytes = (page.memory().len() - accessor.total_length()) as u64;
             for i in 0..accessor.count_children() {
                 if let Some(child) = accessor.child_page(i) {
                     let stats = stats_helper(child, mem, fixed_key_size, fixed_value_size)?;
diff --git a/src/tree_store/page_store/bitmap.rs b/src/tree_store/page_store/bitmap.rs
index 14a7032..c825bc4 100644
--- a/src/tree_store/page_store/bitmap.rs
+++ b/src/tree_store/page_store/bitmap.rs
@@ -53,7 +53,7 @@ impl<'a> BtreeBitmap<'a> {
         Self { data }
     }
 
-    pub(crate) fn count_unset(&self) -> usize {
+    pub(crate) fn count_unset(&self) -> u32 {
         self.get_level(self.get_height() - 1).count_unset()
     }
 
@@ -65,7 +65,7 @@ impl<'a> BtreeBitmap<'a> {
         self.get_level(self.get_height() - 1).get(i)
     }
 
-    pub(crate) fn len(&self) -> usize {
+    pub(crate) fn len(&self) -> u32 {
         self.get_level(self.get_height() - 1).len()
     }
 
@@ -290,12 +290,13 @@ impl<'a> U64GroupedBitmap<'a> {
         )
     }
 
-    fn count_unset(&self) -> usize {
-        self.data.iter().map(|x| x.count_ones() as usize).sum()
+    fn count_unset(&self) -> u32 {
+        self.data.iter().map(|x| x.count_ones()).sum()
     }
 
-    pub fn len(&self) -> usize {
-        self.data.len() * (u8::BITS as usize)
+    pub fn len(&self) -> u32 {
+        let len: u32 = self.data.len().try_into().unwrap();
+        len * u8::BITS
     }
 
     fn any_unset(&self) -> bool {
diff --git a/src/tree_store/page_store/buddy_allocator.rs b/src/tree_store/page_store/buddy_allocator.rs
index 4d3ac70..7151b21 100644
--- a/src/tree_store/page_store/buddy_allocator.rs
+++ b/src/tree_store/page_store/buddy_allocator.rs
@@ -128,20 +128,19 @@ impl<'a> BuddyAllocator<'a> {
             .find(|order| self.get_order_free(*order).has_unset())
     }
 
-    pub(crate) fn count_allocated_pages(&self) -> usize {
+    pub(crate) fn count_allocated_pages(&self) -> u32 {
         self.len() - self.count_free_pages()
     }
 
-    pub(crate) fn count_free_pages(&self) -> usize {
+    pub(crate) fn count_free_pages(&self) -> u32 {
         let mut pages = 0;
         for order in 0..=self.get_max_order() {
-            pages +=
-                self.get_order_free(order).count_unset() * 2usize.pow(order.try_into().unwrap());
+            pages += self.get_order_free(order).count_unset() * 2u32.pow(order.try_into().unwrap());
         }
         pages
     }
 
-    pub(crate) fn capacity(&self) -> usize {
+    pub(crate) fn capacity(&self) -> u32 {
         self.get_order_free(0).len()
     }
 
@@ -155,11 +154,11 @@ impl<'a> BuddyAllocator<'a> {
         None
     }
 
-    pub(crate) fn trailing_free_pages(&self) -> usize {
+    pub(crate) fn trailing_free_pages(&self) -> u32 {
         let mut free_pages = 0;
         let mut next_page = self.len() - 1;
-        while let Some(order) = self.find_free_order(next_page.try_into().unwrap()) {
-            let order_size = 2usize.pow(order.try_into().unwrap());
+        while let Some(order) = self.find_free_order(next_page) {
+            let order_size = 2u32.pow(order.into());
             free_pages += order_size;
             if order_size > next_page {
                 break;
@@ -176,8 +175,8 @@ impl<'a> BuddyAllocator<'a> {
         for order in 0..=self.get_max_order() {
             let allocated = self.get_order_allocated(order);
             for i in 0..min(allocated.len(), self.len()) {
-                if allocated.get(i.try_into().unwrap()) {
-                    result.insert(PageNumber::new(region, i.try_into().unwrap(), order));
+                if allocated.get(i) {
+                    result.insert(PageNumber::new(region, i, order));
                 }
             }
         }
@@ -186,7 +185,7 @@ impl<'a> BuddyAllocator<'a> {
         // Check the result against the free index to be sure it matches
         {
             let mut free_check = HashSet::new();
-            for i in 0..u32::try_from(self.len()).unwrap() {
+            for i in 0..self.len() {
                 if self.find_free_order(i).is_none() {
                     free_check.insert(PageNumber::new(region, i, 0));
                 }
@@ -206,8 +205,8 @@ impl<'a> BuddyAllocator<'a> {
         self.data[0]
     }
 
-    pub(crate) fn len(&self) -> usize {
-        get_num_pages(self.data) as usize
+    pub(crate) fn len(&self) -> u32 {
+        get_num_pages(self.data)
     }
 
     fn get_order_free(&self, order: u8) -> BtreeBitmap {
@@ -282,7 +281,7 @@ impl<'a> BuddyAllocatorMut<'a> {
         Self { data }
     }
 
-    pub(crate) fn resize(&mut self, new_size: usize) {
+    pub(crate) fn resize(&mut self, new_size: u32) {
         self.debug_check_consistency();
         assert!(new_size <= self.capacity());
         if new_size > self.len() {
@@ -290,21 +289,21 @@ impl<'a> BuddyAllocatorMut<'a> {
             // Align to the highest order possible
             while processed_pages < new_size {
                 let order: u8 = processed_pages.trailing_zeros().try_into().unwrap();
-                let order_size = 2usize.pow(order as u32);
+                let order_size = 2u32.pow(order.into());
                 let page = processed_pages / order_size;
                 debug_assert_eq!(processed_pages % order_size, 0);
                 if order >= self.get_max_order() || processed_pages + order_size > new_size {
                     break;
                 }
-                self.free_inner(page.try_into().unwrap(), order);
+                self.free_inner(page, order);
                 processed_pages += order_size;
             }
             // Allocate the remaining space, at the highest order
             for order in (0..=self.get_max_order()).rev() {
-                let order_size = 2usize.pow(order.into());
+                let order_size = 2u32.pow(order.into());
                 while processed_pages + order_size <= new_size {
                     let page = processed_pages / order_size;
-                    self.free_inner(page.try_into().unwrap(), order);
+                    self.free_inner(page, order);
                     processed_pages += order_size;
                 }
             }
@@ -315,28 +314,28 @@ impl<'a> BuddyAllocatorMut<'a> {
             // Align to the highest order possible
             while processed_pages < self.len() {
                 let order: u8 = processed_pages.trailing_zeros().try_into().unwrap();
-                let order_size = 2usize.pow(order as u32);
+                let order_size = 2u32.pow(order.into());
                 let page = processed_pages / order_size;
                 debug_assert_eq!(processed_pages % order_size, 0);
                 if order >= self.get_max_order() || processed_pages + order_size > self.len() {
                     break;
                 }
-                self.record_alloc_inner(page.try_into().unwrap(), order);
+                self.record_alloc_inner(page, order);
                 processed_pages += order_size;
             }
             // Allocate the remaining space, at the highest order
             for order in (0..=self.get_max_order()).rev() {
-                let order_size = 2usize.pow(order.try_into().unwrap());
+                let order_size = 2u32.pow(order.into());
                 while processed_pages + order_size <= self.len() {
                     let page = processed_pages / order_size;
-                    self.record_alloc_inner(page.try_into().unwrap(), order);
+                    self.record_alloc_inner(page, order);
                     processed_pages += order_size;
                 }
             }
             assert_eq!(processed_pages, self.len());
         }
         self.data[NUM_PAGES_OFFSET..(NUM_PAGES_OFFSET + size_of::<u32>())]
-            .copy_from_slice(&u32::try_from(new_size).unwrap().to_le_bytes());
+            .copy_from_slice(&new_size.to_le_bytes());
     }
 
     #[allow(unused_variables)]
@@ -348,7 +347,7 @@ impl<'a> BuddyAllocatorMut<'a> {
             // Ensure that no page is free at multiple orders
             while processed < self.len() {
                 let mut found = false;
-                let mut page: u32 = processed.try_into().unwrap();
+                let mut page = processed;
                 for order in 0..=self.get_max_order() {
                     let order_data = get_order_free_bytes(self.data, order);
                     let allocator = BtreeBitmap::new(order_data);
@@ -363,9 +362,7 @@ impl<'a> BuddyAllocatorMut<'a> {
 
             // Ensure that all buddy pages are merged, except at the highest order
             for order in (0..self.get_max_order()).rev() {
-                let order_len: u32 = (self.len() / (2usize.pow(order.try_into().unwrap())))
-                    .try_into()
-                    .unwrap();
+                let order_len = self.len() / (2u32.pow(order.into()));
                 let order_bytes = get_order_free_bytes(self.data, order);
                 let allocator = BtreeBitmap::new(order_bytes);
                 for page in 0..order_len {
@@ -394,16 +391,16 @@ impl<'a> BuddyAllocatorMut<'a> {
         required
     }
 
-    pub(crate) fn capacity(&self) -> usize {
+    pub(crate) fn capacity(&self) -> u32 {
         BuddyAllocator::new(self.data).capacity()
     }
 
-    pub(crate) fn len(&self) -> usize {
-        get_num_pages(self.data) as usize
+    pub(crate) fn len(&self) -> u32 {
+        get_num_pages(self.data)
     }
 
     #[cfg(test)]
-    fn count_allocated_pages(&self) -> usize {
+    fn count_allocated_pages(&self) -> u32 {
         BuddyAllocator::new(self.data).count_allocated_pages()
     }
 
@@ -582,7 +579,7 @@ mod test {
         for page in 0..num_pages {
             allocator.record_alloc(page, 0);
         }
-        assert_eq!(allocator.count_allocated_pages(), num_pages as usize);
+        assert_eq!(allocator.count_allocated_pages(), num_pages);
 
         assert!(allocator.alloc(0).is_none());
 
@@ -625,7 +622,7 @@ mod test {
         for order in 0..=max_order {
             allocated.push((allocator.alloc(order).unwrap(), order));
         }
-        assert_eq!(allocator.count_allocated_pages(), (num_pages - 1) as usize);
+        assert_eq!(allocator.count_allocated_pages(), num_pages - 1);
 
         for order in 1..=max_order {
             assert!(allocator.alloc(order).is_none());
diff --git a/src/tree_store/page_store/page_manager.rs b/src/tree_store/page_store/page_manager.rs
index 0351361..b6bd224 100644
--- a/src/tree_store/page_store/page_manager.rs
+++ b/src/tree_store/page_store/page_manager.rs
@@ -28,8 +28,8 @@ use std::sync::Mutex;
 const MAX_USABLE_REGION_SPACE: u64 = 4 * 1024 * 1024 * 1024;
 // TODO: remove this constant?
 pub(crate) const MAX_MAX_PAGE_ORDER: u8 = 20;
-pub(super) const MIN_USABLE_PAGES: usize = 10;
-const MIN_DESIRED_USABLE_BYTES: usize = 1024 * 1024;
+pub(super) const MIN_USABLE_PAGES: u32 = 10;
+const MIN_DESIRED_USABLE_BYTES: u64 = 1024 * 1024;
 
 // TODO: allocate more tracker space when it becomes exhausted, and remove this hard coded 1000 regions
 const NUM_REGIONS: u32 = 1000;
@@ -291,7 +291,7 @@ impl Allocators {
                 let last_region = new_layout
                     .trailing_region_layout()
                     .unwrap_or_else(|| new_layout.full_region_layout());
-                match (last_region.num_pages() as usize).cmp(&allocator.len()) {
+                match last_region.num_pages().cmp(&allocator.len()) {
                     cmp::Ordering::Less => true,
                     cmp::Ordering::Equal => {
                         // No-op
@@ -318,8 +318,8 @@ impl Allocators {
                 .unwrap_or_else(|| new_layout.full_region_layout());
             let mut region = RegionHeaderMutator::new(self.region_headers.last_mut().unwrap());
             let mut allocator = region.allocator_mut();
-            if allocator.len() > last_region.num_pages() as usize {
-                allocator.resize(last_region.num_pages() as usize);
+            if allocator.len() > last_region.num_pages() {
+                allocator.resize(last_region.num_pages());
             }
         } else {
             let old_num_regions = self.region_headers.len();
@@ -327,10 +327,10 @@ impl Allocators {
                 let new_region = new_layout.region_layout(i);
                 if (i as usize) < old_num_regions {
                     let mut region = RegionHeaderMutator::new(&mut self.region_headers[i as usize]);
-                    assert!(new_region.num_pages() as usize >= region.allocator_mut().len());
-                    if new_region.num_pages() as usize != region.allocator_mut().len() {
+                    assert!(new_region.num_pages() >= region.allocator_mut().len());
+                    if new_region.num_pages() != region.allocator_mut().len() {
                         let mut allocator = region.allocator_mut();
-                        allocator.resize(new_region.num_pages() as usize);
+                        allocator.resize(new_region.num_pages());
                         let highest_free = allocator.highest_free_order().unwrap();
                         region_tracker.mark_free(highest_free, i);
                     }
@@ -407,15 +407,13 @@ impl TransactionalMemory {
     pub(crate) fn new(
         file: File,
         page_size: usize,
-        requested_region_size: Option<usize>,
+        requested_region_size: Option<u64>,
         read_cache_size_bytes: usize,
         write_cache_size_bytes: usize,
     ) -> Result<Self> {
         assert!(page_size.is_power_of_two() && page_size >= DB_HEADER_SIZE);
 
-        let region_size = requested_region_size
-            .map(|x| x as u64)
-            .unwrap_or(MAX_USABLE_REGION_SPACE);
+        let region_size = requested_region_size.unwrap_or(MAX_USABLE_REGION_SPACE);
         assert!(region_size.is_power_of_two());
 
         // TODO: allocate more tracker space when it becomes exhausted, and remove this hard coded 1000 regions
@@ -423,9 +421,10 @@ impl TransactionalMemory {
             RegionTracker::required_bytes(NUM_REGIONS, MAX_MAX_PAGE_ORDER + 1);
 
         // Make sure that there is enough room to allocate the region tracker into a page
-        let size: u64 = max(MIN_DESIRED_USABLE_BYTES, page_size * MIN_USABLE_PAGES)
-            .try_into()
-            .unwrap();
+        let size: u64 = max(
+            MIN_DESIRED_USABLE_BYTES,
+            page_size as u64 * MIN_USABLE_PAGES as u64,
+        );
         let tracker_space =
             (page_size * ((region_tracker_required_bytes + page_size - 1) / page_size)) as u64;
         let starting_size = size + tracker_space;
@@ -903,9 +902,7 @@ impl TransactionalMemory {
             let last_region_index = restore.num_regions() - 1;
             let last_region = restore.region_layout(last_region_index);
             let mut region = state.get_region_mut(last_region_index);
-            region
-                .allocator_mut()
-                .resize(last_region.num_pages() as usize);
+            region.allocator_mut().resize(last_region.num_pages());
 
             *layout = InProgressLayout {
                 layout: restore,
@@ -1386,13 +1383,13 @@ impl TransactionalMemory {
         })
     }
 
-    pub(crate) fn count_allocated_pages(&self) -> Result<usize> {
+    pub(crate) fn count_allocated_pages(&self) -> Result<u64> {
         let state = self.state.lock().unwrap();
         let layout = self.layout.lock().unwrap();
-        let mut count = 0;
+        let mut count = 0u64;
         for i in 0..layout.layout.num_regions() {
             let region = state.get_region(i);
-            count += region.allocator().count_allocated_pages();
+            count += region.allocator().count_allocated_pages() as u64;
         }
 
         Ok(count)


Test patch:
diff --git a/tests/basic_tests.rs b/tests/basic_tests.rs
index 0b3d5b5..cccf586 100644
--- a/tests/basic_tests.rs
+++ b/tests/basic_tests.rs
@@ -715,7 +715,9 @@ fn insert_reserve() {
     let write_txn = db.begin_write().unwrap();
     {
         let mut table = write_txn.open_table(def).unwrap();
-        let mut reserved = table.insert_reserve("hello", value.len()).unwrap();
+        let mut reserved = table
+            .insert_reserve("hello", value.len().try_into().unwrap())
+            .unwrap();
         reserved.as_mut().copy_from_slice(value.as_bytes());
     }
     write_txn.commit().unwrap();
diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs
index 6a8fc35..122e8df 100644
--- a/tests/integration_tests.rs
+++ b/tests/integration_tests.rs
@@ -1096,7 +1096,7 @@ fn wrong_types() {
 #[test]
 fn tree_balance() {
     const EXPECTED_ORDER: usize = 9;
-    fn expected_height(mut elements: usize) -> usize {
+    fn expected_height(mut elements: usize) -> u32 {
         // Root may have only 2 entries
         let mut height = 1;
         elements /= 2;
@@ -1107,7 +1107,7 @@ fn tree_balance() {
         // Each internal node half-full, plus 1 to round up
         height += (elements as f32).log((EXPECTED_ORDER / 2) as f32) as usize + 1;
 
-        height
+        height.try_into().unwrap()
     }
 
     let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
diff --git a/tests/multimap_tests.rs b/tests/multimap_tests.rs
index 00521e3..e2d9db4 100644
--- a/tests/multimap_tests.rs
+++ b/tests/multimap_tests.rs
@@ -271,7 +271,7 @@ fn efficient_storage() {
 
     let read_txn = db.begin_read().unwrap();
     let table = read_txn.open_multimap_table(table_def).unwrap();
-    assert_eq!(table.len().unwrap(), entries as usize);
+    assert_eq!(table.len().unwrap(), entries as u64);
 }
 
 #[test]