Repository navigation
Core: speed up WASM exports and cut core allocations - #7
Merged
Merged
Conversation
- 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.
Fellmonkey
force-pushed
the
perf/wasm-benchmarks
branch
from
November 26, 2026 19:53
13ca152 to
40aa887
Compare
- 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.
- cache compiled templates and the bridge chain across generate calls - compile specs once for both disk and in-memory renderers - validate the model in goGenerate before rendering it - move the contract gate into internal/bridgecontract and reuse specmeta - keep crypto/rand out of the WASM build BenchmarkGenerateYAML before -> after: 2.01ms -> 0.71ms (-65%) 1477KiB -> 587KiB (-60%) 11.5k -> 2.49k allocs/op (-78%) WASM binary: 10.07 MB raw / 2.59 MB gzip. Other exports unchanged in Node.
- share a sorted-key helper and a flat cycle DFS in the validator - parse strings without a []byte copy and cache feature-field order - cache regexp compilation for field definitions - write goParseField JSON directly and stream result JSON into a builder - grow template output buffers from the previous render size - fix benchmarks that appended a digit to YAML and measured the error path benchstat on corrected benchmarks, base -> this commit: geomean time -36.7%, bytes -28.6%, allocs -24.2% Validate/entities=2000 -28% time GenerateYAML -19% time, -50% bytes ParseField/regex -77% time StudioValidationCycle/changing-yaml -10% time All six exports stay byte-identical: SHA256 matches the previous commit for four models and eight field definitions. WASM binary: 10.14 MB raw.
- add parser, validator, lexer and in-memory renderer benchmarks with budgets - budget WASM export allocations with alternating inputs to exercise the cold path - extend make bench and bench.yml to cover the new modules - clear cmd/wasm-validator on the base side: it only has a js/wasm main.go there - name the new checks in compliance-suite/regressions.tsv Budgets were measured on Go 1.27.4 with ~25% slack and verified under -race. A deliberate extra allocation in lexer.Parse failed the gate; the injection was removed afterwards. Timing stays a report, not a gate.
Fellmonkey
force-pushed
the
perf/wasm-benchmarks
branch
from
November 26, 2026 21:29
915bfdb to
1732050
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes and why
No issue: this is the WASM performance and browser-generation task requested directly.
Performance. All six WASM exports have native benchmarks and a Node smoke harness. Export logic lives in a js-free
api.go, so the browser path is what gets measured. A second pass cut allocations in the core itself — validator, parser, lexer, renderer and result JSON.Browser generation.
goGenerate(yaml, bridge, packagesJSON)builds the IR, resolves theextendschain from the embeddedbridges/tree and returns generated files as JSON. Disk rendering is unchanged:TestRenderMemoryMatchesDiskproves byte-identical output.Changes:
goValidate/goParseDomainresult and thegoSpecmetaJSON;SplitIdentifier,PascalCase,ToDatabaseColumnNameandSplitRespectingQuotesa fast path for the common case;Validationsonly when a field actually has a validation modifier;renderer.NewFromFS+RenderMemory, with js/wasm stubs that keep the network-backed package resolver and bridge clone/update code out of the browser binary;main;[]bytecopy, cache feature-field order, readNode.Valuefor plain scalars;regexp.Compilefor the field definition the Studio reparses on every keystroke;goParseFieldJSON directly and stream result JSON into a strings.Builder;BenchmarkValidate/BenchmarkParseDomain: they appended a digit after a trailing newline and measured the parse-error path instead of validation.How you tested it
Benchstat,
e3ef352→9a27589, Windows/amd64 Ryzen 7 5800X, Go 1.27.4, corrected benchmarks, n=6:Validate/entities=2000Validate/entities=2000, allocsGenerateYAMLParseField/regexParseField/modifiersStudioValidationCycle/changing-yamlGeneration was initially 2.01 ms / 1.44 MiB / 11.5k alloc. Compiling templates once per bridge chain cut it to 0.71 ms / 587 KiB / 2.49k alloc; the second pass (buffered output and streamed result JSON) brings it to 0.67 ms / 301 KiB / 2.42k alloc. The cached plan holds only bridge files, never the user's model.
Earlier measurements against base
main@8927417(the first three commits;Validate/ParseDomainrows predate the benchmark fix):Specmeta22.7 µs → 3.4 ns,StudioValidationCycle/same-yaml2.66 ms → 2.0 µs,ToDatabaseColumnName/simple183 ns → 5.7 ns,Validate/entities=200018.4 ms → 13.8 ms.Real WASM in Node 24 was smoke-tested after every pass: all six exports return the expected values and
goGenerateproduces six files. ColdgoValidate/goParseDomaintimings oscillate by 20–70% between identical runs on this machine, so they are not used as evidence.goParseFieldandgoGenerateimproved consistently (generation of the kitchen-sink model: ~130 ms → ~99 ms in the last interleaved run).Binary size. The naive renderer wiring grew the WASM file from 5.90 MB to 19.38 MB. WASM-only stubs for package resolution, bridge resolution and
crypto/randbring it to 10.14 MB raw / 2.61 MB gzip (base: 5.90 MB raw / 1.61 MB gzip).net/http,crypto/tlsandos/execare absent from the js/wasm dependency graph. NuGet versions are not resolved in the browser; callers pass them viapackagesJSON. Disk writes,--pruneand--migratestay outside the browser.Byte identity and regressions. SHA256 over all six exports for four models and eight field definitions is
aa57102b…for both the previous and the final commit.TestMarshalFieldDefinitionMatchesEncodingJSONguards the hand-written writer againstencoding/json; it caught a real mismatch where a nilValidationsmap was written as{}instead ofnull.TestGenerateYAMLRejectsInvalidModelkeeps the validator gate in front of browser generation. Both are named incompliance-suite/regressions.tsv.DRY. The bridge contract gate lives in
internal/bridgecontractand is reused by the CLI, disk renderer and WASM renderer;specmeta.EffectiveContractVersionhandles absent/zero declarations.targetFrameworkandcompileSpecare single implementations shared by both render paths.goGeneratecalls the same validator asgoValidate— an invalid model is not rendered.Tested on: local Windows, Go 1.27.4, Node 24.18.1; CI-equivalent
make lintandgo test ./....Keeping the gains
Allocation budgets run inside
go test ./..., so CI rejects a systematicregression without trusting a noisy timer. Measured on Go 1.27.4:
ParseYAMLString/entities=100Validate/entities=100Parsesimple / modifiers / regexValidateYAML(cold, alternating inputs)ParseFieldDefinitionGenerateYAMLRenderMemory/admin-race)Budgets leave ~25% slack for patch releases (tighter where absolute counts are
small) and were verified under
-raceand-shuffle=on. A deliberately addedallocation in
lexer.Parsefailed the gate; the injection was removed. Thechecks are named in
compliance-suite/regressions.tsv.make benchandbench.ymlnow coverinternal/parser,internal/validator,internal/lexer,cmd/wasm-validatorandpkg/textutilin addition to theexisting renderer/IR/snapshot targets.
bench.ymltriggers on the two newpaths. On the base checkout it clears
BENCH_WASM_PACKAGE: at that commitcmd/wasm-validatorcontains only ajs && wasmmain.goand cannot build onlinux/amd64, which made the first bench run fail. Timing remains a report:
benchstat compares base and PR on the same runner, while behavior and
allocations are gated by tests.
How you found it
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 and the core allocation fixes. For the second pass it orchestrated four parallel agents in isolated worktrees, each profiling one area with pprof and benchstat; ideas without a measured win were reverted. The human author reviewed every file and the final numbers before opening and updating this PR.Checklist
make fmt,make lintandmake testpasscompliance-suite/regressions.tsv