Using an LruCache instead of a HashMap for loaded pages in a tag
Dependencies
- [2]
N3X5YP7PAdding tag/txn.rs, now that the parser allows it
Change contents
- edit in libpijul/src/tag/txn.rs at line 3
use std::collections::{hash_map::Entry, HashMap}; - edit in libpijul/src/tag/txn.rs at line 4
/// Size of the LRU cache.const CACHE_SIZE: usize = 1024; - replacement in libpijul/src/tag/txn.rs at line 34
loaded: Mutex<HashMap<u64, Box<[u8; crate::tag::BLOCK_SIZE]>>>,loaded: Mutex<lru_cache::LruCache<u64, Box<[u8; crate::tag::BLOCK_SIZE]>>>, - replacement in libpijul/src/tag/txn.rs at line 53
pub fn new<P: AsRef<std::path::Path>>(p: P) -> Result<Self, TagError> {pub fn new<P: AsRef<std::path::Path>>(p: P, h: &Hash) -> Result<Self, TagError> { - replacement in libpijul/src/tag/txn.rs at line 57
let header: crate::tag::FileHeader = bincode::deserialize(&off)?;let header: crate::tag::FileHeader = bincode::deserialize(&off).map_err(TagError::BincodeDe)?;let mut ch = OpenTagFile {file,header};ch.check(h)?;ch.file.seek(SeekFrom::Start(off.len() as u64))?; - replacement in libpijul/src/tag/txn.rs at line 65
r: file,off: header.channel,r: ch.file,off: ch.header.channel, - replacement in libpijul/src/tag/txn.rs at line 69
header,loaded: Mutex::new(HashMap::new()),header: ch.header,loaded: Mutex::new(lru_cache::LruCache::new(CACHE_SIZE)), - replacement in libpijul/src/tag/txn.rs at line 85
let p = match self.loaded.lock().unwrap().entry(off_aligned) {Entry::Occupied(mut e) => unsafe {e.get_mut().as_mut_ptr().add((off - off_aligned) as usize)},Entry::Vacant(e) => {let mut buf = Box::new([0; crate::tag::BLOCK_SIZE]);self.s.lock().unwrap().decompress(&mut buf[..], off_aligned)?;unsafe { e.insert(buf).as_mut_ptr().add((off - off_aligned) as usize) }let mut l = self.loaded.lock().unwrap();let p = if let Some(p) = l.get_mut(&off_aligned) {unsafe {p.as_mut_ptr().add((off - off_aligned) as usize) - edit in libpijul/src/tag/txn.rs at line 90
} else {let mut buf = Box::new([0; crate::tag::BLOCK_SIZE]);self.s.lock().unwrap().decompress(&mut buf[..], off_aligned)?;let p = unsafe {buf.as_mut_ptr().add((off - off_aligned) as usize)};l.insert(off_aligned, buf);p