Add Pattern.programSize() and Matcher.programSize()#180
Merged
sjamesr merged 2 commits intogoogle:masterfrom Jan 5, 2025
Merged
Add Pattern.programSize() and Matcher.programSize()#180sjamesr merged 2 commits intogoogle:masterfrom
Pattern.programSize() and Matcher.programSize()#180sjamesr merged 2 commits intogoogle:masterfrom
Conversation
This PR exposes `Pattern.programSize()` and `Matcher.programSize()` public API. The program size represents a very approximate measure of a regexp's "cost". Larger numbers are more expensive than smaller numbers. Similar to the canonical C++ implementation, re2j will return the program size as the number of instructions of the regex program without making any promises or claims except "larger is more expensive". Context: The need for this change arose from cross-language projects, such as gRPC and CEL. gRPC needs to configure the maximum size of regex programs in CEL to the same number across all languages. While it's possible in CEL-Cpp and gRPC-Cpp, CEL-Java doesn't provide the same configuration option simply because the program size is not available in re2j. In Go, the number of instruction is available in Go via the length of https://pkg.go.dev/regexp/syntax#Prog.Inst.
Member
Author
|
Thank you for accepting the change! |
3 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR exposes
Pattern.programSize()andMatcher.programSize()public API.The program size represents a very approximate measure of a regexp's "cost". Larger numbers are more expensive than smaller numbers.
Similar to the canonical C++ implementation (see
RE2.ProgramSize()), re2j will return the program size as the number of instructions of the regex program without making any promises or claims except "larger is more expensive".Context: The need for this change arose from cross-language projects, such as gRPC and CEL. gRPC needs to configure the maximum size of regex programs in CEL to the same number across all languages. While it's possible in gRPC-Cpp via CEL-Cpp
options.regex_max_program_sizethat uses re2's ProgramSize, CEL-Java doesn't provide the same configuration option simply because the program size is not available in re2j. In Go, the number of instruction is available in Golen(syntax.Prog.Inst).Internal ref: go/grpc-cel-integration
CCs:
@ejona86 (grpc-java), @markdroth (grpc)
@l46kok (cel-java), @TristonianJones (cel)