GH-48728: [C++] Cache compiled regex matchers in string kernels #48729
+122
−47
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.
Rationale for this change
String operations with regex patterns (match, replace, extract) were recompiling regex patterns on every invocation. This PR implements caching to compile once and reuse.
Benchmark shows roughly 36% performance improvement (2.52s -> 1.61s for 200 operations).
arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Line 1371 in 727106f
arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Line 1381 in 727106f
arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Line 1965 in 727106f
arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Line 2218 in 727106f
What changes are included in this PR?
CachedOptionsWrapper<T>template for kernel state with caching supportMatchSubstringState,ReplaceState, andExtractRegexStateto use cachingExec()methods to callGetOrCreate<Matcher>()instead of directMatcher::Make()Are these changes tested?
Yes. All existing tests pass. Benchmark demonstrates measurable performance improvement when same pattern is used across multiple operations.
(Generated by ChatGPT)
Benchmark:
Results:
Are there any user-facing changes?
No, this is an optiomization.