Skip to content

Compile each registry schema once - #541

Merged
rdimitrov merged 1 commit into
mainfrom
cache-compiled-registry-schemas
Nov 20, 2026
Merged

rdimitrov merged 1 commit into
mainfrom
cache-compiled-registry-schemas

Conversation

@rdimitrov

Copy link
Copy Markdown
Member

Summary

validateAgainstSchema built a new schema loader and compiled the embedded JSON schema on every call. ValidateUpstreamRegistryBytes also validates each server's publisher-provided extensions separately, so a registry with N servers that carry extensions paid for N+1 full schema compilations.

This compiles each embedded schema once, on first use, and reuses it:

  • Safe to cache. The schemas are embedded in the binary, so a compilation result, including an error, never changes.
  • Safe to share. gojsonschema.Schema.Validate only reads the compiled schema, so concurrent callers can use the same one.

No exported API changes. The only visible difference: if an embedded schema file couldn't be read, the error now carries the caller's prefix (for example registry schema validation failed: failed to read embedded schema …). That can't happen with the files that ship.

Results

BenchmarkValidateUpstreamRegistryBytes, each server carrying publisher-provided extensions:

Servers Before After
1 868 µs, 1.2 MB, 13.6k allocs 11.6 µs, 13 KB, 291 allocs
100 40.0 ms, 55 MB, 615k allocs 0.87 ms, 0.97 MB, 21k allocs

This matters for toolhive-registry-server, which runs this validation on every file and git sync whose content changed, and on publish.

Testing

  • New tests:
    • each embedded schema is compiled once and the same compiled schema is returned on later calls;
    • a missing schema file reports the same error every time;
    • valid and invalid registries validated concurrently under -race give the right results.
  • go test -race ./registry/... passes. golangci-lint reports 0 issues; go vet and task license-check pass.

🤖 Generated with Claude Code

`validateAgainstSchema` built a new schema loader and compiled the
embedded JSON schema on every call. `ValidateUpstreamRegistryBytes`
then compiled the publisher-provided schema again for every server
that carries publisher-provided extensions, so validating a registry
cost one full compilation per server.

Compile each embedded schema on first use and reuse the result. The
schemas are embedded, so a compilation result never changes, and
`gojsonschema.Schema.Validate` does not modify the schema, so callers
can share it concurrently. Validating a 100-server registry drops from
40 ms and 55 MB allocated to 0.9 ms and 1 MB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rdimitrov
rdimitrov enabled auto-merge (squash) November 20, 2026 09:17
@rdimitrov
rdimitrov merged commit fc1923d into main Nov 20, 2026
6 checks passed
@rdimitrov
rdimitrov deleted the cache-compiled-registry-schemas branch November 20, 2026 09:19
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.

2 participants