Repository navigation
perf(events): cache compiled event schemas and avoid string copy on validation - #2022
Open
Tyagiquamar wants to merge 1 commit into
Open
Tyagiquamar wants to merge 1 commit into
Tyagiquamar wants to merge 1 commit into
Conversation
NumaryBot
approved these changes
Nov 26, 2026
NumaryBot
left a comment
There was a problem hiding this comment.
The required automated review completed with no remaining findings.
This branch has not been deployed
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.
Summary
Cache compiled
*gojsonschema.Schemainstances ineventsand pass raw[]bytepayloads directly togojsonschema.NewBytesLoaderduring validation.Root Cause & Problem
ComputeSchema,ComputeSchemaForVersion,Check, andCheckForVersionpreviously re-read embedded YAML files, unmarshaled YAML into maps, marshaled back to JSON, and compiled a freshgojsonschema.Schemaon every single call. In addition,validateconverteddatatostringviagojsonschema.NewStringLoader(string(data)), allocating a full copy of every validated event payload. Because all service schemas andbase.yamlare embedded at compile time via//go:embed, recompiling on every validation adds unnecessary CPU and allocation overhead.Technical Solution
sync.Mapcaches (versionedSchemaCacheandlatestSchemaCache) inevents/events.gokeyed by(service, version, event)and(service, event)respectively.LoadOrStoreso invalid/unknown service, version, or event lookups do not populate the cache.validateinevents/events.goto usegojsonschema.NewBytesLoader(data)directly.events/events_test.go:TestComputeSchemaCachesCompiledSchemas: verifies repeated calls toComputeSchemaandComputeSchemaForVersionreturn the same cached*gojsonschema.Schemainstance, and error cases are not cached.TestConcurrentCheckAndComputeSchema: exercises concurrentComputeSchema,ComputeSchemaForVersion,Check, andCheckForVersioncalls across 16 goroutines under-race.Test Plan
Executed in
D:\pers\open source\stack\eventsinsidegolang:1.27.4-trixie(--memory=1500m):Observed results:
go vet ./...: passed with exit code 0.go test -race -v ./...: all tests passed (ok github.com/formancehq/stacks/libs/events 2.668s), includingTestComputeSchemaCachesCompiledSchemas,TestConcurrentCheckAndComputeSchema,TestGatewayAuditEventSchemas,TestGatewayAuditRejectsInvalidEvents,TestGatewayAuditAcceptsCaptureLimit,TestGatewayAuditAcceptsLegacyHTTPAndAdditiveFields,TestReconciliationAlertEventSchemas,TestLedgerV3EventSchemas,TestEmbeddedServicePathsUseForwardSlashes, andTestComputeSchemaUsesLatestVersionContainingEvent.