Skip to content

Conversation

@PThorpe92
Copy link
Collaborator

@PThorpe92 PThorpe92 commented Nov 21, 2025

This PR implements cache spilling so we can remove the ridiculous 100k page default cache size :)

Successfully tested on TPC-H queries with page_cache size=100 pages

Makes some attempt at preventing thrashing by tracking how many times a page is 'touched' in between 'clear_dirty' and counting that as 'dirty_generation', then intentionally spilling only the coldest pages first.

EDIT: Might need to go back through and self-review this tomorrow, as I haven't touched this in a couple weeks, I just fixed the last remaining bug and pushed.

Copy link
Collaborator

@jussisaurio jussisaurio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before I look at this more, submitting these comments since the use of return_if_io!() is problematic for async IO

let past_rightmost_pointer = cell_count as i32 + 1;
self.stack.set_cell_index(past_rightmost_pointer);
let (page, c) = self.read_page(rightmost_pointer as i64)?;
let (page, c) = return_if_io!(self.read_page(rightmost_pointer as i64));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't re-entrant in async IO - i32::MAX is a sentinel value in backwards iteration to signify we have just started iterating this page backwards. If we:

self.stack.set_cell_index(past_rightmost_pointer);
// return IO here
let (page, c) = return_if_io!(self.read_page(rightmost_pointer as i64));

Then we never push the rightmost child page to the page stack and will have mutated stack.cell_index when we enter the function again.

We are already returning a completion here, is return_if_io! necessary?

Copy link
Collaborator Author

@PThorpe92 PThorpe92 Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I half-vibecoded, then fixed, making all of this reentrant... the fuzz tests that were failing previously are passing.

TBH I'm not sure exactly what the best ergonomics are for the cases where we are already returning a completion.. it doesn't fit exactly because now its an IOResult<(PageRef, Option<Completion>)>.

}

let (mem_page, c) = self.read_page(left_child_page as i64)?;
let (mem_page, c) = return_if_io!(self.read_page(left_child_page as i64));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same deal here - we might've just called self.stack.retreat() and then return on IO without pushing mem_page to the stack.

We are already returning a completion here, is return_if_io! necessary?

loop {
if self.read_overflow_state.borrow().is_none() {
let (page, c) = self.read_page(start_next_page as i64)?;
let (page, c) = return_if_io!(self.read_page(start_next_page as i64));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are already returning a completion here, is return_if_io! necessary?


if *remaining_to_read != 0 && next != 0 {
let (new_page, c) = self.pager.read_page(next as i64)?;
let (new_page, c) = return_if_io!(self.pager.read_page(next as i64));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not re-entrant. We are already returning a completion here, is return_if_io! necessary?

// Read page
let (page, c) = self.read_page(first_overflow_page.unwrap() as i64)?;

let (page, c) = return_if_io!(self.read_page(first_overflow_page.unwrap() as i64));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are already returning a completion here, is return_if_io! necessary?


let (next_page, c) = self.read_page(next as i64)?;

let (next_page, c) = return_if_io!(self.read_page(next as i64));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are already returning a completion here, is return_if_io! necessary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants