Skip to content

perf: optimize find_in_set (up to 24x faster) - #23460

Merged
Dandandan merged 3 commits into
apache:mainfrom
andygrove:auto-opt/find_in_set-datafusion-20260710-123200
Jul 24, 2026
Merged

perf: optimize find_in_set (up to 24x faster)#23460
Dandandan merged 3 commits into
apache:mainfrom
andygrove:auto-opt/find_in_set-datafusion-20260710-123200

Conversation

@andygrove

@andygrove andygrove commented Jul 10, 2026

Copy link
Copy Markdown
Member

Which issue does this PR close?

N/A

Rationale for this change

Improve performance of existing expression.

What changes are included in this PR?

Replace per-row O(set_len) linear scan in find_in_set's constant-list path with a one-time HashMap lookup (threshold-guarded so short lists keep the linear scan), giving O(1) per-row probing for large sets.

Are these changes tested?

Existing tests + new tests

Benchmark (criterion):

  • long_list_256: 95.827% faster (base 1246412ns -> cand 52009ns) - ~24x faster
  • short_list_4: 2.13% faster (base 64343ns -> cand 62972ns)
  • long_list_64: 88.562% faster (base 422083ns -> cand 48276ns)

Are there any user-facing changes?

No

@github-actions github-actions Bot added the functions Changes to functions implementation label Jul 10, 2026
@andygrove
andygrove marked this pull request as ready for review July 10, 2026 19:14
@andygrove andygrove added the performance Make DataFusion faster label Jul 10, 2026
// built at most once here rather than per row.
let map: Option<HashMap<&str, usize>> =
(str_list.len() >= FIND_IN_SET_LOOKUP_THRESHOLD).then(|| {
let mut map = HashMap::with_capacity(str_list.len());

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This map is reconstructed for each batch being processed. Would be better to reuse across batches.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I looked into this, and it doesn't seem like it would improve performance much, and would be a significant change.

@andygrove andygrove changed the title perf: optimize find_in_set in datafusion-functions perf: optimize find_in_set (up to 24x faster) Jul 13, 2026
@andygrove
andygrove requested a review from Jefffrey July 13, 2026 20:25
.iter()
.position(|s| *s == string)
.map_or(0, |idx| idx + 1);
let position = match &map {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we pull the match on the map outside the loop or doesnt make too much of a difference?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good question — I benchmarked it. Hoisting the match into two loops is a tradeoff rather than a win: the map path gets ~5–7% faster (long_list_64 −5.0%, long_list_256 −7.2%), but the short-list linear-scan path regresses ~8% (short_list_4 +7.7%). Since lists below the threshold stay on the linear scan and short lists are the common case for find_in_set, I would rather not regress them for a gain on the path that is already HashMap-optimized. Keeping the single loop. Results were stable across three runs and two builds.

(Disclosure: I used an LLM-based coding assistant to run these benchmarks and help draft this reply.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

kinda interesting how it causes a regression, woulda thought with less branching it at least be the same 🤔

.iter()
.position(|s| *s == string)
.map_or(0, |idx| idx + 1);
let position = match &map {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

kinda interesting how it causes a regression, woulda thought with less branching it at least be the same 🤔

@codecov-commenter

codecov-commenter commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.71%. Comparing base (abc226e) to head (9665433).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23460      +/-   ##
==========================================
- Coverage   80.71%   80.71%   -0.01%     
==========================================
  Files        1090     1090              
  Lines      369828   369866      +38     
  Branches   369828   369866      +38     
==========================================
+ Hits       298493   298520      +27     
- Misses      53548    53552       +4     
- Partials    17787    17794       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

.iter()
.position(|s| *s == string)
.map_or(0, |idx| idx + 1);
let position = match &map {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

a possible microoptimization if we can match through some bool expression, rather than Option

@Dandandan
Dandandan added this pull request to the merge queue Jul 24, 2026
Merged via the queue into apache:main with commit b8d3b0b Jul 24, 2026
38 checks passed
kosiew pushed a commit to kosiew/datafusion that referenced this pull request Aug 12, 2026
## Which issue does this PR close?

N/A

## Rationale for this change

Improve performance of existing expression.

## What changes are included in this PR?

Replace per-row O(set_len) linear scan in find_in_set's constant-list
path with a one-time HashMap lookup (threshold-guarded so short lists
keep the linear scan), giving O(1) per-row probing for large sets.

## Are these changes tested?

Existing tests + new tests

Benchmark (criterion):

- long_list_256: 95.827% faster (base 1246412ns -> cand 52009ns) - ~24x
faster
- short_list_4: 2.13% faster (base 64343ns -> cand 62972ns)
- long_list_64: 88.562% faster (base 422083ns -> cand 48276ns)


## 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.
-->

Co-authored-by: Jeffrey Vo <jeffrey.vo.australia@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation performance Make DataFusion faster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants