Skip to content

GH-53159: [C++][Compute] Cache substring and regex matchers across invocations in scalar string kernels - #53160

Open
ishaaaan17 wants to merge 1 commit into
apache:mainfrom
ishaaaan17:perf/compute-cache-substring-matcher
Open

ishaaaan17 wants to merge 1 commit into
apache:mainfrom
ishaaaan17:perf/compute-cache-substring-matcher

Conversation

@ishaaaan17

@ishaaaan17 ishaaaan17 commented Oct 11, 2026 •

Copy link
Copy Markdown

rationale for this change

in cpp/src/arrow/compute/kernels/scalar_string_ascii.cc, the string matching kernels (match_substring, starts_with, ends_with, match_substring_regex and match_like) rebuild their matcher on every call to Exec. for regex matchers that means the RE2 DFA is recompiled for every batch in a stream or chunked array. plain matchers reallocate and rebuild the Knuth-Morris-Pratt prefix table each time.

this also closes out the existing todo in MatchSubstring:
// TODO Cache matcher across invocations (for regex compilation).

what changes are included in this PR?

  • replaced OptionsWrapper<MatchSubstringOptions> with a MatchSubstringState : public KernelState that caches the plain and regex matchers.
  • kept MatchSubstringState::Get(ctx) so other kernels and tests that use the old accessor still work.
  • PlainSubstringMatcher, PlainStartsWithMatcher, PlainEndsWithMatcher and RegexSubstringMatcher now reuse their compiled matchers across batches.
  • MatchLike looks at the SQL LIKE pattern and sets up the converter state once, instead of once per batch.
  • ran clang-format.

are these changes tested?

yes, by the existing kernel tests in cpp/src/arrow/compute/kernels/scalar_string_test.cc. they run the matching kernels over chunks, slices and scalars through CheckScalarUnary.

are there any user-facing changes?

no public api changes.

was ai used for this PR?

in line with the ai generation guidelines, here is how ai was used.

pr code and description written by:

  • human
  • ai

reviewed before submission by:

  • human
  • ai
  • not reviewed

i used an llm assistant to look at different ways of caching the matchers and to draft the first version of the changes. i then went through and reworked the design myself, checked that the old state accessors still work, made sure the options are stored safely, and formatted the code.

@github-actions github-actions Bot added the awaiting review Awaiting review label Oct 11, 2026
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #53159 has been automatically assigned in GitHub to PR creator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review Awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant