Skip to content

Commit

Permalink
avoid memset for buffer allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
muhamadazmy committed Sep 28, 2023
1 parent a8ed37b commit 7c52fce
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ where
{
store: Arc<BlockStore<S>>,
writer: Writer,
buffer: [u8; BLOB_SIZE],
}

impl<S> Clone for Uploader<S>
Expand All @@ -290,6 +291,7 @@ where
Self {
store: Arc::clone(&self.store),
writer: self.writer.clone(),
buffer: [0; BLOB_SIZE],
}
}
}
Expand All @@ -302,10 +304,11 @@ where
Self {
store: Arc::new(store),
writer,
buffer: [0; BLOB_SIZE],
}
}

async fn upload(&self, ino: Ino, path: &Path) -> Result<()> {
async fn upload(&mut self, ino: Ino, path: &Path) -> Result<()> {
use tokio::fs;
use tokio::io::AsyncReadExt;
use tokio::io::BufReader;
Expand All @@ -314,17 +317,16 @@ where
let fd = fs::OpenOptions::default().read(true).open(path).await?;

let mut reader = BufReader::new(fd);
let mut buffer: [u8; BLOB_SIZE] = [0; BLOB_SIZE];
loop {
let size = reader.read(&mut buffer).await?;
let size = reader.read(&mut self.buffer).await?;
if size == 0 {
break;
}

// write block to remote store
let block = self
.store
.set(&buffer[..size])
.set(&self.buffer[..size])
.await
.context("failed to store blob")?;

Expand All @@ -344,7 +346,7 @@ where
type Input = (Ino, PathBuf);
type Output = ();

async fn run(&self, (ino, path): Self::Input) -> Self::Output {
async fn run(&mut self, (ino, path): Self::Input) -> Self::Output {
if let Err(err) = self.upload(ino, &path).await {
log::error!("failed to upload file ({:?}): {}", path, err);
}
Expand Down

0 comments on commit 7c52fce

Please sign in to comment.