Skip to content

Cache compiled programs by serialized rule message#452

Closed
snuderl wants to merge 1 commit intobufbuild:mainfrom
snuderl:perf/rulecache-program-cache
Closed

Cache compiled programs by serialized rule message#452
snuderl wants to merge 1 commit intobufbuild:mainfrom
snuderl:perf/rulecache-program-cache

Conversation

@snuderl
Copy link
Copy Markdown
Contributor

@snuderl snuderl commented Apr 23, 2026

Summary

Fields with identical resolved rules (same rule type + same values — e.g. multiple fields sharing the same predefined constraint such as an evm_hash pattern) currently rebuild their List<CompiledProgram> from scratch every time, even though the resulting programs are equivalent.

This PR adds a static ConcurrentHashMap in RuleCache keyed by the serialized rule message bytes. On hit, compile() returns the cached list directly; on miss, it falls through to the existing build path and populates the cache with the result.

In a workload with 14 descriptor types and 19 fields sharing 2 predefined rules, warmup time dropped from ~58ms to ~3.6ms.

Note: this is independent of the cache-key fix in #451 — the two help different scenarios (AST reuse across fields sharing a rule field descriptor vs. program reuse across fields with identical resolved rule values) and can be merged in either order.

Test plan

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

Fields with identical resolved rules (same rule type + same values, e.g.
multiple fields sharing the same predefined constraint like an evm_hash
pattern) currently re-create their CompiledProgram list independently,
even though the resulting programs are equivalent.

Add a static ConcurrentHashMap keyed by the serialized rule message
bytes that caches the built List<CompiledProgram>. On hit we short-circuit
the rebuild; on miss we populate it at the end of compile().

In a workload with 14 descriptor types and 19 fields sharing 2 predefined
rules, this reduced warmup time from ~58ms to ~3.6ms.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 23, 2026

CLA assistant check
All committers have signed the CLA.

@snuderl
Copy link
Copy Markdown
Contributor Author

snuderl commented Apr 23, 2026

Superseded by #453, which achieves the same reuse by caching the compiled program in the existing descriptor-keyed cache (matching protovalidate-go's design) instead of adding a second bytes-keyed cache.

@snuderl snuderl closed this Apr 23, 2026
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.

2 participants