use std::collections::HashMap;
use std::fs::File;
use std::io::Write as IoWrite;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
use std::time::{SystemTime, UNIX_EPOCH};
use memmap2::Mmap;
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
pub const SHARD_MAGIC: u32 = 0x56_49_4D_4C;
pub const SHARD_FORMAT_VERSION: u32 = 3;
#[derive(Archive, RkyvDeserialize, RkyvSerialize, Debug, Clone)]
#[archive(check_bytes)]
pub struct ShardHeader {
pub magic: u32,
pub format_version: u32,
pub vimlrs_version: String,
pub pointer_width: u32,
pub built_at_secs: u64,
}
#[derive(Archive, RkyvDeserialize, RkyvSerialize, Debug, Clone)]
#[archive(check_bytes)]
pub struct ScriptEntry {
pub mtime_secs: i64,
pub mtime_nsecs: i64,
pub binary_mtime_at_cache: i64,
pub cached_at_secs: i64,
pub chunk_blob: Vec<u8>,
}
#[derive(Archive, RkyvDeserialize, RkyvSerialize, Debug, Clone)]
#[archive(check_bytes)]
pub struct ScriptShard {
pub header: ShardHeader,
pub entries: HashMap<String, ScriptEntry>,
}
pub struct MmappedShard {
_mmap: Mmap,
archived: *const ArchivedScriptShard,
}
unsafe impl Send for MmappedShard {}
unsafe impl Sync for MmappedShard {}
impl MmappedShard {
pub fn open(path: &Path) -> Option<Self> {
let file = File::open(path).ok()?;
let mmap = unsafe { Mmap::map(&file).ok()? };
let archived = rkyv::check_archived_root::<ScriptShard>(&mmap[..]).ok()?;
let archived = archived as *const ArchivedScriptShard;
Some(Self {
_mmap: mmap,
archived,
})
}
fn shard(&self) -> &ArchivedScriptShard {
unsafe { &*self.archived }
}
fn header_ok(&self) -> bool {
let h = &self.shard().header;
let magic: u32 = h.magic.into();
let fv: u32 = h.format_version.into();
let pw: u32 = h.pointer_width.into();
magic == SHARD_MAGIC
&& fv == SHARD_FORMAT_VERSION
&& pw as usize == std::mem::size_of::<usize>()
&& h.vimlrs_version.as_str() == env!("CARGO_PKG_VERSION")
}
fn lookup(&self, path: &str) -> Option<&ArchivedScriptEntry> {
self.shard().entries.get(path)
}
}
pub struct ScriptCache {
path: PathBuf,
lock_path: PathBuf,
mmap: Mutex<Option<MmappedShard>>,
}
impl ScriptCache {
pub fn open(path: &Path) -> std::io::Result<Self> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let parent = path.parent().unwrap_or_else(|| Path::new("/tmp"));
let lock_path = parent.join(format!(
"{}.lock",
path.file_name()
.and_then(|s| s.to_str())
.unwrap_or("scripts.rkyv")
));
Ok(Self {
path: path.to_path_buf(),
lock_path,
mmap: Mutex::new(None),
})
}
fn ensure_mmap(&self) {
let mut guard = self.mmap.lock().unwrap();
if guard.is_none() {
*guard = MmappedShard::open(&self.path);
}
}
fn invalidate_mmap(&self) {
*self.mmap.lock().unwrap() = None;
}
pub fn get(&self, path: &str, mtime_secs: i64, mtime_nsecs: i64) -> Option<Vec<u8>> {
self.ensure_mmap();
let guard = self.mmap.lock().unwrap();
let shard = guard.as_ref()?;
if !shard.header_ok() {
return None;
}
let entry = shard.lookup(path)?;
let entry_mtime_s: i64 = entry.mtime_secs.into();
let entry_mtime_ns: i64 = entry.mtime_nsecs.into();
if entry_mtime_s != mtime_secs || entry_mtime_ns != mtime_nsecs {
return None;
}
if let Some(bin_mtime) = current_binary_mtime_secs() {
let cached_bin_mtime: i64 = entry.binary_mtime_at_cache.into();
if cached_bin_mtime < bin_mtime {
return None;
}
}
Some(entry.chunk_blob.as_slice().to_vec())
}
pub fn put(
&self,
path: &str,
mtime_secs: i64,
mtime_nsecs: i64,
chunk_blob: Vec<u8>,
) -> std::io::Result<()> {
let _lock = match acquire_lock(&self.lock_path) {
Some(l) => l,
None => return Ok(()),
};
let mut shard = match read_owned_shard(&self.path) {
Some(s)
if s.header.vimlrs_version == env!("CARGO_PKG_VERSION")
&& s.header.pointer_width as usize == std::mem::size_of::<usize>()
&& s.header.format_version == SHARD_FORMAT_VERSION =>
{
s
}
_ => fresh_shard(),
};
let bin_mtime = current_binary_mtime_secs().unwrap_or(0);
shard.entries.insert(
path.to_string(),
ScriptEntry {
mtime_secs,
mtime_nsecs,
binary_mtime_at_cache: bin_mtime,
cached_at_secs: now_secs(),
chunk_blob,
},
);
shard.header.built_at_secs = now_secs() as u64;
write_shard_atomic(&self.path, &shard)?;
self.invalidate_mmap();
Ok(())
}
pub fn evict_stale(&self) -> usize {
let _lock = match acquire_lock(&self.lock_path) {
Some(l) => l,
None => return 0,
};
let mut shard = match read_owned_shard(&self.path) {
Some(s) => s,
None => return 0,
};
let before = shard.entries.len();
shard.entries.retain(|p, e| match file_mtime(Path::new(p)) {
Some((s, ns)) => s == e.mtime_secs && ns == e.mtime_nsecs,
None => false,
});
let evicted = before - shard.entries.len();
if evicted > 0 {
let _ = write_shard_atomic(&self.path, &shard);
self.invalidate_mmap();
}
evicted
}
pub fn clear(&self) -> std::io::Result<()> {
let _lock = acquire_lock(&self.lock_path);
let res = match std::fs::remove_file(&self.path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e),
};
self.invalidate_mmap();
res
}
}
fn acquire_lock(path: &Path) -> Option<nix::fcntl::Flock<File>> {
let f = File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(path)
.ok()?;
nix::fcntl::Flock::lock(f, nix::fcntl::FlockArg::LockExclusive).ok()
}
fn fresh_shard() -> ScriptShard {
ScriptShard {
header: ShardHeader {
magic: SHARD_MAGIC,
format_version: SHARD_FORMAT_VERSION,
vimlrs_version: env!("CARGO_PKG_VERSION").to_string(),
pointer_width: std::mem::size_of::<usize>() as u32,
built_at_secs: now_secs() as u64,
},
entries: HashMap::new(),
}
}
fn read_owned_shard(path: &Path) -> Option<ScriptShard> {
let bytes = std::fs::read(path).ok()?;
let archived = rkyv::check_archived_root::<ScriptShard>(&bytes[..]).ok()?;
archived.deserialize(&mut rkyv::Infallible).ok()
}
fn write_shard_atomic(path: &Path, shard: &ScriptShard) -> std::io::Result<()> {
let bytes = rkyv::to_bytes::<_, 4096>(shard)
.map_err(|e| std::io::Error::other(format!("rkyv serialize: {e}")))?;
let parent = path.parent().expect("cache path has parent");
let _ = std::fs::create_dir_all(parent);
let pid = std::process::id();
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let tmp_path = parent.join(format!(
"{}.tmp.{pid}.{nanos}",
path.file_name()
.and_then(|s| s.to_str())
.unwrap_or("scripts.rkyv")
));
{
let mut f = File::create(&tmp_path)?;
f.write_all(&bytes)?;
f.sync_all()?;
}
std::fs::rename(&tmp_path, path)?;
Ok(())
}
fn now_secs() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0)
}
pub fn file_mtime(path: &Path) -> Option<(i64, i64)> {
use std::os::unix::fs::MetadataExt;
let meta = std::fs::metadata(path).ok()?;
Some((meta.mtime(), meta.mtime_nsec()))
}
fn current_binary_mtime_secs() -> Option<i64> {
static BIN_MTIME: OnceLock<Option<i64>> = OnceLock::new();
*BIN_MTIME.get_or_init(|| {
let exe = std::env::current_exe().ok()?;
let (secs, _) = file_mtime(&exe)?;
Some(secs)
})
}
pub fn default_cache_path() -> PathBuf {
dirs::cache_dir()
.or_else(dirs::home_dir)
.unwrap_or_else(|| PathBuf::from("/tmp"))
.join("vimlrs/scripts.rkyv")
}
pub fn cache_enabled() -> bool {
!matches!(
std::env::var("VIMLRS_CACHE").as_deref(),
Ok("0") | Ok("false") | Ok("no")
)
}
pub static CACHE: once_cell::sync::Lazy<Option<ScriptCache>> = once_cell::sync::Lazy::new(|| {
if !cache_enabled() {
return None;
}
ScriptCache::open(&default_cache_path()).ok()
});
pub fn try_load(path: &Path) -> Option<crate::compile_viml::CompiledProgram> {
let cache = CACHE.as_ref()?;
let canonical = path.canonicalize().ok()?;
let key = canonical.to_str()?;
let (mtime_s, mtime_ns) = file_mtime(&canonical)?;
let blob = cache.get(key, mtime_s, mtime_ns)?;
bincode::deserialize::<crate::compile_viml::CompiledProgram>(&blob).ok()
}
pub fn store(path: &Path, prog: &crate::compile_viml::CompiledProgram) {
let Some(cache) = CACHE.as_ref() else {
return;
};
let Ok(canonical) = path.canonicalize() else {
return;
};
let Some(key) = canonical.to_str() else {
return;
};
let Some((mtime_s, mtime_ns)) = file_mtime(&canonical) else {
return;
};
let Ok(blob) = bincode::serialize(prog) else {
return;
};
let _ = cache.put(key, mtime_s, mtime_ns, blob);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shard_roundtrip_via_rkyv() {
let mut shard = fresh_shard();
shard.entries.insert(
"/tmp/x.vim".to_string(),
ScriptEntry {
mtime_secs: 1,
mtime_nsecs: 2,
binary_mtime_at_cache: 3,
cached_at_secs: 4,
chunk_blob: vec![9, 9, 9],
},
);
let bytes = rkyv::to_bytes::<_, 4096>(&shard).unwrap();
let archived = rkyv::check_archived_root::<ScriptShard>(&bytes[..]).unwrap();
let back: ScriptShard = archived.deserialize(&mut rkyv::Infallible).unwrap();
assert_eq!(back.entries["/tmp/x.vim"].chunk_blob, vec![9, 9, 9]);
assert_eq!(back.header.magic, SHARD_MAGIC);
}
}