From 072c81c266c1e5b8a00c2d41b88bbee57a18b155 Mon Sep 17 00:00:00 2001 From: Christopher Berner Date: Sun, 25 Aug 2024 15:35:27 -0700 Subject: [PATCH] Improve compaction Fixes an edge case where leaf pages might not be compacted, if one of their parent pages could not be compacted to a lower index --- src/tree_store/btree.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/tree_store/btree.rs b/src/tree_store/btree.rs index f51c5150..ae1eb649 100644 --- a/src/tree_store/btree.rs +++ b/src/tree_store/btree.rs @@ -187,16 +187,11 @@ impl UntypedBtreeMut { CachePriority::default_btree(old_page.memory()), )?; let new_page_number = new_page.get_page_number(); - // TODO: we should only bail out if we're at a leaf page. Branch pages still need to process their children - if !new_page_number.is_before(page_number) { - drop(new_page); - self.mem.free(new_page_number); - return Ok(None); - } new_page.memory_mut().copy_from_slice(old_page.memory()); let node_mem = old_page.memory(); + let mut changed = false; match node_mem[0] { LEAF => { // No-op @@ -208,18 +203,25 @@ impl UntypedBtreeMut { let child = accessor.child_page(i).unwrap(); if let Some((new_child, new_checksum)) = self.relocate_helper(child)? { mutator.write_child_page(i, new_child, new_checksum); + changed = true; } } } _ => unreachable!(), } - let mut freed_pages = self.freed_pages.lock().unwrap(); - if !self.mem.free_if_uncommitted(page_number) { - freed_pages.push(page_number); - } + if changed || new_page_number.is_before(page_number) { + let mut freed_pages = self.freed_pages.lock().unwrap(); + if !self.mem.free_if_uncommitted(page_number) { + freed_pages.push(page_number); + } - Ok(Some((new_page_number, DEFERRED))) + Ok(Some((new_page_number, DEFERRED))) + } else { + drop(new_page); + self.mem.free(new_page_number); + Ok(None) + } } }