-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
PCRE2: use thread local for jit stack and match data #16175
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
base: master
Are you sure you want to change the base?
PCRE2: use thread local for jit stack and match data #16175
Conversation
This is a simple abstraction over the `ThreadLocal` annotation (for quick access) that also injects a reference into `Thread.current` to keep the value visible to the GC (that can't scan `ThreadLocal` values). There is no support for destructors. When needed just use a class with a finalizer: when the Thread is collected the thread local value will also be collected, which will run the finalizer.
Refactors the PCRE2 allocations for the jit stack scratch space and match data to once per thread, instead of once per thread per Regex. No more `Crystal::ThreadLocalValue` that involves a mutex to protect a global hash (contention and complexity). Regular expressions may run a bit faster, especially in a MT environment. Relies on the `thread_local` macro and class wrappers for the raw PCRE2 pointers with a finalizer method that will free the allocations when we don't need them anymore (i.e. the thread has stopped). There are still no strong dependency: the external libpcre2 won't be linked if the program doesn't use regular expressions.
class JITStackPtr | ||
def initialize(start_size, max_size) | ||
@ptr = LibPCRE2.jit_stack_create(start_size, max_size, nil) | ||
raise RuntimeError.new("Error allocating JIT stack") if @ptr.null? | ||
end | ||
|
||
def finalize | ||
LibPCRE2.jit_stack_free(@ptr) | ||
end | ||
|
||
def to_unsafe | ||
@ptr | ||
end | ||
end | ||
|
||
class MatchDataPtr | ||
def initialize(ovec_size) | ||
@ptr = LibPCRE2.match_data_create(ovec_size, nil) | ||
raise RuntimeError.new("Error allocating match data") if @ptr.null? | ||
end | ||
|
||
def finalize | ||
LibPCRE2.match_data_free(@ptr) | ||
end | ||
|
||
def to_unsafe | ||
@ptr | ||
end | ||
end |
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.
thought: It feels a bit silly to have these two special types just in order to associate a finalizer with a pointer. I'm wondering if we could consider a more generic abstraction to encapsulate a value + finalizer. This would be easily reusable for similar use cases.
For example something like this:
class ValueWithFinalizer(T)
getter value : T
def initialize(@value : T, @finalizer : T ->)
end
def finalize
@finalizer.call(self.value)
end
end
jit_stack = ValueWithFinalizer.new(
LibPCRE2.jit_stack_create(start_size, max_size, nil),
->LibPCRE2.jit_stack_free(Void*)
)
match_data = ValueWithFinalizer.new(
LibPCRE2.match_data_create(ovec_size, nil),
->LibPCRE2.match_data_free(Void*)
)
# We need to dup the ovector because `match_data` is re-used for subsequent | ||
# matches (see `@match_data`). | ||
# Dup brings the ovector data into the realm of the GC. | ||
# matches. Dup brings the ovector data into the realm of the GC. |
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.
issue: This comment is no longer correct: "Dup brings the ovector data into the realm of the GC."
Previously, match_data
was allocated by libpcre2
outside the GC. Now, we already allocate the per-thread buffer in the GC. So dup moves it from thread-shared memory into memory owned by this instance.
# matches. Dup brings the ovector data into the realm of the GC. | |
# matches. Dup copies the ovector data into memory owned by this particular instance. |
See #15395 for details.
Depends on #16173.