From 1e2ddf7c7b795569eb94554a12fd02671cee164b Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 30 Aug 2020 21:08:50 +0100 Subject: [PATCH] rage-mount: Use os-thread-local to cache read buffers --- Cargo.lock | 11 +++++++++++ rage/Cargo.toml | 3 ++- rage/src/bin/rage-mount/tar.rs | 23 +++++++++++++++-------- rage/src/bin/rage-mount/zip.rs | 33 ++++++++++++++++++++------------- 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e07f6b0..1dcdcaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1591,6 +1591,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "os-thread-local" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd7fc7fa9ea7dc8907f9b10e730106ed0011926e7f5abb382530ac91d1af2b7c" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "os_str_bytes" version = "6.1.0" @@ -1903,6 +1913,7 @@ dependencies = [ "libc", "log", "man", + "os-thread-local", "pinentry", "rust-embed", "tar", diff --git a/rage/Cargo.toml b/rage/Cargo.toml index f431df8..f399524 100644 --- a/rage/Cargo.toml +++ b/rage/Cargo.toml @@ -71,6 +71,7 @@ ctrlc = { version = "3.2", optional = true } fuse_mt = { version = "0.6.0", optional = true } fuser = { version = "0.11.1", optional = true } libc = { version = "0.2", optional = true } +os-thread-local = { version = "0.1.3", optional = true } tar = { version = "0.4", optional = true } time = { version = "0.3.7", optional = true } zip = { version = "0.6.2", optional = true } @@ -83,7 +84,7 @@ man = "0.3" [features] default = ["ssh"] -mount = ["ctrlc", "fuse_mt", "fuser", "libc", "tar", "time", "zip"] +mount = ["ctrlc", "fuse_mt", "fuser", "libc", "os-thread-local", "tar", "time", "zip"] ssh = ["age/ssh"] unstable = ["age/unstable"] diff --git a/rage/src/bin/rage-mount/tar.rs b/rage/src/bin/rage-mount/tar.rs index 9f4e071..47e5533 100644 --- a/rage/src/bin/rage-mount/tar.rs +++ b/rage/src/bin/rage-mount/tar.rs @@ -1,5 +1,7 @@ use age::{armor::ArmoredReader, stream::StreamReader}; use fuse_mt::*; +use os_thread_local::ThreadLocal; +use std::cell::RefCell; use std::collections::HashMap; use std::fs::File; use std::io::{self, BufReader, Read, Seek, SeekFrom}; @@ -111,6 +113,7 @@ pub struct AgeTarFs { file_map: HashMap, open_dirs: Mutex<(HashMap, u64)>, open_files: Mutex<(HashMap, u64)>, + read_buf: ThreadLocal>>, } impl AgeTarFs { @@ -145,6 +148,7 @@ impl AgeTarFs { file_map, open_dirs: Mutex::new((HashMap::new(), 0)), open_files: Mutex::new((HashMap::new(), 0)), + read_buf: ThreadLocal::new(|| RefCell::new(vec![])), }) } } @@ -268,14 +272,17 @@ impl FilesystemMT for AgeTarFs { return callback(Err(libc::EIO)); } - // Read bytes - let to_read = usize::min(size as usize, (file_size - offset) as usize); - let mut buf = vec![]; - buf.resize(to_read, 0); - match inner.read_exact(&mut buf) { - Ok(_) => callback(Ok(&buf)), - Err(_) => callback(Err(libc::EIO)), - } + self.read_buf.with(|buf| { + let mut buf = buf.borrow_mut(); + + // Read bytes + let to_read = usize::min(size as usize, (file_size - offset) as usize); + buf.resize(to_read, 0); + match inner.read_exact(&mut buf) { + Ok(_) => callback(Ok(&buf)), + Err(_) => callback(Err(libc::EIO)), + } + }) } else { callback(Err(libc::EBADF)) } diff --git a/rage/src/bin/rage-mount/zip.rs b/rage/src/bin/rage-mount/zip.rs index 30b7e24..b2aa192 100644 --- a/rage/src/bin/rage-mount/zip.rs +++ b/rage/src/bin/rage-mount/zip.rs @@ -1,5 +1,7 @@ use age::{armor::ArmoredReader, stream::StreamReader}; use fuse_mt::*; +use os_thread_local::ThreadLocal; +use std::cell::RefCell; use std::collections::HashMap; use std::fs::File; use std::io::{self, BufReader, Read}; @@ -89,6 +91,7 @@ pub struct AgeZipFs { dir_map: HashMap>, open_dirs: Mutex<(HashMap, u64)>, open_files: Mutex<(HashMap, u64)>, + read_buf: ThreadLocal>>, } impl AgeZipFs { @@ -117,6 +120,7 @@ impl AgeZipFs { dir_map, open_dirs: Mutex::new((HashMap::new(), 0)), open_files: Mutex::new((HashMap::new(), 0)), + read_buf: ThreadLocal::new(|| RefCell::new(vec![])), }) } } @@ -232,20 +236,23 @@ impl FilesystemMT for AgeZipFs { return callback(Err(libc::EINVAL)); } - // Skip to offset - let mut buf = vec![]; - buf.resize(offset as usize, 0); - if zf.read_exact(&mut buf).is_err() { - return callback(Err(libc::EIO)); - } + self.read_buf.with(|buf| { + let mut buf = buf.borrow_mut(); - // Read bytes - let to_read = usize::min(size as usize, (zf.size() - offset) as usize); - buf.resize(to_read, 0); - match zf.read_exact(&mut buf) { - Ok(_) => callback(Ok(&buf)), - Err(_) => callback(Err(libc::EIO)), - } + // Skip to offset + buf.resize(offset as usize, 0); + if zf.read_exact(&mut buf).is_err() { + return callback(Err(libc::EIO)); + } + + // Read bytes + let to_read = usize::min(size as usize, (zf.size() - offset) as usize); + buf.resize(to_read, 0); + match zf.read_exact(&mut buf) { + Ok(_) => callback(Ok(&buf)), + Err(_) => callback(Err(libc::EIO)), + } + }) } None => callback(Err(libc::EBADF)), }