Skip to main content

Module script_cache

Module script_cache 

Source
Expand description

script_cache submodule. rkyv-backed bytecode cache for scripts.

Single-file shard at ~/.stryke/scripts.rkyv. On 2+ runs of a given script, lex/parse/compile is skipped — the cache hit is mmap + zero-copy ArchivedHashMap lookup + bincode-decode of the inner Program/Chunk blobs.

Storage layout (rkyv archived): ScriptShard { header: { magic, format_version, stryke_version, pointer_width, built_at_secs }, entries: HashMap<canonical_path, ScriptEntry>, } ScriptEntry { mtime_secs, mtime_nsecs, binary_mtime_at_cache, cached_at_secs, program_blob: Vec<u8>, chunk_blob: Vec<u8> }

Inner program_blob / chunk_blob are bincode for now — StrykeValue’s Arc-shared graph and the CacheConst adapter aren’t trivially rkyv-archivable, so phase 1 keeps that codec inside the rkyv outer container. Phase 2 can derive Archive directly on Chunk / Program for true zero-copy load.

Read path:

  • Lazy mmap of the shard, kept alive for the process lifetime so repeat lookups (s test t running 87 scripts) pay validation once.
  • rkyv::check_archived_root::<ScriptShard> validates the byte image.
  • Header validated for magic / format_version / stryke_version / pointer_width.
  • Per-entry: source mtime must match, and binary_mtime_at_cache ≥ running stryke binary’s mtime (any rebuild of stryke invalidates entries silently).

Write path:

  • flock(LOCK_EX) on scripts.rkyv.lock so concurrent writers serialize.
  • Read existing shard into owned form, mutate, rkyv::to_bytes, write to scripts.rkyv.tmp.<pid>.<nanos>, fsync, atomic-rename.
  • Drop the in-process mmap so the next read picks up the new shard.

Modules§

constants_pool_codec
constants_pool_codec submodule.

Structs§

ArchivedScriptEntry
An archived ScriptEntry
ArchivedScriptShard
An archived ScriptShard
ArchivedShardHeader
An archived ShardHeader
CachedScript
Owned bundle handed back from try_load / ScriptCache::get.
MmappedShard
mmap + validated *const ArchivedScriptShard. Self-referential — the pointer is valid for the lifetime of the wrapping struct.
ScriptCache
Shard cache keyed by canonical script path. One per shard file.
ScriptEntry
ScriptEntry — see fields for layout.
ScriptEntryResolver
The resolver for an archived ScriptEntry
ScriptShard
ScriptShard — see fields for layout.
ScriptShardResolver
The resolver for an archived ScriptShard
ShardHeader
ShardHeader — see fields for layout.
ShardHeaderResolver
The resolver for an archived ShardHeader

Constants§

SHARD_FORMAT_VERSION
Bumped on incompatible rkyv schema changes.
SHARD_MAGIC
Magic header bytes — fail-fast if a wrong-format file is mmap’d.

Statics§

CACHE
Process-wide ScriptCache rooted at default_cache_path(). None when the cache is disabled or the path could not be opened.

Functions§

cache_enabled
STRYKE_CACHE=0|false|no disables the cache entirely.
clear
Clear the global cache.
default_cache_path
Default shard path: ~/.stryke/scripts.rkyv.
evict_stale
Evict stale entries from global cache.
file_mtime
Get mtime from file metadata as (secs, nsecs).
stats
Global cache stats.
try_load
Try to load a cached script by source path. Returns None on any miss.
try_save
Store a compiled script in the cache.