sx: step 13 — buffer primitives + buffer-based inspect

Added short aliases make-buffer / buffer? / buffer-append! / buffer->string /
buffer-length on both OCaml and JS hosts, sharing the existing StringBuffer
value type. buffer-append! auto-coerces non-strings via inspect.

Rewrote the OCaml host inspect function to walk a single shared Buffer.t
instead of allocating O(n) intermediate strings via String.concat at every
recursion level. inspect underlies sx-serialize and error-path formatting,
so this benefits the tightest serialization paths.

Median improvements (bin/bench_inspect.exe, best-of-3 of 9-run min):
  tree-d8 (75KB):    5.31ms -> 1.30ms  (-76%)
  tree-d10 (679KB): 81.89ms -> 16.02ms (-80%)
  dict-1000:         0.80ms -> 0.31ms  (-61%)
  list-2000:         0.74ms -> 0.33ms  (-55%)

Tests: OCaml 4545 -> 4550. JS 2591 -> 2596. Zero regressions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-07 02:16:59 +00:00
parent c48911e591
commit 0e022ab670
9 changed files with 303 additions and 67 deletions

View File

@@ -810,6 +810,24 @@
:returns "string-buffer"
:doc "Create a new empty mutable string buffer for O(1) amortised append.")
(define-primitive
"make-buffer"
:params ()
:returns "string-buffer"
:doc "Create a new mutable buffer (alias for make-string-buffer with terser name).")
(define-primitive
"buffer-append!"
:params (buf v)
:returns "nil"
:doc "Append a value to a buffer; coerces non-strings to their printed form.")
(define-primitive
"buffer->string"
:params (buf)
:returns "string"
:doc "Finalize a buffer to a single string.")
(define-module :stdlib.coroutines)
(define-module :stdlib.bitwise)