Skip to content

perf(schema): compile JSON schema once in Validate - #6508

Merged
jess-lowe merged 2 commits into
google:masterfrom
jess-lowe:perf/osvutil-schema-cache
Dec 4, 2026
Merged

jess-lowe merged 2 commits into
google:masterfrom
jess-lowe:perf/osvutil-schema-cache

Conversation

@jess-lowe

Copy link
Copy Markdown
Contributor

Overview

Precompiles the embedded OSV JSON schema once via sync.OnceValues in go/internal/osvutil/schema/schema.go instead of recompiling schema_generated.json on every Validate call (mirroring ossf/osv-schema#691).

(This PR was authored with AI assistance.)

Details

  • Added compiledSchema using sync.OnceValues to lazily compile schemaBytes into a reusable *gojsonschema.Schema once per process.
  • Updated Validate(data []byte) to validate against the compiled schema while preserving the existing error wrapping and formatting.
  • Added TestValidate_Concurrent in go/internal/osvutil/schema/schema_test.go to verify concurrent Validate calls under the race detector.

Testing

  • cd go && gofmt -s -l ./internal/osvutil/schema/ (clean)
  • cd go && go vet ./internal/osvutil/schema/... (clean)
  • cd go && go test -count=1 -race -shuffle=on ./internal/osvutil/... (pass)
  • cd go && go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.14.0 run ./internal/osvutil/schema/... (0 issues.)

@jess-lowe
jess-lowe requested review from a team and another-rex December 4, 2026 04:05
…ma-cache

# Conflicts:
#	go/internal/osvutil/schema/schema.go
#	go/internal/osvutil/schema/schema_test.go
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 58.66%. Comparing base (f6b775a) to head (4121637).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
go/internal/osvutil/schema/schema.go 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6508      +/-   ##
==========================================
+ Coverage   58.61%   58.66%   +0.04%     
==========================================
  Files         228      228              
  Lines       24740    24742       +2     
==========================================
+ Hits        14502    14515      +13     
+ Misses      10235    10224      -11     
  Partials        3        3              
Flag Coverage Δ
go 62.40% <80.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

}
}

func TestValidate_Concurrent(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little weird in that it doesn't really test that the schema is only being compiled once, but whatever

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remove the test probably.

@jess-lowe
jess-lowe merged commit fc447d8 into google:master Dec 4, 2026
25 checks passed
@jess-lowe
jess-lowe deleted the perf/osvutil-schema-cache branch December 4, 2026 04: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.

4 participants