-
Notifications
You must be signed in to change notification settings - Fork 2k
perf: Optimize Utf8View string concat
#21535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
comphead
merged 4 commits into
apache:main
from
neilconway:neilc/perf-string-concat-nulls
Apr 10, 2026
+115
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use arrow::array::StringViewArray; | ||
| use arrow::datatypes::{DataType, Field, Schema}; | ||
| use arrow::record_batch::RecordBatch; | ||
| use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; | ||
| use datafusion_expr::Operator; | ||
| use datafusion_physical_expr::PhysicalExpr; | ||
| use datafusion_physical_expr::expressions::{BinaryExpr, Column}; | ||
| use rand::rngs::StdRng; | ||
| use rand::{Rng, SeedableRng}; | ||
| use std::hint::black_box; | ||
| use std::sync::Arc; | ||
|
|
||
| const NUM_ROWS: usize = 8192; | ||
| const SEED: u64 = 42; | ||
|
|
||
| fn create_string_view_array( | ||
| num_rows: usize, | ||
| str_len: usize, | ||
| null_density: f64, | ||
| seed: u64, | ||
| ) -> StringViewArray { | ||
| let mut rng = StdRng::seed_from_u64(seed); | ||
| let values: Vec<Option<String>> = (0..num_rows) | ||
| .map(|_| { | ||
| if rng.random::<f64>() < null_density { | ||
| None | ||
| } else { | ||
| let s: String = (0..str_len) | ||
| .map(|_| rng.random_range(b'a'..=b'z') as char) | ||
| .collect(); | ||
| Some(s) | ||
| } | ||
| }) | ||
| .collect(); | ||
| StringViewArray::from_iter(values) | ||
| } | ||
|
|
||
| fn bench_concat_utf8view(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group("concat_utf8view"); | ||
|
|
||
| let schema = Arc::new(Schema::new(vec![ | ||
| Field::new("left", DataType::Utf8View, true), | ||
| Field::new("right", DataType::Utf8View, true), | ||
| ])); | ||
|
|
||
| // left || right | ||
| let expr = BinaryExpr::new( | ||
| Arc::new(Column::new("left", 0)), | ||
| Operator::StringConcat, | ||
| Arc::new(Column::new("right", 1)), | ||
| ); | ||
|
|
||
| for null_density in [0.0, 0.1, 0.5] { | ||
| let left = create_string_view_array(NUM_ROWS, 16, null_density, SEED); | ||
| let right = create_string_view_array(NUM_ROWS, 16, null_density, SEED + 1); | ||
|
|
||
| let batch = | ||
| RecordBatch::try_new(schema.clone(), vec![Arc::new(left), Arc::new(right)]) | ||
| .unwrap(); | ||
|
|
||
| let label = format!("nulls_{}", (null_density * 100.0) as u32); | ||
| group.bench_with_input( | ||
| BenchmarkId::new("concat", &label), | ||
| &null_density, | ||
| |b, _| { | ||
| b.iter(|| { | ||
| black_box(expr.evaluate(black_box(&batch)).unwrap()); | ||
| }) | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| criterion_group!(benches, bench_concat_utf8view); | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| //! This module contains computation kernels that are specific to | ||
| //! datafusion and not (yet) targeted to port upstream to arrow | ||
| use arrow::array::*; | ||
| use arrow::buffer::NullBuffer; | ||
| use arrow::compute::kernels::bitwise::{ | ||
| bitwise_and, bitwise_and_scalar, bitwise_or, bitwise_or_scalar, bitwise_shift_left, | ||
| bitwise_shift_left_scalar, bitwise_shift_right, bitwise_shift_right_scalar, | ||
|
|
@@ -177,24 +178,27 @@ pub fn concat_elements_utf8view( | |
| right.len() | ||
| ))); | ||
| } | ||
| let capacity = left.len(); | ||
| let mut result = StringViewBuilder::with_capacity(capacity); | ||
| let mut result = StringViewBuilder::with_capacity(left.len()); | ||
|
|
||
| // Avoid reallocations by writing to a reused buffer (note we | ||
| // could be even more efficient r by creating the view directly | ||
| // here and avoid the buffer but that would be more complex) | ||
| // Avoid reallocations by writing to a reused buffer (note we could be even | ||
| // more efficient by creating the view directly here and avoid the buffer | ||
| // but that would be more complex) | ||
| let mut buffer = String::new(); | ||
|
|
||
| for (left, right) in left.iter().zip(right.iter()) { | ||
| if let (Some(left), Some(right)) = (left, right) { | ||
| use std::fmt::Write; | ||
| // Pre-compute combined null bitmap, so the per-row NULL check is more | ||
| // efficient | ||
| let nulls = NullBuffer::union(left.nulls(), right.nulls()); | ||
|
|
||
| for i in 0..left.len() { | ||
| if nulls.as_ref().is_some_and(|n| n.is_null(i)) { | ||
| result.append_null(); | ||
| } else { | ||
| let l = left.value(i); | ||
| let r = right.value(i); | ||
| buffer.clear(); | ||
| write!(&mut buffer, "{left}{right}") | ||
| .expect("writing into string buffer failed"); | ||
| buffer.push_str(l); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| buffer.push_str(r); | ||
| result.try_append_value(&buffer)?; | ||
| } else { | ||
| // at least one of the values is null, so the output is also null | ||
| result.append_null() | ||
| } | ||
| } | ||
| Ok(result.finish()) | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really need to check
nulls.as_ref().is_somein loop?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could hoist this outside the loop, although
I'd say the current approach is okay but lmk if you disagree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checked with godbolt and and yeah, looks like compiler rewrote the thing correctly so it identifies
is_someoutside of the loop 😮