Skip to content

Add a cache for compiled regex patterns#459

Merged
pkwarren merged 1 commit intomainfrom
pkw/cache-regex-patterns
Apr 24, 2026
Merged

Add a cache for compiled regex patterns#459
pkwarren merged 1 commit intomainfrom
pkw/cache-regex-patterns

Conversation

@pkwarren
Copy link
Copy Markdown
Member

cel-java doesn't currently have the optimization from cel-go which optimizes calls to the 'matches' function to compile a regular expression once instead of at every invocation.

Override cel-java's builtin matches functions to add a pattern compilation cache (scoped to a Validator).

cel-java doesn't currently have the optimization from cel-go which
optimizes calls to the 'matches' function to compile a regular
expression once instead of at every invocation.

Override cel-java's builtin matches functions to add a pattern
compilation cache (scoped to a Validator).

private static final CelOptions CEL_OPTIONS = CelOptions.DEFAULT;

private final ConcurrentMap<String, Pattern> patternCache = new ConcurrentHashMap<>();
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the proposed fix in google/cel-java#1038, this cache is scoped to a Validator and not global.

}

@Benchmark
public void validateRegexPattern(Blackhole bh) throws ValidationException {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Results:

benchmark             metric  before         after          delta
validateRegexPattern  time    1873.21 ns/op  1323.61 ns/op  -29.3%
validateRegexPattern  alloc   7828 B/op      4272 B/op      -45.4%

Comment thread src/test/java/build/buf/protovalidate/CustomOverloadTest.java
@pkwarren pkwarren merged commit f4f3e75 into main Apr 24, 2026
5 checks passed
@pkwarren pkwarren deleted the pkw/cache-regex-patterns branch April 24, 2026 16:42
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.

4 participants