Cache compiled Lua scripts between executions #127
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
RuDS currently creates a fresh sandboxed Lua state and calls
lua.load(source).exec()for every script invocation. This recompiles the same source for every custom macro occurrence and for every enabled Blogmark transformer on every capture. Syntax validation also compiles the source but discards the result.Add a small process-local cache of compiled Lua 5.4 binary chunks in the shared scripting runtime so every execution path benefits without changing macro or transformer call sites.
Minimal design
Luastates orFunctionhandles. Each invocation must still create a fresh sandbox, install its current project-scoped host API, hooks, memory limit, cancellation state, output sinks, and then execute the cached chunk. This preserves isolation and allows concurrent use.crates/bds-core/src/scripting/mod.rs, behind the existingvalidate/execute_many_with_hostpath.render_script_macroandrun_transformsshould continue using the shared executor unchanged.Chunk::into_function(), retain debug information withFunction::dump(false), and load cache hits explicitly asChunkMode::Binarybefore executing the chunk and resolving the configured entrypoint.Implementation plan
Mutex/OnceLockcache.validatecall that helper. Makeexecute_many_with_hostobtain the cached bytes, create its normal fresh sandbox, install the host API and hook, then load/execute the binary chunk.cargo fmt --all -- --check, warnings-denied workspace Clippy, andcargo test --workspacewith loopback permission as required byAGENTS.md.Acceptance criteria
Out of scope
Implemented in
87a1610. The shared scripting runtime now stores Lua 5.4 bytecode in a dependency-free, process-local 32-entry FIFO cache keyed only by exact source. Validation and execution share the helper; cache misses compile with into_function() and dump(false), while every execution still creates the existing fresh 32 MiB sandbox, installs the current host/output/progress/toast API and cancellation/timeout hook, then loads the cached chunk explicitly as binary. Failed compilation is excluded and poisoned locks recover. Macro and Blogmark transformer call sites remain unchanged. Added red/green tests for exact hits, edited-source misses, failure exclusion, eviction, concurrent compile-once behavior, poison recovery, validation warming, equivalent results, fresh globals, and source-line errors; updated Allium and README. Verified against bDS2 behavior and current mlua 0.12 APIs with Allium check/analyse, fmt, warnings-denied workspace Clippy, the full workspace suite (575 core passed; one existing ignored real-model test), workspace build, and release macOS bundle.