Cache is the sixteenth Element, and it is a
process-wide library rather than a process. It remembers work the editor has already done;
it never changes what an operation returns, only how long the operation takes.
- Two independent caches: static assets through
memmap2, canonical file paths throughmoka - Both purely additive - with either or both disabled every call still returns the right answer, only slower
- Bounded staleness: a path entry lives 60 seconds after its last use, so an external rename converges within a minute
Overview 📋
The first cache holds static assets - the JavaScript, CSS and font files that make
up the interface. When the webview asks for one of them, Cache does not read the file
from disk, allocate a buffer and copy the bytes in; it hands back a borrowed slice of a
memmap2 region the operating system already keeps in memory. It also finds a
pre-compressed .br sibling beside the file, so a client that accepts Brotli is served the
smaller bytes without anything being compressed on the fly.
The second cache holds file paths. Resolving a relative or symlinked path to its
canonical absolute form is a filesystem call, and startup makes the same one dozens of
times - extension manifests, JavaScript imports, git scope checks. Cache keeps each
answer in a moka hash table for 60 seconds after its last use, so the second ask is a
lookup rather than a syscall.
| Attribute | Value |
|---|---|
| Language | Rust (edition 2024) |
| Crate type | Library, publish = false |
| Crate root | Source/Library.rs |
| Dependencies | dashmap, dunce, log, memmap2, moka, once_cell, tokio |
| Consumed by | Mountain, Wind, Maintain |
Key Features ⚙️
Memory-Mapped Asset Cache - AssetMemoryMap is a shared, thread-safe dictionary from a
file path to a ready-to-serve entry: the bytes as a memory-mapped region, the MIME type
guessed from the extension, the file size, an optional pre-compressed Brotli sibling and a
weak ETag. The scheme handlers for vscode-file://, tauri:// and land:// look a file
up here and serve it without touching the disk again.
Automatic Brotli Handling - when a file is first loaded, Cache checks whether a .br
sibling exists next to it (those are pre-baked by Maintain). If one is
found it is loaded alongside the original, and a request carrying Accept-Encoding: br is
answered with the compressed bytes directly.
Canonical Path Cache - the path cache holds up to 8 192 resolutions, each living 60
seconds after its last use (time_to_idle = Duration::from_secs(60)). Frequently accessed
paths stay for the session; one-off lookups expire. Two functions are exposed:
Canonicalize checks the cache first and resolves on a miss, and CanonicalizeUncached
always goes to the filesystem, for the callers that need a fresh answer.
Lock-Free Concurrent Reads - DashMap splits the asset cache into independently locked
shards, so threads reading different entries never wait on each other; moka gives the
path cache the same property through its own internal design. Both caches are safe to use
from any number of threads.
Inspection and Reset - both caches report statistics (hit count, miss count, capacity, time before expiry) and accept a full clear or a per-entry invalidate, which is what a development session needs when a file changes and the cache has to pick up the new bytes immediately.
Core Architecture Principles 🏗️
Each principle below is implemented by the components named in its row, so the table is also a map from intent to code.
| Principle | Description | Key Components |
|---|---|---|
| Zero-Copy Serving | Static assets are served as borrowed &[u8] slices of memmap2::Mmap regions - no per-request allocation, no second copy of the kernel page cache, no GC pressure. |
AssetMemoryMap::Entry::Struct, memmap2::Mmap, AssetMemoryMap::Map::Fn |
| Additive Performance | Both caches are transparent acceleration layers: with either or both disabled, consumers produce identical results, only slower. | AssetMemoryMap::LoadOrInsert, PathCanon::Canonicalize, PathCanon::CanonicalizeUncached |
| Bounded Staleness | time_to_idle bounds freshness - hot paths reset the timer, cold paths evict, and an external mutation converges within 60 s. |
moka::sync::Cache<PathBuf, PathBuf>, time_to_idle = Duration::from_secs(60) |
| Thread Safety | DashMap for wait-free sharded asset reads, moka for amortised lock-free path reads, OnceLock / Lazy for one-time global initialisation. |
DashMap<PathBuf, Arc<Entry::Struct>>, Lazy<moka::sync::Cache>, OnceLock |
Module Map 🗺️
| Path | Purpose |
|---|---|
Source/Library.rs |
Crate root; declares the AssetMemoryMap and PathCanon modules |
Source/AssetMemoryMap/Map.rs |
Process-global DashMap<PathBuf, Arc<Entry::Struct>> behind a OnceLock |
Source/AssetMemoryMap.rs, Source/PathCanon.rs |
Module roots; re-export each cache’s public surface |
Source/AssetMemoryMap/Entry.rs |
Mmap, MIME, length, optional Brotli Mmap, weak ETag |
Source/AssetMemoryMap/LoadOrInsert.rs |
Lazy Mmap creation with Brotli sibling auto-detection |
Source/AssetMemoryMap/CacheStats.rs, Stats.rs |
Hit and miss counters, aggregated statistics |
Source/AssetMemoryMap/Invalidate.rs, Clear.rs |
Per-entry eviction and full reset |
Source/AssetMemoryMap/MimeFromExtension.rs |
Extension-to-MIME lookup |
Source/PathCanon/Cache.rs |
Lazy<moka::sync::Cache<PathBuf, PathBuf>> - 8 192 cap, 60 s time-to-idle |
Source/PathCanon/Canonicalize.rs |
Cached dunce::canonicalize; falls through to the filesystem on a miss |
Source/PathCanon/CanonicalizeUncached.rs |
Bypass-cache dunce::canonicalize for a forced fresh answer |
Source/PathCanon/Invalidate.rs, Clear.rs |
Per-path eviction and full reset |
Source/PathCanon/SpawnDiagnosticLogger.rs |
Periodic telemetry emission for the cache statistics |
In the Land Project 🔌
Cache speeds the editor up by remembering results that would otherwise be recomputed. It
never changes what happens, only how fast.
| Consumer | Cache used | Hot-path pattern |
|---|---|---|
Mountain |
AssetMemoryMap |
Scheme-handler asset serving to the Sky / Wind webview |
Mountain |
PathCanon |
FS-scope security gates - extension paths, git scopes, file imports |
Wind |
AssetMemoryMap |
Per-body zero-copy response under Content-Type: application/javascript |
Maintain |
AssetMemoryMap |
Brotli sibling pre-bake - the .br files LoadOrInsert finds |
Resolving every incoming path through the path cache saves roughly 150 ms during a typical
startup: about 113 extension-manifest paths, 80 JavaScript imports and 60 git scope
checks that would each otherwise be a filesystem call.
Related Documentation 📚
- Element record - the six-fact working register and the three-file trace this page is the manual for
- Mountain - the scheme handlers and the FS-scope gates that call in
- Wind - the per-body zero-copy response path
- Element Dependency Map - every build and runtime edge, in one table
- Project Structure - where
Element/Cachesits in the tree
Project Maintainers: Source Open (Source/[email protected]) | GitHub Repository | Report an Issue
The record this page documents, as the site files it: the six facts the claim is checked against, and the three files the manual names to check it.
WORKING, 06
- FORM:
- CACHING PRIMITIVES
- WRITTEN IN:
- RUST
- BOUNDARY:
- IN-PROCESS DASHMAP + MOKA
- DEPENDS ON:
- NONE NO ELEMENT DEPENDENCY
- MANUAL:
-
/DOC/CACHE - EVIDENCE:
- UNAVAILABLE NO MANIFEST IN THE BUILD
# TRACE / CACHE 03 STEPS
- 01
Source/AssetMemoryMap/Map.rsTHE ASSET MAP - 02
Source/PathCanon/Canonicalize.rsTHE PATH CACHE - 03
Source/Library.rsWHAT THE CRATE EXPORTS
SOURCE: /DOC/CACHE