Auto-compile: lambdas → bytecode VM at load time

After loading .sx files, (vm-compile) iterates all named lambdas,
compiles each body to bytecode, replaces with NativeFn VM wrapper.

Results: 3/109 functions compiled (compiler needs more features).
CEK steps: 49911 → 38083 (23% fewer) for home page.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-19 19:07:42 +00:00
parent a8d1163aa6
commit e14fc9b0e1
2 changed files with 92 additions and 2 deletions

View File

@@ -215,9 +215,9 @@ class OcamlBridge:
# Collect files to load
all_files: list[str] = []
# Spec files needed by aser
# Spec files needed by aser + bytecode compiler
spec_dir = os.path.join(os.path.dirname(__file__), "../../spec")
for spec_file in ["parser.sx", "render.sx"]:
for spec_file in ["parser.sx", "render.sx", "bytecode.sx", "compiler.sx"]:
path = os.path.normpath(os.path.join(spec_dir, spec_file))
if os.path.isfile(path):
all_files.append(path)
@@ -264,6 +264,13 @@ class OcamlBridge:
filepath, e)
_logger.info("Loaded %d definitions from .sx files into OCaml kernel (%d skipped)",
count, skipped)
# Auto-compile all lambdas to bytecode VM
try:
await self._send('(vm-compile)')
await self._read_until_ok(ctx=None)
except OcamlBridgeError as e:
_logger.warning("VM compilation skipped: %s", e)
except Exception as e:
_logger.error("Failed to load .sx files into OCaml kernel: %s", e)
self._components_loaded = False # retry next time