Skip to content

perf: batch contiguous extend calls in array_replace#22119

Merged
comphead merged 2 commits into
apache:mainfrom
lyne7-sc:perf/array_replace_clean
May 12, 2026
Merged

perf: batch contiguous extend calls in array_replace#22119
comphead merged 2 commits into
apache:mainfrom
lyne7-sc:perf/array_replace_clean

Conversation

@lyne7-sc
Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • Closes #.

Rationale for this change

array_replace / array_replace_n / array_replace_all currently call MutableArrayData::extend once per non-matching element. This pr batches consecutive non-matching elements into a single extend call, reducing call count.

What changes are included in this PR?

  • Track contiguous non-matching runs and flush them as one extend call.
  • Add Criterion benchmark and SLT edge-case tests for n = 0 / -1 / > match_count.

Benchmarks

group                                                                      main                                    perf
-----                                                                      ----                                    ----
array_replace_all_int64/replace/list size: 10, num_rows: 4000              1.06  1149.7±23.40µs        ? ?/sec     1.00  1080.3±71.22µs        ? ?/sec
array_replace_all_int64/replace/list size: 100, num_rows: 10000            1.70     11.2±0.19ms        ? ?/sec     1.00      6.6±0.36ms        ? ?/sec
array_replace_all_int64/replace/list size: 500, num_rows: 10000            1.94     44.2±0.71ms        ? ?/sec     1.00     22.8±0.48ms        ? ?/sec
array_replace_all_int64_nested/replace/list size: 10, num_rows: 4000       1.03      7.4±0.05ms        ? ?/sec     1.00      7.2±0.26ms        ? ?/sec
array_replace_all_int64_nested/replace/list size: 100, num_rows: 3000      1.14     42.1±0.57ms        ? ?/sec     1.00     36.9±0.22ms        ? ?/sec
array_replace_all_int64_nested/replace/list size: 300, num_rows: 1500      1.14     60.5±2.57ms        ? ?/sec     1.00     52.8±0.60ms        ? ?/sec
array_replace_boolean/replace/list size: 10, num_rows: 4000                1.04  1087.5±25.31µs        ? ?/sec     1.00  1042.4±18.85µs        ? ?/sec
array_replace_boolean/replace/list size: 100, num_rows: 10000              1.20      4.2±0.13ms        ? ?/sec     1.00      3.5±0.06ms        ? ?/sec
array_replace_boolean/replace/list size: 500, num_rows: 10000              1.11      7.0±0.28ms        ? ?/sec     1.00      6.3±0.06ms        ? ?/sec
array_replace_fixed_size_binary/replace/list size: 10, num_rows: 4000      1.03  1187.3±13.63µs        ? ?/sec     1.00  1155.9±26.39µs        ? ?/sec
array_replace_fixed_size_binary/replace/list size: 100, num_rows: 10000    1.01      7.0±0.08ms        ? ?/sec     1.00      6.9±0.67ms        ? ?/sec
array_replace_fixed_size_binary/replace/list size: 500, num_rows: 10000    1.00     23.6±0.61ms        ? ?/sec     1.01     23.8±0.92ms        ? ?/sec
array_replace_int64/replace/list size: 10, num_rows: 4000                  1.07  1071.8±25.30µs        ? ?/sec     1.00  1003.3±24.68µs        ? ?/sec
array_replace_int64/replace/list size: 100, num_rows: 10000                1.16      4.0±0.07ms        ? ?/sec     1.00      3.4±0.09ms        ? ?/sec
array_replace_int64/replace/list size: 500, num_rows: 10000                1.10      7.2±0.31ms        ? ?/sec     1.00      6.6±0.34ms        ? ?/sec
array_replace_int64_nested/replace/list size: 10, num_rows: 4000           1.02      7.2±0.07ms        ? ?/sec     1.00      7.0±0.21ms        ? ?/sec
array_replace_int64_nested/replace/list size: 100, num_rows: 3000          1.01     36.1±0.38ms        ? ?/sec     1.00     35.8±1.62ms        ? ?/sec
array_replace_int64_nested/replace/list size: 300, num_rows: 1500          1.00     51.2±0.48ms        ? ?/sec     1.00     51.1±0.67ms        ? ?/sec
array_replace_n_int64/replace/list size: 10, num_rows: 4000                1.09  1119.2±24.61µs        ? ?/sec     1.00  1022.4±20.53µs        ? ?/sec
array_replace_n_int64/replace/list size: 100, num_rows: 10000              1.45      7.2±0.22ms        ? ?/sec     1.00      5.0±0.16ms        ? ?/sec
array_replace_n_int64/replace/list size: 500, num_rows: 10000              1.68     25.3±0.38ms        ? ?/sec     1.00     15.1±1.32ms        ? ?/sec
array_replace_n_int64_nested/replace/list size: 10, num_rows: 4000         1.03      7.4±0.08ms        ? ?/sec     1.00      7.1±0.07ms        ? ?/sec
array_replace_n_int64_nested/replace/list size: 100, num_rows: 3000        1.05     38.5±0.36ms        ? ?/sec     1.00     36.5±1.38ms        ? ?/sec
array_replace_n_int64_nested/replace/list size: 300, num_rows: 1500        1.04     55.0±0.41ms        ? ?/sec     1.00     52.6±1.99ms        ? ?/sec
array_replace_strings/replace/list size: 10, num_rows: 4000                1.07  1443.5±18.35µs        ? ?/sec     1.00  1348.6±41.07µs        ? ?/sec
array_replace_strings/replace/list size: 100, num_rows: 10000              1.09     10.9±0.17ms        ? ?/sec     1.00      9.9±0.17ms        ? ?/sec
array_replace_strings/replace/list size: 500, num_rows: 10000              1.00     36.2±0.82ms        ? ?/sec     1.04     37.5±0.91ms        ? ?/sec

Are these changes tested?

Yes — existing SLT tests and new edge-case coverage.

Are there any user-facing changes?

No.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels May 12, 2026
Copy link
Copy Markdown
Member

@waynexia waynexia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great, thank you @lyne7-sc. This optimization is very clean and effective 👍

@waynexia
Copy link
Copy Markdown
Member

This patch optimizes cases when the element to replace is sparse. Maybe for the opposite scenario where elements to retain are sparse can also benefit from a similar optimization. Like only materialize an extend when the boolean flag flips.

@waynexia
Copy link
Copy Markdown
Member

Just mention it, we can explore this in a follow up PR

@comphead
Copy link
Copy Markdown
Contributor

Thanks everyone!

@comphead comphead added this pull request to the merge queue May 12, 2026
Merged via the queue into apache:main with commit 484bddb May 12, 2026
36 checks passed
@lyne7-sc
Copy link
Copy Markdown
Contributor Author

@waynexia @comphead Thanks for the review and suggestions! I'll look into the optimizations in a follow up PR.

@lyne7-sc lyne7-sc deleted the perf/array_replace_clean branch May 13, 2026 02:03
gstvg pushed a commit to gstvg/arrow-datafusion that referenced this pull request May 14, 2026
## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Closes #.

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

array_replace / array_replace_n / array_replace_all currently call
`MutableArrayData::extend` once per non-matching element. This pr
batches consecutive non-matching elements into a single extend call,
reducing call count.

## What changes are included in this PR?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

- Track contiguous non-matching runs and flush them as one extend call.
- Add Criterion benchmark and SLT edge-case tests for n = 0 / -1 / >
match_count.

### Benchmarks
```
group                                                                      main                                    perf
-----                                                                      ----                                    ----
array_replace_all_int64/replace/list size: 10, num_rows: 4000              1.06  1149.7±23.40µs        ? ?/sec     1.00  1080.3±71.22µs        ? ?/sec
array_replace_all_int64/replace/list size: 100, num_rows: 10000            1.70     11.2±0.19ms        ? ?/sec     1.00      6.6±0.36ms        ? ?/sec
array_replace_all_int64/replace/list size: 500, num_rows: 10000            1.94     44.2±0.71ms        ? ?/sec     1.00     22.8±0.48ms        ? ?/sec
array_replace_all_int64_nested/replace/list size: 10, num_rows: 4000       1.03      7.4±0.05ms        ? ?/sec     1.00      7.2±0.26ms        ? ?/sec
array_replace_all_int64_nested/replace/list size: 100, num_rows: 3000      1.14     42.1±0.57ms        ? ?/sec     1.00     36.9±0.22ms        ? ?/sec
array_replace_all_int64_nested/replace/list size: 300, num_rows: 1500      1.14     60.5±2.57ms        ? ?/sec     1.00     52.8±0.60ms        ? ?/sec
array_replace_boolean/replace/list size: 10, num_rows: 4000                1.04  1087.5±25.31µs        ? ?/sec     1.00  1042.4±18.85µs        ? ?/sec
array_replace_boolean/replace/list size: 100, num_rows: 10000              1.20      4.2±0.13ms        ? ?/sec     1.00      3.5±0.06ms        ? ?/sec
array_replace_boolean/replace/list size: 500, num_rows: 10000              1.11      7.0±0.28ms        ? ?/sec     1.00      6.3±0.06ms        ? ?/sec
array_replace_fixed_size_binary/replace/list size: 10, num_rows: 4000      1.03  1187.3±13.63µs        ? ?/sec     1.00  1155.9±26.39µs        ? ?/sec
array_replace_fixed_size_binary/replace/list size: 100, num_rows: 10000    1.01      7.0±0.08ms        ? ?/sec     1.00      6.9±0.67ms        ? ?/sec
array_replace_fixed_size_binary/replace/list size: 500, num_rows: 10000    1.00     23.6±0.61ms        ? ?/sec     1.01     23.8±0.92ms        ? ?/sec
array_replace_int64/replace/list size: 10, num_rows: 4000                  1.07  1071.8±25.30µs        ? ?/sec     1.00  1003.3±24.68µs        ? ?/sec
array_replace_int64/replace/list size: 100, num_rows: 10000                1.16      4.0±0.07ms        ? ?/sec     1.00      3.4±0.09ms        ? ?/sec
array_replace_int64/replace/list size: 500, num_rows: 10000                1.10      7.2±0.31ms        ? ?/sec     1.00      6.6±0.34ms        ? ?/sec
array_replace_int64_nested/replace/list size: 10, num_rows: 4000           1.02      7.2±0.07ms        ? ?/sec     1.00      7.0±0.21ms        ? ?/sec
array_replace_int64_nested/replace/list size: 100, num_rows: 3000          1.01     36.1±0.38ms        ? ?/sec     1.00     35.8±1.62ms        ? ?/sec
array_replace_int64_nested/replace/list size: 300, num_rows: 1500          1.00     51.2±0.48ms        ? ?/sec     1.00     51.1±0.67ms        ? ?/sec
array_replace_n_int64/replace/list size: 10, num_rows: 4000                1.09  1119.2±24.61µs        ? ?/sec     1.00  1022.4±20.53µs        ? ?/sec
array_replace_n_int64/replace/list size: 100, num_rows: 10000              1.45      7.2±0.22ms        ? ?/sec     1.00      5.0±0.16ms        ? ?/sec
array_replace_n_int64/replace/list size: 500, num_rows: 10000              1.68     25.3±0.38ms        ? ?/sec     1.00     15.1±1.32ms        ? ?/sec
array_replace_n_int64_nested/replace/list size: 10, num_rows: 4000         1.03      7.4±0.08ms        ? ?/sec     1.00      7.1±0.07ms        ? ?/sec
array_replace_n_int64_nested/replace/list size: 100, num_rows: 3000        1.05     38.5±0.36ms        ? ?/sec     1.00     36.5±1.38ms        ? ?/sec
array_replace_n_int64_nested/replace/list size: 300, num_rows: 1500        1.04     55.0±0.41ms        ? ?/sec     1.00     52.6±1.99ms        ? ?/sec
array_replace_strings/replace/list size: 10, num_rows: 4000                1.07  1443.5±18.35µs        ? ?/sec     1.00  1348.6±41.07µs        ? ?/sec
array_replace_strings/replace/list size: 100, num_rows: 10000              1.09     10.9±0.17ms        ? ?/sec     1.00      9.9±0.17ms        ? ?/sec
array_replace_strings/replace/list size: 500, num_rows: 10000              1.00     36.2±0.82ms        ? ?/sec     1.04     37.5±0.91ms        ? ?/sec
```

## Are these changes tested?

Yes — existing SLT tests and new edge-case coverage.

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

## Are there any user-facing changes?

No.

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants