From 28ae96f49fc8a46a74f76a3c41b978783890aff0 Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Thu, 17 Jul 2025 12:42:32 -0300 Subject: [PATCH] remove confusing casting from usize -> u16 -> usize for usable space --- core/storage/btree.rs | 22 +++++++++++----------- core/storage/sqlite3_ondisk.rs | 8 ++++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core/storage/btree.rs b/core/storage/btree.rs index 1e83592a2..c2f747e73 100644 --- a/core/storage/btree.rs +++ b/core/storage/btree.rs @@ -837,8 +837,8 @@ impl BTreeCursor { page_type: PageType, usable_size: usize, ) -> Result<(usize, usize)> { - let max_local = payload_overflow_threshold_max(page_type, usable_size as u16); - let min_local = payload_overflow_threshold_min(page_type, usable_size as u16); + let max_local = payload_overflow_threshold_max(page_type, usable_size); + let min_local = payload_overflow_threshold_min(page_type, usable_size); // This matches btreeParseCellAdjustSizeForOverflow logic let n_local = if payload_len <= max_local { @@ -2206,7 +2206,7 @@ impl BTreeCursor { &mut cell_payload, cell_idx, record, - self.usable_space() as u16, + self.usable_space(), self.pager.clone(), ); @@ -4890,7 +4890,7 @@ impl BTreeCursor { &mut new_payload, cell_idx, record, - self.usable_space() as u16, + self.usable_space(), self.pager.clone(), ); @@ -6403,7 +6403,7 @@ fn fill_cell_payload( cell_payload: &mut Vec, cell_idx: usize, record: &ImmutableRecord, - usable_space: u16, + usable_space: usize, pager: Rc, ) { // TODO: make record raw from start, having to serialize is not good @@ -6498,13 +6498,13 @@ fn fill_cell_payload( /// - Give a minimum fanout of 4 for index b-trees /// - Ensure enough payload is on the b-tree page that the record header can usually be accessed /// without consulting an overflow page -pub fn payload_overflow_threshold_max(page_type: PageType, usable_space: u16) -> usize { +pub fn payload_overflow_threshold_max(page_type: PageType, usable_space: usize) -> usize { match page_type { PageType::IndexInterior | PageType::IndexLeaf => { - ((usable_space as usize - 12) * 64 / 255) - 23 // Index page formula + ((usable_space - 12) * 64 / 255) - 23 // Index page formula } PageType::TableInterior | PageType::TableLeaf => { - usable_space as usize - 35 // Table leaf page formula + usable_space - 35 // Table leaf page formula } } } @@ -6518,9 +6518,9 @@ pub fn payload_overflow_threshold_max(page_type: PageType, usable_space: u16) -> /// - Otherwise: store M bytes on page /// /// The remaining bytes are stored on overflow pages in both cases. -pub fn payload_overflow_threshold_min(_page_type: PageType, usable_space: u16) -> usize { +pub fn payload_overflow_threshold_min(_page_type: PageType, usable_space: usize) -> usize { // Same formula for all page types - ((usable_space as usize - 12) * 32 / 255) - 23 + ((usable_space - 12) * 32 / 255) - 23 } /// Drop a cell from a page. @@ -8746,7 +8746,7 @@ mod tests { &mut payload, cell_idx as usize, &record, - pager.usable_space() as u16, + pager.usable_space(), pager.clone(), ); insert_into_cell( diff --git a/core/storage/sqlite3_ondisk.rs b/core/storage/sqlite3_ondisk.rs index 3151136c6..979b2ef9f 100644 --- a/core/storage/sqlite3_ondisk.rs +++ b/core/storage/sqlite3_ondisk.rs @@ -622,9 +622,9 @@ impl PageContent { let cell_pointer = self.read_u16_no_offset(cell_pointer) as usize; let start = cell_pointer; let payload_overflow_threshold_max = - payload_overflow_threshold_max(self.page_type(), usable_size as u16); + payload_overflow_threshold_max(self.page_type(), usable_size); let payload_overflow_threshold_min = - payload_overflow_threshold_min(self.page_type(), usable_size as u16); + payload_overflow_threshold_min(self.page_type(), usable_size); let len = match self.page_type() { PageType::IndexInterior => { let (len_payload, n_payload) = read_varint(&buf[cell_pointer + 4..]).unwrap(); @@ -879,8 +879,8 @@ pub fn read_btree_cell( usable_size: usize, ) -> Result { let page_type = page_content.page_type(); - let max_local = payload_overflow_threshold_max(page_type, usable_size as u16); - let min_local = payload_overflow_threshold_min(page_type, usable_size as u16); + let max_local = payload_overflow_threshold_max(page_type, usable_size); + let min_local = payload_overflow_threshold_min(page_type, usable_size); match page_type { PageType::IndexInterior => { let mut pos = pos;