Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow AlignedBuffer to return "owned slices" (like Bytes), which refer to the same buffer in RAM #114

Closed
JackKelly opened this issue Mar 26, 2024 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@JackKelly
Copy link
Owner

Use-cases

  • Merge multiple ranges in get_ranges, and return the individual "owned slices".

Related

Implementation

I think I can't just use Arc<AlignedBuffer> because each clone needs to have its own valid_slice.

@JackKelly
Copy link
Owner Author

JackKelly commented Mar 27, 2024

Some more thoughts on this:

Use-cases

  • Re-use buffers
    • Mark segment as "free to be reused" when all references to that segment go away (i.e. when each segment is Drop'd)
  • Allocate once, and manage re-use ourselves (although maybe this is a premature optimisation?!)

@JackKelly JackKelly changed the title Allow AlignedBytes to return "owned slices" (like Bytes), which refer to the same buffer in RAM Allow AlignedBuffer to return "owned slices" (like Bytes), which refer to the same buffer in RAM Apr 3, 2024
@JackKelly JackKelly pinned this issue Apr 3, 2024
@JackKelly JackKelly unpinned this issue Apr 3, 2024
@JackKelly
Copy link
Owner Author

JackKelly commented Apr 22, 2024

For now, I'm going to ignore re-using buffers.

But I do need to support the two use-cases at the top of this thread:

1. The user requests multiple contiguous byte ranges.

Let's say the user requests byte_ranges: 0..1000, 1000..2000, 2000..3000, 3000..4000.

LSIO then:

  • merges these ranges
  • allocates a single 4000 byte AlignedBufferMut
  • submits a single read operation to the operation system.
  • when the single read op completes, we freeze the buffer, which turns the entire 4,000 byte AlignedBufferMut into a 4,000 byte AlignedBuffer (immutable).
  • Get four owned slices into the AlignedBuffer. Return these to the user.
  • Drop the 4,000 byte slice (but don't drop the underlying buffer!)
  • The buffer is deallocated when all the owned slices are dropped by the user.

2. The user requests a single 8 GB file.

Linux can't read more than 2 GB at once.

LSIO:

  • allocates a single 8 GB AlignedBufferMut
  • splits this into a new 2 GB AlignedBufferMut and the old AlignedBufferMut is reduced to 6 GB. Both of these buffers must have their starts and ends aligned. Then repeat the process to get 4 x 2 GB AlignedBufferMuts.
  • issues four read operations to the OS.
  • when the first, second, and third read ops complete, drop that AlignedBufferMut (but that won't drop the underlying storage, it just removes its reference)
  • when the last read op completes, freeze it to get an immutable view of the 8 GB slice requested by the user.

@JackKelly
Copy link
Owner Author

Implementation

InnerAlignedBuffer needs to keep track of which byte ranges are "owned" by AlignedBufferMuts. Only one owner can see those bytes.

Maybe something like this:

struct InnerAlignedBuffer {
  buf: *mut u8,  // Should be NotNull?
  layout: alloc::Layout,
  exclusive_ranges: Vec<Range<usize>>,
}

impl InnerAlignedBuffer {
  // `AlignedBufferMut.split` must call this function:
  fn get_exclusive_slice(&mut self, slice: Range<usize>) -> Result<()> {
    for exclusive_range in self.exclusive_ranges.iter() {
        if overlaps(slice, exclusive_range) {
          return Err
        }
    }
    Ok(())
  }
  
  // `AlignedBufferMut.drop` must call this function:
  fn remove_exclusive_slice(&mut self, slice: Range<usize>) -> Result<()) {
  }
}

@JackKelly JackKelly moved this from Todo to In Progress in light-speed-io Apr 22, 2024
@JackKelly JackKelly self-assigned this Apr 23, 2024
@JackKelly
Copy link
Owner Author

JackKelly commented Apr 23, 2024

OK, I think the main functionality is implemented in 030920d

Still TODO:

  • Write tests!
  • Simplify! See the TODO items in the code

@JackKelly
Copy link
Owner Author

todo: doctest for 2nd use-case above

@JackKelly
Copy link
Owner Author

Great! I think this is done! I just want to re-read the code with fresh eyes, and then I can close this issue!

@github-project-automation github-project-automation bot moved this from In Progress to Done in light-speed-io Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

No branches or pull requests

1 participant