From 8beb38b50c135991a15958716612463cc78b87ca Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sun, 19 Nov 2023 00:56:46 +0200 Subject: [PATCH] refactor(tvix/store/fs): simpllify read We can just use take(size) to restrict reading to that as a max. Change-Id: I0fbda74e4fb98ffeababae86a325233416029acf Reviewed-on: https://cl.tvl.fyi/c/depot/+/10072 Reviewed-by: raitobezarius Autosubmit: flokli Tested-by: BuildkiteCI --- tvix/store/src/fs/mod.rs | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/tvix/store/src/fs/mod.rs b/tvix/store/src/fs/mod.rs index ee0348c1f..f942cc962 100644 --- a/tvix/store/src/fs/mod.rs +++ b/tvix/store/src/fs/mod.rs @@ -26,7 +26,7 @@ use std::{ time::Duration, }; use tokio::{ - io::{AsyncBufReadExt, AsyncSeekExt}, + io::{AsyncReadExt, AsyncSeekExt}, sync::mpsc, }; use tracing::{debug, info_span, instrument, warn}; @@ -614,24 +614,8 @@ impl FileSystem for TvixStoreFs { let mut buf: Vec = Vec::with_capacity(size as usize); - while (buf.len() as u64) < size as u64 { - let int_buf = blob_reader.fill_buf().await?; - // copy things from the internal buffer into buf to fill it till up until size - - // an empty buffer signals we reached EOF. - if int_buf.is_empty() { - break; - } - - // calculate how many bytes we can read from int_buf. - // It's either all of int_buf, or the number of bytes missing in buf to reach size. - let len_to_copy = std::cmp::min(int_buf.len(), size as usize - buf.len()); - - // copy these bytes into our buffer - buf.extend_from_slice(&int_buf[..len_to_copy]); - // and consume them in the buffered reader. - blob_reader.consume(len_to_copy); - } + // copy things from the internal buffer into buf to fill it till up until size + tokio::io::copy(&mut blob_reader.as_mut().take(size as u64), &mut buf).await?; Ok(buf) });