Commit Graph
1 Commits
Author SHA1 Message Date
yetone 772f936272 perf: jsonschema's metaschemas are compiled on first use, not at launch
github.com/santhosh-tekuri/jsonschema/v6's init compiles every draft's
metaschema (4, 6, 7, 2019-09, 2020-12 and their vocabularies): ~1.7 MB
and ~21,000 allocations on every launch, the largest init left in the
binary. magpie compiles a schema only for a Claude TUI answer tool, so
`magpie statusline`, run on each Claude Code message, paid it for
nothing.

v6.0.3's non-test files are now in third_party/jsonschema (go.mod
replace; LICENSE and MAGPIE.md beside them), with one change: init
became loadDrafts behind a sync.Once, which Compiler.Compile calls
first; loadDrafts compiles the metaschemas through the unexported
compile exactly as init did. The Dockerfile copies third_party before
`go mod download`, which needs the replace's directory.

New TestAnswerSchemaCheckedAgainstItsDraft: three schemas that break
their draft (type 5, minLength "x", required "a" under 2020-12) are
refused. Broken by hand (Compile without ensureDrafts) it panics red.

Measured on mote, sandbox HOME, t45 (v0.1.3522) against t46 (this):
- GODEBUG=inittrace=1 sum: 2.98 MB / 28,515 allocs -> 1.28 MB / 7,385
- `magpie statusline`, 3 runs: instructions retired ~135M -> ~101M,
  peak memory footprint 15.5-16.1 MB -> 12.6-12.7 MB

Run: upstream's own tests (`go test .`, GOWORK=off) on the forked
draft.go and compiler.go: ok, as on upstream (36 pass; the official
JSON-Schema-Test-Suite submodule isn't in the module zip, so it wasn't
run). go vet ./... ok; GOOS=windows go vet ./internal/gateway ok;
go test -run 'TestAnswer|TestClaudeTUISchema' ./internal/gateway ok;
root TestNoEagerPackageRegexps, TestAllLazyPatternsCompile ok.
Not tried: a docker build of the Dockerfile.
2026-11-10 13:16:59 +08:00