VM aser-slot → sx-page-full: single-call page render, 0.55s warm

Compiler fixes:
- Upvalue re-lookup returns own position (uv-index), not parent slot
- Spec: cek-call uses (make-env) not (dict) — OCaml Dict≠Env
- Bootstrap post-processes transpiler Dict→Env for cek_call

VM runtime fixes:
- compile_adapter evaluates constant defines (SPECIAL_FORM_NAMES etc.)
  via execute_module instead of wrapping as NativeFn closures
- Native primitives: map-indexed, some, every?
- Nil-safe HO forms: map/filter/for-each/some/every? accept nil as empty
- expand-components? set in kernel env (not just VM globals)
- unwrap_env diagnostic: reports actual type received

sx-page-full command:
- Single OCaml call: aser-slot body + render-to-html shell
- Eliminates two pipe round-trips (was: aser-slot→Python→shell render)
- Shell statics (component_defs, CSS, pages_sx) cached in Python,
  injected into kernel once, referenced by symbol in per-request command
- Large blobs use placeholder tokens — Python splices post-render,
  pipe transfers ~51KB instead of 2MB

Performance (warm):
- Server total: 0.55s (was ~2s)
- aser-slot VM: 0.3s, shell render: 0.01s, pipe: 0.06s
- kwargs computation: 0.000s (cached)

SX_STANDALONE mode for sx_docs dev (skips fragment fetches).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-20 11:06:04 +00:00
parent 8dd3eaa1d9
commit ae0e87fbf8
13 changed files with 477 additions and 149 deletions

View File

@@ -159,6 +159,102 @@ class OcamlBridge:
await self._send(f'(aser-slot "{_escape(source)}")')
return await self._read_until_ok(ctx)
_shell_statics_injected: bool = False
async def _inject_shell_statics_locked(self) -> None:
"""Inject cached shell static data into kernel. MUST hold lock."""
if self._shell_statics_injected:
return
from .helpers import _get_shell_static
try:
static = _get_shell_static()
except Exception:
return # not ready yet (no app context)
# Define small values as kernel variables.
# Large blobs (component_defs, pages_sx, init_sx) use placeholders
# at render time — NOT injected here.
for key in ("sx_css", "component_hash", "sx_css_classes", "asset_url",
"sx_js_hash", "body_js_hash"):
val = static.get(key, "")
if val is None:
val = ""
var = f"__shell-{key.replace('_', '-')}"
defn = f'(define {var} "{_escape(str(val))}")'
try:
await self._send(f'(load-source "{_escape(defn)}")')
await self._read_until_ok(ctx=None)
except OcamlBridgeError:
pass
# Also inject list/nil values
for key in ("head_scripts", "inline_css", "inline_head_js", "body_scripts"):
val = static.get(key)
var = f"__shell-{key.replace('_', '-')}"
if val is None:
defn = f'(define {var} nil)'
elif isinstance(val, list):
items = " ".join(f'"{_escape(str(v))}"' for v in val)
defn = f'(define {var} (list {items}))'
else:
defn = f'(define {var} "{_escape(str(val))}")'
try:
await self._send(f'(load-source "{_escape(defn)}")')
await self._read_until_ok(ctx=None)
except OcamlBridgeError:
pass
self._shell_statics_injected = True
_logger.info("Injected shell statics into OCaml kernel")
async def sx_page_full(
self,
page_source: str,
shell_kwargs: dict[str, Any],
ctx: dict[str, Any] | None = None,
) -> str:
"""Render full page HTML in one OCaml call: aser-slot + shell render.
Static data (component_defs, CSS, pages_sx) is pre-injected as
kernel vars on first call. Per-request command sends only small
values (title, csrf) + references to the kernel vars.
"""
await self._ensure_components()
async with self._lock:
await self._inject_helpers_locked()
await self._inject_shell_statics_locked()
# Large blobs (component_defs, pages_sx, init_sx) use placeholders.
# OCaml renders the shell with short tokens; Python splices in
# the real values. This avoids piping ~1MB through stdin/stdout.
PLACEHOLDER_KEYS = {"component_defs", "pages_sx", "init_sx"}
placeholders = {}
static_keys = {"component_hash", "sx_css_classes", "asset_url",
"sx_js_hash", "body_js_hash", "sx_css",
"head_scripts", "inline_css", "inline_head_js", "body_scripts"}
parts = [f'(sx-page-full "{_escape(page_source)}"']
for key, val in shell_kwargs.items():
k = key.replace("_", "-")
if key in PLACEHOLDER_KEYS:
token = f"__SLOT_{key.upper()}__"
placeholders[token] = str(val) if val else ""
parts.append(f' :{k} "{token}"')
elif key in static_keys:
parts.append(f' :{k} __shell-{k}')
elif val is None:
parts.append(f' :{k} nil')
elif isinstance(val, bool):
parts.append(f' :{k} {"true" if val else "false"}')
elif isinstance(val, list):
items = " ".join(f'"{_escape(str(v))}"' for v in val)
parts.append(f' :{k} ({items})')
else:
parts.append(f' :{k} "{_escape(str(val))}"')
parts.append(")")
cmd = "".join(parts)
await self._send(cmd)
html = await self._read_until_ok(ctx)
# Splice in large blobs
for token, blob in placeholders.items():
html = html.replace(token, blob)
return html
async def _inject_helpers_locked(self) -> None:
"""Inject page helpers into the kernel. MUST be called with lock held."""
if self._helpers_injected: