Skip to content

Fix cache key mismatch in RuleCache.compileRule#451

Merged
pkwarren merged 3 commits intobufbuild:mainfrom
snuderl:fix/rulecache-cache-key-mismatch
Apr 23, 2026
Merged

Fix cache key mismatch in RuleCache.compileRule#451
pkwarren merged 3 commits intobufbuild:mainfrom
snuderl:fix/rulecache-cache-key-mismatch

Conversation

@snuderl
Copy link
Copy Markdown
Contributor

@snuderl snuderl commented Apr 23, 2026

Summary

RuleCache.compileRule() stores entries in descriptorMap keyed by ruleFieldDesc (the rule field, e.g. StringRules.pattern), but the lookup at the top of the method uses fieldDescriptor (the user's field being validated).

The read key and write key never agree, so cached CelRule lists are never reused across different user fields that share the same predefined rule — every call falls through and re-builds the AST.

This PR changes the lookup to use ruleFieldDesc so it matches the store.

Test plan

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

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.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 23, 2026

CLA assistant check
All committers have signed the CLA.

@pkwarren
Copy link
Copy Markdown
Member

Thank you for the contribution. The fix looks correct to me - I've merged the latest changes from main which resolve an issue with PRs raised from forks although builds may still be rate limited.

Can you sign the CLA?

@pkwarren pkwarren self-requested a review April 23, 2026 13:58
@pkwarren pkwarren merged commit f05bf07 into bufbuild:main Apr 23, 2026
4 checks passed
@pkwarren
Copy link
Copy Markdown
Member

Wiring up a small benchmark harness - verified the fix:

benchmark                    metric  before            after            delta
compileValidatorForRepeated  time    4696209.42 ns/op  1064942.2 ns/op  -77.4%
compileValidatorForRepeated  alloc   12950196.95 B/op  3262651.61 B/op  -74.9%

pkwarren added a commit that referenced this pull request Apr 23, 2026
In order to verify performance fixes (like #451 and #454), it would be
helpful to be able to write a JMH benchmark and quickly measure the
impact.

This adds a benchmark module to the Gradle build. CI only verifies the
build of the benchmarks. Added a README showing how to add benchmarks
and measure improvements locally.

Instead of outputting JSON or other format, created a jq script to
roughly print out output similar to benchstat for Go.
pkwarren added a commit that referenced this pull request Apr 23, 2026
In order to verify performance fixes (like #451 and #454), it would be
helpful to be able to write a JMH benchmark and quickly measure the
impact.

This adds a benchmark module to the Gradle build. CI only verifies the
build of the benchmarks. Added a README showing how to add benchmarks
and measure improvements locally.

Instead of outputting JSON or other format, created a jq script to
roughly print out output similar to benchstat for Go.
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