sx-http: buffer-based streaming renderer, eliminates string allocation

New render_to_buf / render_to_html_streaming in sx_render.ml — writes
HTML directly to a Buffer.t instead of building intermediate strings.
Eliminates hundreds of string concatenations per page render.

Full parallel renderer: render_to_buf, render_element_buf,
render_component_buf, render_cond_buf, render_let_buf, render_map_buf,
render_for_each_buf — all buffer-native.

HTTP server SSR + shell now use streaming renderer.

Performance (warm, 2 worker domains, 2MB RSS):
  Homepage:  138-195ms TTFB (Quart: 202ms) — faster
  Geography: 218-286ms TTFB (Quart: 144ms)
  Throughput: 6.85 req/s at c=5 (Quart: 6.8 req/s) — matched

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-28 17:06:27 +00:00
parent 3620a433c1
commit 1412648f6e
2 changed files with 291 additions and 7 deletions

View File

@@ -1490,21 +1490,19 @@ let http_render_page env path =
| _ -> serialize_value body_result
in
let t2 = Unix.gettimeofday () in
(* Phase 2: SSR — render to HTML using native renderer.
Native Sx_render handles islands as placeholders when SSR fails,
avoiding deep recursion into client-only island bodies. *)
(* Phase 2: SSR — render to HTML using streaming buffer renderer.
Writes directly to buffer, no intermediate string allocations. *)
let body_html =
try
let body_exprs = Sx_parser.parse_all body_str in
let body_expr = match body_exprs with
| [e] -> e | [] -> Nil | _ -> List (Symbol "<>" :: body_exprs) in
Sx_render.render_to_html body_expr env
Sx_render.render_to_html_streaming body_expr env
with e ->
Printf.eprintf "[http-ssr] failed: %s\n%!" (Printexc.to_string e); ""
in
let t3 = Unix.gettimeofday () in
(* Phase 3: Shell — wrap in full HTML page.
Shell kwargs reference pre-injected __shell-* vars from env. *)
(* Phase 3: Shell — render directly to buffer for zero-copy output *)
let get_shell_var name = try env_get env ("__shell-" ^ name) with _ -> Nil in
let shell_args = [
Keyword "title"; String "SX";
@@ -1529,7 +1527,7 @@ let http_render_page env path =
Keyword "meta-html"; String "";
] in
let shell_call = List (Symbol "~shared:shell/sx-page-shell" :: shell_args) in
let html = Sx_render.render_to_html shell_call env in
let html = Sx_render.render_to_html_streaming shell_call env in
let t4 = Unix.gettimeofday () in
Printf.eprintf "[sx-http] %s route=%.3fs aser=%.3fs ssr=%.3fs shell=%.3fs total=%.3fs html=%d\n%!"
path (t1 -. t0) (t2 -. t1) (t3 -. t2) (t4 -. t3) (t4 -. t0) (String.length html);