Skip to content

Core: speed up WASM validation exports and add benchmarks - #7

Open
Fellmonkey wants to merge 2 commits into
mainfrom
perf/wasm-benchmarks
Open

Fellmonkey wants to merge 2 commits into
mainfrom
perf/wasm-benchmarks

Conversation

@Fellmonkey

@Fellmonkey Fellmonkey commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

What this changes and why

No issue: this is the WASM performance and browser-generation task requested directly.

Performance. All five existing WASM exports (goValidate, goParseField, goParseDomain, goSpecmeta, goVersion) now have native benchmarks and a Node smoke harness. The export logic lives in a js-free api.go, so the browser path is the one being measured.

Browser generation. A sixth export, goGenerate(yaml, bridge, packagesJSON), builds the IR, resolves the extends chain from the embedded bridges/ tree and returns generated files as JSON. Disk rendering is unchanged: TestRenderMemoryMatchesDisk proves byte-identical output.

Changes:

  • memoize the last goValidate/goParseDomain result and the goSpecmeta JSON;
  • give SplitIdentifier, PascalCase, ToDatabaseColumnName and SplitRespectingQuotes a fast path for the common case;
  • create Validations only when a field actually has a validation modifier;
  • add renderer.NewFromFS + RenderMemory, with js/wasm stubs that keep the network-backed package resolver and bridge clone/update code out of the browser binary;
  • keep the JS argument type checks and the Unicode casing fix from main.

How you tested it

go test ./... -count=1                 # all packages pass
make lint                              # 0 issues
GOOS=js GOARCH=wasm go build -o bin/validate.wasm ./cmd/wasm-validator/
node cmd/wasm-validator/smoke.mjs bin/validate.wasm   # all 6 exports ok, 6 generated files

go test ./cmd/wasm-validator/ -run '^$' -bench . -benchmem -count=8
go test ./pkg/textutil/ -run '^$' -bench . -benchmem -count=8

Benchstat, base main@8927417 vs this branch, Windows/amd64 Ryzen 7 5800X, Go 1.27.4:

Benchmark Before After Delta
Specmeta 22.7 µs 3.4 ns −100%
StudioValidationCycle/same-yaml 2.66 ms 2.0 µs −99.9%
StudioValidationCycle/changing-yaml 2.59 ms 2.27 ms −12.5%
SplitIdentifier/simple 192 ns 16 ns −92%
ToDatabaseColumnName/simple 183 ns 5.7 ns −97%
Validate/entities=2000 18.4 ms 13.8 ms −25%
ParseField/modifiers 3.36 µs, 25 alloc 2.89 µs, 15 alloc −14% / −40%
GenerateYAML — 2.0 ms, 1.44 MiB new

Real WASM, Node 24, 200 calls, median of four interleaved runs, ms/call:

Export Before After
goValidate (same YAML) 0.26 0.055
goValidate (new YAML) 0.215 0.24
goParseDomain (same YAML) 0.19 0.021
goParseDomain (new YAML) 0.19 0.19
goSpecmeta 0.14 0.016
goParseField 0.044 0.049
validate + parseDomain cycle 0.39 0.042
goGenerate (admin-alpine) — 9

Cold per-call paths are unchanged within noise. An earlier attempt to cache *parser.RawSchema was dropped: it raised GC pressure and slowed every other WASM export; only strings are cached now.

Binary size. The naive renderer wiring grew the WASM file from 5.90 MB to 19.38 MB. WASM-only stubs for package resolution and bridge resolution bring it to 10.24 MB raw / 2.64 MB gzip (base: 5.90 MB raw / 1.61 MB gzip). -ldflags "-s -w" produces 10.01 MB. NuGet versions are not resolved in the browser; callers pass them via packagesJSON. Disk writes, --prune and --migrate stay outside the browser.

Tested on: local Windows, Go 1.27.4, Node 24.18.1; CI-equivalent make lint and go test ./....

How you found it

  • I use DomainCraft and ran into this myself
  • I found it by reading the code or comparing with other compilers

AI tools used to write this change: yes. OpenCode (hy4-flash-free) wrote the native benchmark harness, the js-free API extraction, the Node smoke harness, the in-memory renderer path, the identifier fast paths and the measurements; it used pprof, benchstat and binary-size probes to choose the changes. The human author reviewed every file and the final numbers before opening and updating this PR.

Checklist

  • Commits are signed and verified
  • The author of this PR is a person; an AI agent is not the contributor
  • make fmt, make lint and make test pass
  • Deterministic output: generated files are sorted by path; no map iteration was added
  • Regression checks are named in compliance-suite/regressions.tsv

- move WASM export logic into a js-free API so it is testable natively
- memoize goValidate/goParseDomain results and goSpecmeta JSON
- cut identifier and modifier-split allocations in the hot parse path
- add native benchmarks, a Node WASM smoke harness and measured results

Benchstat, Windows/amd64 Ryzen 7 5800X, Go 1.27.4, -count=6+8,
base main@8927417 vs PR after re-sync:

Specmeta                          22.7us  ->  3.4ns    -100%
StudioValidationCycle/same-yaml    2.66ms ->  2.0us    -99.9%
StudioValidationCycle/changing     2.59ms ->  2.27ms   -12.5%
SplitIdentifier/simple             192ns  ->  16ns     -92%
ToDatabaseColumnName/simple        183ns  ->  5.7ns    -97%
Validate/entities=2000            18.4ms  -> 13.8ms    -25%

Node, 200 calls, median of 4 interleaved runs, ms/call:
goValidate (same)       0.26 -> 0.055
goParseDomain (same)    0.19 -> 0.021
goSpecmeta              0.14 -> 0.016
studio cycle            0.39 -> 0.042

Cold per-call paths are unchanged within noise.
- expose goGenerate(yaml, bridge, packagesJSON) returning generated files
- load the embedded bridges tree through a new fs.FS renderer path
- keep CLI rendering byte-identical (TestRenderMemoryMatchesDisk)
- stub package resolution and bridge clone/update code for js/wasm

The naive build grew from 5.9 MB to 19.4 MB; the WASM-only stubs bring it
back to 10.2 MB raw (2.6 MB gzip). RenderMemory adds no disk or symlink
machinery, so browser generation stays bounded by the returned JSON.

go test ./... -count=1 and make lint pass; Node smoke generates 6 files
for admin-alpine in ~9 ms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant