Skip to content

Cache compiled CEL programs alongside ASTs in RuleCache#453

Merged
pkwarren merged 3 commits intobufbuild:mainfrom
snuderl:perf/rulecache-cache-programs-go-parity
Apr 23, 2026
Merged

Cache compiled CEL programs alongside ASTs in RuleCache#453
pkwarren merged 3 commits intobufbuild:mainfrom
snuderl:perf/rulecache-cache-programs-go-parity

Conversation

@snuderl
Copy link
Copy Markdown
Contributor

@snuderl snuderl commented Apr 23, 2026

Summary

Bring Java's RuleCache in line with the protovalidate-go design: cache the compiled CEL Program alongside the AST, keyed by the rule field descriptor, and bind per-field rule values at call time.

Today compile() rebuilds the rule Cel environment and calls createProgram(ast) on every invocation, even when the AST came from the cache. For two fields sharing the same ruleFieldDesc, the only input that actually differs is the rule value (e.g. min_len = 5 vs. min_len = 10) — that value is already passed as a CEL variable, not compiled into the program, so the Program can safely be reused.

After this PR, compileRule() builds the program once, alongside the AST, and stores both in descriptorMap keyed by ruleFieldDesc. compile() wraps the cached program with the per-call ObjectValue + rule variable, nothing more.

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 is introduced — the existing descriptorMap is simply extended to hold the Program as well.

Relationship to other PRs

Test plan

  • Existing unit tests pass (./gradlew test)
  • Existing conformance tests pass

snuderl added 2 commits April 23, 2026 09:30
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.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 23, 2026

CLA assistant check
All committers have signed the CLA.

@pkwarren pkwarren self-requested a review April 23, 2026 19:31
@pkwarren pkwarren merged commit 6e4bb6f into bufbuild:main Apr 23, 2026
4 checks passed
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.

3 participants