Skip to content

PCRE2: use thread local for jit stack and match data#16175

Merged
straight-shoota merged 5 commits intocrystal-lang:masterfrom
ysbaddaden:refactor/pcre2-use-thread-local
Oct 18, 2025
Merged

PCRE2: use thread local for jit stack and match data#16175
straight-shoota merged 5 commits intocrystal-lang:masterfrom
ysbaddaden:refactor/pcre2-use-thread-local

Conversation

@ysbaddaden
Copy link
Collaborator

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.
@ysbaddaden ysbaddaden marked this pull request as ready for review October 8, 2025 13:36
@ysbaddaden
Copy link
Collaborator Author

We might want to wait for #16194, then revert the last commit before merge (we don't bother with backward compatibility with the interpreter, yet).

@straight-shoota
Copy link
Member

straight-shoota commented Oct 9, 2025

I'd rather not bother with that. We can merge this with the wrapper and then simplify after #16194.
There might be more places to simplify in the code base anyway.

Backwards compatibility of the interpreter may not be as important. But it's very inconvenient for testing purposes if stdlib code doesn't work with interpreter from the latest release. It means you need to rebuild the compiler with changes from master.
So I'd prefer to simplify one release after support for proc pointers has landed in the interpreter.

@ysbaddaden
Copy link
Collaborator Author

Well, that's the case right now with interpreter_proc_new. A bit annoying, but nothing serious, it's just a make interpreter=1 call.

@ysbaddaden ysbaddaden added this to the 1.19.0 milestone Oct 13, 2025
@ysbaddaden ysbaddaden moved this from Review to Approved in Multi-threading Oct 13, 2025
@straight-shoota straight-shoota merged commit 340f024 into crystal-lang:master Oct 18, 2025
41 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in Multi-threading Oct 18, 2025
@ysbaddaden ysbaddaden deleted the refactor/pcre2-use-thread-local branch October 18, 2025 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants