-
Notifications
You must be signed in to change notification settings - Fork 1.9k
perf: improve performance of string replace #19530
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,193 @@ | ||
| // 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. | ||
|
|
||
| extern crate criterion; | ||
|
|
||
| use arrow::array::OffsetSizeTrait; | ||
| use arrow::datatypes::{DataType, Field}; | ||
| use arrow::util::bench_util::{ | ||
| create_string_array_with_len, create_string_view_array_with_len, | ||
| }; | ||
| use criterion::{Criterion, SamplingMode, criterion_group, criterion_main}; | ||
| use datafusion_common::DataFusionError; | ||
| use datafusion_common::config::ConfigOptions; | ||
| use datafusion_expr::{ColumnarValue, ScalarFunctionArgs}; | ||
| use datafusion_functions::string; | ||
| use std::hint::black_box; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
|
|
||
| fn create_args<O: OffsetSizeTrait>( | ||
| size: usize, | ||
| str_len: usize, | ||
| force_view_types: bool, | ||
| from_len: usize, | ||
| to_len: usize, | ||
| ) -> Vec<ColumnarValue> { | ||
| if force_view_types { | ||
| let string_array = | ||
| Arc::new(create_string_view_array_with_len(size, 0.1, str_len, false)); | ||
| let from_array = Arc::new(create_string_view_array_with_len( | ||
| size, 0.1, from_len, false, | ||
| )); | ||
| let to_array = | ||
| Arc::new(create_string_view_array_with_len(size, 0.1, to_len, false)); | ||
| vec![ | ||
| ColumnarValue::Array(string_array), | ||
| ColumnarValue::Array(from_array), | ||
| ColumnarValue::Array(to_array), | ||
| ] | ||
| } else { | ||
| let string_array = | ||
| Arc::new(create_string_array_with_len::<O>(size, 0.1, str_len)); | ||
| let from_array = Arc::new(create_string_array_with_len::<O>(size, 0.1, from_len)); | ||
| let to_array = Arc::new(create_string_array_with_len::<O>(size, 0.1, to_len)); | ||
|
|
||
| vec![ | ||
| ColumnarValue::Array(string_array), | ||
| ColumnarValue::Array(from_array), | ||
| ColumnarValue::Array(to_array), | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| fn invoke_replace_with_args( | ||
| args: Vec<ColumnarValue>, | ||
| number_rows: usize, | ||
| ) -> Result<ColumnarValue, DataFusionError> { | ||
| let arg_fields = args | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(idx, arg)| Field::new(format!("arg_{idx}"), arg.data_type(), true).into()) | ||
| .collect::<Vec<_>>(); | ||
| let config_options = Arc::new(ConfigOptions::default()); | ||
|
|
||
| string::replace().invoke_with_args(ScalarFunctionArgs { | ||
| args, | ||
| arg_fields, | ||
| number_rows, | ||
| return_field: Field::new("f", DataType::Utf8, true).into(), | ||
| config_options: Arc::clone(&config_options), | ||
| }) | ||
| } | ||
|
|
||
| fn criterion_benchmark(c: &mut Criterion) { | ||
| for size in [1024, 4096] { | ||
| let mut group = c.benchmark_group(format!("replace size={size}")); | ||
| group.sampling_mode(SamplingMode::Flat); | ||
| group.sample_size(10); | ||
| group.measurement_time(Duration::from_secs(10)); | ||
|
|
||
| // ASCII single character replacement (fast path) | ||
| let str_len = 32; | ||
| let args = create_args::<i32>(size, str_len, false, 1, 1); | ||
| group.bench_function( | ||
| format!("replace_string_ascii_single [size={size}, str_len={str_len}]"), | ||
| |b| { | ||
| b.iter(|| { | ||
| let args_cloned = args.clone(); | ||
| black_box(invoke_replace_with_args(args_cloned, size)) | ||
| }) | ||
| }, | ||
| ); | ||
|
|
||
| // Multi-character strings (general path) | ||
| let args = create_args::<i32>(size, str_len, true, 3, 5); | ||
| group.bench_function( | ||
| format!("replace_string_view [size={size}, str_len={str_len}]"), | ||
| |b| { | ||
| b.iter(|| { | ||
| let args_cloned = args.clone(); | ||
| black_box(invoke_replace_with_args(args_cloned, size)) | ||
| }) | ||
| }, | ||
| ); | ||
|
|
||
| let args = create_args::<i32>(size, str_len, false, 3, 5); | ||
| group.bench_function( | ||
| format!("replace_string [size={size}, str_len={str_len}]"), | ||
| |b| { | ||
| b.iter(|| { | ||
| let args_cloned = args.clone(); | ||
| black_box(invoke_replace_with_args(args_cloned, size)) | ||
| }) | ||
| }, | ||
| ); | ||
|
|
||
| let args = create_args::<i64>(size, str_len, false, 3, 5); | ||
| group.bench_function( | ||
| format!("replace_large_string [size={size}, str_len={str_len}]"), | ||
| |b| { | ||
| b.iter(|| { | ||
| let args_cloned = args.clone(); | ||
| black_box(invoke_replace_with_args(args_cloned, size)) | ||
| }) | ||
| }, | ||
| ); | ||
|
|
||
| // Larger strings | ||
| let str_len = 128; | ||
| let args = create_args::<i32>(size, str_len, false, 1, 1); | ||
| group.bench_function( | ||
| format!("replace_string_ascii_single [size={size}, str_len={str_len}]"), | ||
| |b| { | ||
| b.iter(|| { | ||
| let args_cloned = args.clone(); | ||
| black_box(invoke_replace_with_args(args_cloned, size)) | ||
| }) | ||
| }, | ||
| ); | ||
|
|
||
| let args = create_args::<i32>(size, str_len, true, 3, 5); | ||
| group.bench_function( | ||
| format!("replace_string_view [size={size}, str_len={str_len}]"), | ||
| |b| { | ||
| b.iter(|| { | ||
| let args_cloned = args.clone(); | ||
| black_box(invoke_replace_with_args(args_cloned, size)) | ||
| }) | ||
| }, | ||
| ); | ||
|
|
||
| let args = create_args::<i32>(size, str_len, false, 3, 5); | ||
| group.bench_function( | ||
| format!("replace_string [size={size}, str_len={str_len}]"), | ||
| |b| { | ||
| b.iter(|| { | ||
| let args_cloned = args.clone(); | ||
| black_box(invoke_replace_with_args(args_cloned, size)) | ||
| }) | ||
| }, | ||
| ); | ||
|
|
||
| let args = create_args::<i64>(size, str_len, false, 3, 5); | ||
| group.bench_function( | ||
| format!("replace_large_string [size={size}, str_len={str_len}]"), | ||
| |b| { | ||
| b.iter(|| { | ||
| let args_cloned = args.clone(); | ||
| black_box(invoke_replace_with_args(args_cloned, size)) | ||
| }) | ||
| }, | ||
| ); | ||
|
|
||
| group.finish(); | ||
| } | ||
| } | ||
|
|
||
| criterion_group!(benches, criterion_benchmark); | ||
| 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
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.
I wonder if we lost other optimization tricks that the Rust
replacefunction provides; for example looking at the code: https://doc.rust-lang.org/src/alloc/str.rs.html#268It has a
Fast path for replacing a single ASCII character with another. I wonder if we capture this in our benchmarks?Uh oh!
There was an error while loading. Please reload this page.
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.
Excellent point! Thanks for the review. The single ASCII character fast path optimization has been added.