Skip to content

Commit

Permalink
Improve compaction
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cberner committed Aug 26, 2024
1 parent 0d94695 commit 072c81c
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/tree_store/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}

Expand Down

0 comments on commit 072c81c

Please sign in to comment.