Skip to content

Conversation

ysbaddaden
Copy link
Contributor

See #15395 for details.
Depends on #16173.

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.
Comment on lines +5 to +33
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
Copy link
Member

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.
Copy link
Member

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.

Suggested change
# 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Review
Development

Successfully merging this pull request may close these issues.

2 participants