Skip to content

Contrib/issue 11083 - #12529

Open
yutianjiang39 wants to merge 2 commits into
apache:mainfrom
yutianjiang39:contrib/issue-11083
Open

yutianjiang39 wants to merge 2 commits into
apache:mainfrom
yutianjiang39:contrib/issue-11083

Conversation

@yutianjiang39

Copy link
Copy Markdown

Which issue does this PR close?

Closes #11083.

Rationale for this change

When a pattern column repeats, regexp_is_match reuses the compiled regex but still copies the pattern string on every cache hit. With flags, it also rebuilds the flag prefix for each lookup.

What changes are included in this PR?

The array kernel now borrows its cache keys from the input columns. The loop without flags looks up &str. The loop with flags looks up (&str, Option<&str>). Each uses a single entry lookup and only compiles on a miss. A present flag gets its prefix at that point, even when the pattern is empty.

The first commit adds the repeated-pattern benchmark so it can run against the original kernel and the optimized kernel. Run cargo bench -p arrow --features test_utils --bench regexp_kernels -- regexp_is_match/repeated_pattern to compare constant and random nullable inputs, with and without flags.

Are these changes tested?

The allocation check counts allocations on the calling thread during one kernel call with two alternating patterns. Adding 8,128 rows cost one allocation per extra row on the baseline. With the patch, both counts stayed flat.

Flags Baseline, 64 rows Baseline, 8,192 rows Patched, 64 rows Patched, 8,192 rows
None 143 8,271 78 78
Repeated i 173 8,301 110 110

Benchmark results from cargo bench -p arrow --features test_utils --bench regexp_kernels -- regexp_is_match/repeated_pattern, with the first commit as the baseline (Linux, 4 vCPU AMD EPYC 9K65, 65,536 rows):

Input Flags Baseline Patched Change
constant none 2.05 ms 1.44 ms −29.6%
constant i 3.93 ms 2.26 ms −42.3%
random, nullable none 1.98 ms 1.28 ms −35.2%
random, nullable i 3.74 ms 1.90 ms −49.0%

The gain comes from no longer building an owned String per row before the cache lookup (and, with flags, no per-row format!), which matches the allocation counts above. The flags path gains more because it used to format a new string for every row. None of the measured inputs got slower.

The allocation check runs separately because it installs a global allocator. The PR includes unit tests for Utf8, LargeUtf8 and Utf8View. They check the same pattern with different flags, null inputs, empty patterns and compile errors, including Some("") flags. These cases pass on both the baseline and the patch. All 201 arrow-string unit tests, 10 doctests and lint checks passed with the patch.

Are there any significant user-facing changes?

The optimization applies only to array/array regexp_is_match and its cache lasts for one call. Matching results, errors and the public API stay the same.

@boring-cyborg boring-cyborg Bot added arrow-core Core crates related to representing arrow data arrow-kernels Crates that transform arrow data (e.g. compute, cast) labels Dec 1, 2026

This branch has not been deployed

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

Labels

arrow-core Core crates related to representing arrow data arrow-kernels Crates that transform arrow data (e.g. compute, cast)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid per-row pattern allocation in regexp_is_match for repeated patterns

1 participant