-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implement PCRE2 JIT compilation #12866
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7b91e2d
Implement `Regex` engine on PCRE2 (#12840)
straight-shoota f1fb7aa
Fix OOB in Regex::Engine:PCRE2 capture group look
straight-shoota c4ebd4c
Allocate matchdata via GC
straight-shoota ac92ddf
[CI] Add workflow for testing regex engine implementations
straight-shoota 6c9d1b1
Improve error reporting for match error
straight-shoota 33f6479
Skip larg single line string spec when depth limit is insufficient
straight-shoota 979f01c
Make PCRE2 opt-in for now
straight-shoota 626ad01
Implement PCRE2 JIT compilation
straight-shoota 5dd4ddf
Ensure depth limit in large string spec
straight-shoota ae8b62e
Merge branch 'master' into feature/pcre2-jit
straight-shoota 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,20 @@ module Regex::PCRE2 | |
| @re = PCRE2.compile(source, pcre2_options(options) | LibPCRE2::UTF | LibPCRE2::NO_UTF_CHECK | LibPCRE2::DUPNAMES | LibPCRE2::UCP) do |error_message| | ||
| raise ArgumentError.new(error_message) | ||
| end | ||
|
|
||
| jit_compile | ||
| end | ||
|
|
||
| private def jit_compile : Nil | ||
| ret = LibPCRE2.jit_compile(@re, LibPCRE2::JIT_COMPLETE) | ||
| if ret < 0 | ||
| case error = LibPCRE2::Error.new(ret) | ||
| when .jit_badoption? | ||
| # okay | ||
| else | ||
| raise ArgumentError.new("Regex JIT compile error: #{error}") | ||
| end | ||
| end | ||
| end | ||
|
|
||
| protected def self.compile(source, options) | ||
|
|
@@ -123,9 +137,24 @@ module Regex::PCRE2 | |
| LibPCRE2.general_context_create(->(size : LibC::Int, data : Void*) { GC.malloc(size) }.pointer, ->(pointer : Void*, data : Void*) { GC.free(pointer) }.pointer, nil) | ||
| end | ||
|
|
||
| # Returns a JIT stack that's shared in the current thread. | ||
| # | ||
| # Only a single `match` function can run per thread at any given time, so there | ||
| # can't be any concurrent access to the JIT stack. | ||
| @[ThreadLocal] | ||
| class_getter jit_stack : LibPCRE2::JITStack do | ||
| jit_stack = LibPCRE2.jit_stack_create(32_768, 1_048_576, Regex::PCRE2.general_context) | ||
|
Member
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. Maybe for a follow up, but these magic numbers could be or'ed with the |
||
| if jit_stack.null? | ||
| raise "Error allocating JIT stack" | ||
| end | ||
| jit_stack | ||
| end | ||
|
|
||
| private def match_data(str, byte_index, options) | ||
| match_data = LibPCRE2.match_data_create_from_pattern(@re, Regex::PCRE2.general_context) | ||
| match_count = LibPCRE2.match(@re, str, str.bytesize, byte_index, pcre2_options(options) | LibPCRE2::NO_UTF_CHECK, match_data, nil) | ||
| match_context = LibPCRE2.match_context_create(nil) | ||
| LibPCRE2.jit_stack_assign(match_context, nil, Regex::PCRE2.jit_stack.as(Void*)) | ||
| match_count = LibPCRE2.match(@re, str, str.bytesize, byte_index, pcre2_options(options) | LibPCRE2::NO_UTF_CHECK, match_data, match_context) | ||
|
|
||
| if match_count < 0 | ||
| case error = LibPCRE2::Error.new(match_count) | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Isn't this a bit too harsh? Without compilation the application might still work, even if slow. This said, I'm not sure how could we let the user know that something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
I'm not sure what errors can actually reasonably appear here. When JIT compilation is not supported, that's indicated by
ERROR_JIT_BADOPTIONand handled in the previous branch (we ignore that).Anything else, I don't know. But I would probably expect that could be more fatal errors where you may not be able to just continue on.
A more plausible point for ignoring errors might be when JIT stack allocation fails (
jit_stack_create).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.
PCRE2_ERROR_NOMEMORYis another one we might want to not raise. I wonder if it makes sense to just ignore any error and have a@compiled : Boolto know if it was compiled or not