From 28ae96f49fc8a46a74f76a3c41b978783890aff0 Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Thu, 17 Jul 2025 12:42:32 -0300 Subject: [PATCH 1/2] 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; From 97d2306e266e423e1d9876b96b436e5c38327863 Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Thu, 17 Jul 2025 12:51:08 -0300 Subject: [PATCH 2/2] unwrap on failed usable_space --- core/storage/btree.rs | 4 ++-- core/storage/pager.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/storage/btree.rs b/core/storage/btree.rs index c2f747e73..5a0a3ede6 100644 --- a/core/storage/btree.rs +++ b/core/storage/btree.rs @@ -6440,7 +6440,7 @@ fn fill_cell_payload( let payload_overflow_threshold_min = payload_overflow_threshold_min(page_type, usable_space); // see e.g. https://github.com/sqlite/sqlite/blob/9591d3fe93936533c8c3b0dc4d025ac999539e11/src/dbstat.c#L371 let mut space_left = payload_overflow_threshold_min - + (record_buf.len() - payload_overflow_threshold_min) % (usable_space as usize - 4); + + (record_buf.len() - payload_overflow_threshold_min) % (usable_space - 4); if space_left > payload_overflow_threshold_max { space_left = payload_overflow_threshold_min; @@ -6479,7 +6479,7 @@ fn fill_cell_payload( pointer = unsafe { buf.as_mut_ptr().add(4) }; pointer_to_next = buf.as_mut_ptr(); - space_left = usable_space as usize - 4; + space_left = usable_space - 4; } to_copy_buffer = &to_copy_buffer[to_copy..]; diff --git a/core/storage/pager.rs b/core/storage/pager.rs index 1cabb79ce..88121325f 100644 --- a/core/storage/pager.rs +++ b/core/storage/pager.rs @@ -650,11 +650,11 @@ impl Pager { let page_size = *self .page_size .get() - .get_or_insert_with(|| header_accessor::get_page_size(self).unwrap_or_default()); + .get_or_insert_with(|| header_accessor::get_page_size(self).unwrap()); let reserved_space = *self .reserved_space - .get_or_init(|| header_accessor::get_reserved_space(self).unwrap_or_default()); + .get_or_init(|| header_accessor::get_reserved_space(self).unwrap()); (page_size as usize) - (reserved_space as usize) }