feat(perf): add LRU cache for file metadata - #41
BandiAkarsh wants to merge 1 commit into
Conversation
Following maintainer approach - PR B Changes: - Add cache.rs with LRU cache implementation (no dead_code) - Wire cache into fs stat command - Register cache in app state (10,000 entries) - Add lru = 0.12 dependency to Cargo.toml The cache is integrated into the stat command: - Checks cache first for O(1) lookup - On miss, fetches from filesystem and caches result - Reduces repeated stat() calls for the same files
73a6477 to
fd1ad3f
Compare
- Add cache_invalidate command for frontend to call when files change - Emit cache-invalidate event from watch.rs when files are modified/created/deleted - Wire cache into file watcher for proper invalidation Following maintainer feedback from PR Sidenai#41 review.
QexleOSS
left a comment
There was a problem hiding this comment.
stat does two filesystem reads on every miss. You call ws::stat at line 44, then call std::fs::metadata(&path) again at line 52 just to build the cache entry. Convert the ws::stat result directly and drop the second call, or the miss path stays as slow as no cache plus extra work.
Also the metadata_to_cache_entry helper is duplicated. cache.rs:118 has the &fs::Metadata version and fs.rs:18 has a second Option-returning copy with the same field mapping. Delete one and reuse it in both spots.
FileMetadataCache already wraps the LruCache in Arc<Mutex<...>>, and lib.rs registers it as Arc<Mutex>. That's a mutex behind a mutex. fs.rs locks the outer one, then cache.rs locks the inner. Drop the outer Mutex and just manage Arc.
is_symlink will never be true. std::fs::metadata follows the link, so cache.rs:138 reports false for every symlink. Use symlink_metadata if you intend to cache link type.
This PR has no invalidation path in the diff. remove/clear exist but nothing calls them, and the description points to a separate file-watcher PR. Until that happens, stat returns stale size/modified after a write and never recovers until restart. Worth noting in the description or gating merge on the follow-up.
Clippy and cargo-audit are red right now, so this probably won't merge until those pass.
Summary
Following maintainer approach - PR B
This PR adds an LRU cache for file metadata to improve stat command performance.
Changes
Added
src-tauri/src/commands/cache.rs- LRU cache implementation (10,000 entries)#![allow(dead_code)]- code is actively usedModified
src-tauri/src/commands/fs.rs- stat command wired to use cachesrc-tauri/src/lib.rs- cache registered in app statesrc-tauri/src/commands/mod.rs- cache module addedsrc-tauri/Cargo.toml- lru = 0.12 dependencyHow It Works
The cache is integrated into the
statcommand:Performance Impact
Note
This is following the maintainer's suggested approach: