Cache compiled CEL programs alongside ASTs in RuleCache#453
Merged
pkwarren merged 3 commits intobufbuild:mainfrom Apr 23, 2026
Merged
Cache compiled CEL programs alongside ASTs in RuleCache#453pkwarren merged 3 commits intobufbuild:mainfrom
pkwarren merged 3 commits intobufbuild:mainfrom
Conversation
The descriptorMap lookup at the top of compileRule() used fieldDescriptor (the user's field), but the corresponding store at the bottom uses ruleFieldDesc (the rule field, e.g. StringRules.pattern). The read therefore always missed, and cached CelRules were never reused across different user fields that share the same predefined rule. Use ruleFieldDesc for the lookup so it matches the store.
Match the protovalidate-go design where the rule cache stores the compiled program (not just the AST) per rule field descriptor, and per-field rule values are bound at call time. Before: compile() rebuilt the rule Cel environment and called createProgram(ast) on every invocation, even when the AST came from the cache. The only per-call input that actually differs between two fields sharing the same ruleFieldDesc is the rule value (e.g. min_len = 5 vs. min_len = 10), which is already bound as a CEL variable and not baked into the program. After: compileRule() builds the program once, alongside the AST, and stores both in descriptorMap keyed by ruleFieldDesc. compile() just wraps the cached program with per-call ObjectValue + rule variable. This mirrors loadOrCompileStandardRule in protovalidate-go (internal/constraints/cache.go), which caches per protoreflect.FieldDescriptor and binds rule values via WithRuleValues at evaluation time. No new cache was introduced.
2 tasks
pkwarren
approved these changes
Apr 23, 2026
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
Bring Java's
RuleCachein line with theprotovalidate-godesign: cache the compiled CELProgramalongside the AST, keyed by the rule field descriptor, and bind per-field rule values at call time.Today
compile()rebuilds the ruleCelenvironment and callscreateProgram(ast)on every invocation, even when the AST came from the cache. For two fields sharing the sameruleFieldDesc, the only input that actually differs is the rule value (e.g.min_len = 5vs.min_len = 10) — that value is already passed as a CEL variable, not compiled into the program, so theProgramcan safely be reused.After this PR,
compileRule()builds the program once, alongside the AST, and stores both indescriptorMapkeyed byruleFieldDesc.compile()wraps the cached program with the per-callObjectValue+ rule variable, nothing more.This mirrors
loadOrCompileStandardRulein protovalidate-go (internal/constraints/cache.go), which caches perprotoreflect.FieldDescriptorand binds rule values viaWithRuleValuesat evaluation time. No new cache is introduced — the existingdescriptorMapis simply extended to hold theProgramas well.Relationship to other PRs
descriptorMaplookup never hits, so caching the program has no effect. If Fix cache key mismatch in RuleCache.compileRule #451 merges first, this PR rebases down to one commit automatically.Test plan
./gradlew test)