Repository: tokio-rs/tokio
File: tokio/src/fs/read_uring.rs (Rust)

Code before:
        // prepare the spare capacity to be read into
        let buf_len = usize::min(buf.spare_capacity_mut().len(), MAX_READ_SIZE);

        // SAFETY: buf_len cannot be greater than u32::MAX because max_read_size
        // is u32::MAX
        let mut read_len = buf_len as u32;

        // read into spare capacity
        let (size_read, r_fd, r_buf) = op_read(fd, buf, &mut offset, &mut read_len).await?;

        if size_read == 0 {
            return Ok(r_buf);
        }

        fd = r_fd;
        buf = r_buf;
    }
}

async fn small_probe_read(
    fd: OwnedFd,
    mut buf: Vec<u8>,
    offset: &mut u64,
) -> io::Result<(u32, OwnedFd, Vec<u8>)> {
    let mut temp_arr = [0; PROBE_SIZE];
    let mut read_len = PROBE_SIZE_U32;

    let has_enough = buf.len() > PROBE_SIZE;

    if has_enough {
        // if we have more than PROBE_SIZE bytes in the buffer already then
        // don't call reserve as we might potentially read 0 bytes
        let back_bytes_len = buf.len() - PROBE_SIZE;
        temp_arr.copy_from_slice(&buf[back_bytes_len..]);
        // We're decreasing the length of the buffer and len is greater
        // than PROBE_SIZE. So we can read into the discarded length
        buf.truncate(back_bytes_len);
    } else {
        // we don't even have PROBE_SIZE length in the buffer, we need this
        // reservation
        buf.reserve_exact(PROBE_SIZE);
    }

    let (size_read, r_fd, mut r_buf) = op_read(fd, buf, offset, &mut read_len).await?;

    // return early if we inserted into reserved PROBE_SIZE
    // bytes
    if !has_enough {
        Ok((size_read, r_fd, r_buf))
    } else {
        let old_len = r_buf.len() - (size_read as usize);

Review comment (suggestion):
```suggestion
async fn small_probe_read(
    fd: OwnedFd,
    mut buf: Vec<u8>,
    offset: &mut u64,
) -> io::Result<(u32, OwnedFd, Vec<u8>)> {
    if buf.len() < PROBE_SIZE {
        buf.try_reserve(PROBE_SIZE)?;
        return op_read(fd, buf, offset, PROBE_SIZE_U32).await;
    }

    let mut temp_arr = [0; PROBE_SIZE];

    let back_bytes_len = buf.len() - PROBE_SIZE;
    temp_arr.copy_from_slice(&buf[back_bytes_len..]);

    // We're decreasing the length of the buffer and len is greater
    // than PROBE_SIZE. So we can read into the discarded length
    buf.truncate(back_bytes_len);

    let (size_read, r_fd, mut r_buf) = op_read(fd, buf, offset, PROBE_SIZE_U32).await?;

    r_buf.splice(back_bytes_len..back_bytes_len, temp_arr);

    Ok((size_read, r_fd, r_buf))
}
```

Code after:

async fn small_probe_read(
    fd: OwnedFd,
    mut buf: Vec<u8>,
    offset: &mut u64,
) -> io::Result<(OwnedFd, Vec<u8>, bool)> {
    let read_len = PROBE_SIZE_U32;

    let mut temp_arr = [0; PROBE_SIZE];
    // we don't call this function if buffer's length < PROBE_SIZE
    let back_bytes_len = buf.len() - PROBE_SIZE;

    temp_arr.copy_from_slice(&buf[back_bytes_len..]);

    // We're decreasing the length of the buffer and len is greater
    // than PROBE_SIZE. So we can read into the discarded length
    buf.truncate(back_bytes_len);

    let (r_fd, mut r_buf, is_eof) = op_read(fd, buf, offset, read_len).await?;
    // If `size_read` returns zero due to reasons such as buffer's exact fit,
    // then this `try_reserve` does not perform allocation.
    r_buf.try_reserve(PROBE_SIZE)?;
    r_buf.splice(back_bytes_len..back_bytes_len, temp_arr);

    Ok((r_fd, r_buf, is_eof))
}

// Takes a amount of length to read and returns a singluar read in the buffer
//
// Returns the file descriptor, buffer and EOF reached or not
async fn op_read(
    mut fd: OwnedFd,
    mut buf: Vec<u8>,
    offset: &mut u64,
    read_len: u32,
) -> io::Result<(OwnedFd, Vec<u8>, bool)> {
    loop {
        let (res, r_fd, r_buf) = Op::read(fd, buf, read_len, *offset).await;

        match res {
            Err(e) if e.kind() == ErrorKind::Interrupted => {
                buf = r_buf;
                fd = r_fd;
            }
            Err(e) => return Err(e),
            Ok(size_read) => {
                *offset += size_read as u64;

                return Ok((r_fd, r_buf, size_read == 0));
            }
        }