PCRE2: optimize memory allocations #15395
Draft
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.
We noticed in #15088 that we don't need
Crystal::ThreadLocalValue
in the Regex PCRE2 engine.We can reuse the JIT stack but also the match data for every Regex (no need for a specific match data per Regex). We can allocate one and make sure it's not used by other threads... hence the thread locals: no more spinlock (thread contention) nor hash.
It's simpler and faster. Here's the benchmark from #13144 for example:
Enabling MT also no longer has any impact on performance:
The drawback is that we must allocate each matchdata with a maximum number of ovectors (65535). That might increase memory usage, though I failed to notice it in practice. Maybe not allocating memory for every regular expression is helping?
Note: this PR will be separated into a couple PRs to introduce
Crystal::System::ThreadLocal(T)
. The point of this new type is for this patch, so I want approval on the overall approach before the split.I could have used
@[ThreadLocal]
but some targets don't support it (namely: Android, MinGW and OpenBSD) and we can't register destructors either (on thread shutdown). But usingpthread_key_create
orFlsAlloc
we can 😍