rage-mount: Use os-thread-local to cache read buffers

This commit is contained in:
Jack Grigg 2020-08-30 21:08:50 +01:00
parent 59c9997f78
commit 1e2ddf7c7b
4 changed files with 48 additions and 22 deletions

11
Cargo.lock generated
View file

@ -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",

View file

@ -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"]

View file

@ -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<PathBuf, (FileAttr, u64)>,
open_dirs: Mutex<(HashMap<u64, PathBuf>, u64)>,
open_files: Mutex<(HashMap<u64, OpenFile>, u64)>,
read_buf: ThreadLocal<RefCell<Vec<u8>>>,
}
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))
}

View file

@ -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<PathBuf, Vec<DirectoryEntry>>,
open_dirs: Mutex<(HashMap<u64, PathBuf>, u64)>,
open_files: Mutex<(HashMap<u64, usize>, u64)>,
read_buf: ThreadLocal<RefCell<Vec<u8>>>,
}
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)),
}