-
Notifications
You must be signed in to change notification settings - Fork 17
Add a cache for compiled regex patterns #459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,24 +14,59 @@ | |
|
|
||
| package build.buf.protovalidate; | ||
|
|
||
| import com.google.re2j.Pattern; | ||
| import dev.cel.bundle.Cel; | ||
| import dev.cel.bundle.CelFactory; | ||
| import dev.cel.checker.CelCheckerBuilder; | ||
| import dev.cel.checker.CelStandardDeclarations; | ||
| import dev.cel.common.CelOptions; | ||
| import dev.cel.common.CelVarDecl; | ||
| import dev.cel.common.types.SimpleType; | ||
| import dev.cel.compiler.CelCompilerLibrary; | ||
| import dev.cel.extensions.CelExtensions; | ||
| import dev.cel.parser.CelParserBuilder; | ||
| import dev.cel.parser.CelStandardMacro; | ||
| import dev.cel.runtime.CelRuntimeBuilder; | ||
| import dev.cel.runtime.CelRuntimeLibrary; | ||
| import dev.cel.runtime.CelStandardFunctions; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
|
|
||
| /** | ||
| * Custom {@link CelCompilerLibrary} and {@link CelRuntimeLibrary}. Provides all the custom | ||
| * extension function definitions and overloads. | ||
| */ | ||
| final class ValidateLibrary implements CelCompilerLibrary, CelRuntimeLibrary { | ||
|
|
||
| private static final CelOptions CEL_OPTIONS = CelOptions.DEFAULT; | ||
|
|
||
| private final ConcurrentMap<String, Pattern> patternCache = new ConcurrentHashMap<>(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| /** Creates a ValidateLibrary with all custom declarations and overloads. */ | ||
| ValidateLibrary() {} | ||
|
|
||
| static Cel newCel() { | ||
| ValidateLibrary validateLibrary = new ValidateLibrary(); | ||
| // NOTE: CelExtensions.strings() does not implement string.reverse() or strings.quote() which | ||
| // are available in protovalidate-go. Fixed in https://github.com/google/cel-java/pull/998. | ||
| return CelFactory.standardCelBuilder() | ||
| .setOptions(CEL_OPTIONS) | ||
| // Drop stdlib matches; CustomOverload provides a caching replacement. | ||
| // Ref: https://github.com/google/cel-java/issues/1038 | ||
| .setStandardEnvironmentEnabled(false) | ||
| .setStandardDeclarations( | ||
| CelStandardDeclarations.newBuilder() | ||
| .excludeFunctions(CelStandardDeclarations.StandardFunction.MATCHES) | ||
| .build()) | ||
| .setStandardFunctions( | ||
| CelStandardFunctions.newBuilder() | ||
| .excludeFunctions(CelStandardFunctions.StandardFunction.MATCHES) | ||
| .build()) | ||
| .addCompilerLibraries(validateLibrary, CelExtensions.strings()) | ||
| .addRuntimeLibraries(validateLibrary, CelExtensions.strings()) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setParserOptions(CelParserBuilder parserBuilder) { | ||
| parserBuilder.setStandardMacros( | ||
|
|
@@ -54,6 +89,6 @@ public void setCheckerOptions(CelCheckerBuilder checkerBuilder) { | |
|
|
||
| @Override | ||
| public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) { | ||
| runtimeBuilder.addFunctionBindings(CustomOverload.create()); | ||
| runtimeBuilder.addFunctionBindings(CustomOverload.create(patternCache, CEL_OPTIONS)); | ||
| } | ||
| } | ||
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results: